message stringlengths 2 22.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 145 109k | cluster float64 9 9 | __index_level_0__ int64 290 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As usual, those who called wolves get together on 8 p.m. at the supermarket. The thing they want is only one, a box lunch that is labeled half price. Scrambling for a few discounted box lunch, they fiercely fight every day. And those who are blessed by hunger and appetite the best can acquire the box lunch, while others have to have cup ramen or something with tear in their eyes.
A senior high school student, Sato, is one of wolves. A dormitry he lives doesn't serve a dinner, and his parents don't send so much money. Therefore he absolutely acquire the half-priced box lunch and save his money. Otherwise he have to give up comic books and video games, or begin part-time job.
Since Sato is an excellent wolf, he can acquire the discounted box lunch in 100% probability on the first day. But on the next day, many other wolves cooperate to block him and the probability to get a box lunch will be 50%. Even though he can get, the probability to get will be 25% on the next day of the day. Likewise, if he gets a box lunch on a certain day, the probability to get on the next day will be half. Once he failed to get a box lunch, probability to get would be back to 100%.
He continue to go to supermaket and try to get the discounted box lunch for n days. Please write a program to computes the expected value of the number of the discounted box lunches he can acquire.
Constraints
* 1 ≤ n ≤ 100,000
Input
Input consists of several datasets.
Input for a single dataset is given as a single integer n.
Input terminates with a dataset where n = 0.
Output
For each dataset, write a line that contains an expected value. You may print any number of digits after the decimal point. Answers that have an error less than 1.0e-2 will be accepted.
Example
Input
1
2
3
0
Output
1.00000000
1.50000000
2.12500000
Submitted Solution:
```
code="""
#include <bits/stdc++.h>
using namespace std;
#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
print_tuple<Cdr...>(os,t);
os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
print_tuple<Args...>(os<<'(',t);
return os<<')';
}
template<typename Ch,typename Tr,typename C,typename=decltype(begin(C()))>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
os<<'[';
for(auto i=begin(c);i!=end(c);++i)
os<<(i==begin(c)?"":" ")<<*i;
return os<<']';
}
constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;
int main()
{
for(int n;cin>>n && n;){
map<pii,double> dp;
dp[mp(0,0)]=1;
rep(_,n){
map<pii,double> dp2;
for(auto p:dp){
if(p.second<1e-6) continue;
int cnt,cur; tie(cnt,cur)=p.first;
dp2[mp(cnt+1,cur+1)]+=p.second*1./(1<<cur);
dp2[mp(cnt,0)]+=p.second*(1-1./(1<<cur));
}
swap(dp,dp2);
}
double res=0;
for(auto p:dp)
res+=p.first.first*p.second;
printf("%.8f",res); puts("");
}
}
"""
import os,tempfile
(_,filename)=tempfile.mkstemp(".cpp")
f=open(filename,"w")
f.write(code)
f.close()
os.system("g++ -std=c++0x {} -o ./a.out".format(filename))
os.system("./a.out")
``` | instruction | 0 | 58,800 | 9 | 117,600 |
No | output | 1 | 58,800 | 9 | 117,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of n candies and a pile consisting of m stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat a candies and b stones. Then Mike awards Gerald f(a, b) prize points. Gerald during his move either eats a candy from the pile of candies or a stone from the pile of stones. As Mike sees that Gerald has eaten everything apart one candy and one stone, he awards points for the last time and the game ends. Gerald is not allowed to eat all the candies, and he is not allowed to eat all the stones too. Tell Gerald how to play to get the largest possible number of points: it is required to find one of the possible optimal playing strategies for Gerald.
Input
The first line contains three integers n, m, p (1 ≤ n, m ≤ 20000, 1 ≤ p ≤ 109). The second line contains n integers x0, x1, ..., xn - 1 (0 ≤ xi ≤ 20000). The third line contains m integers y0, y1, ..., ym - 1 (0 ≤ yi ≤ 20000). The value of f(a, b) is calculated as a remainder of the division of the sum xa + yb by number p.
Output
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of n + m - 2 characters, each of which is either a "C" or "S", the i-th character should be "C" if Gerald's i-th move should be eating a candy and "S" if he should eat a stone.
Examples
Input
2 2 10
0 0
0 1
Output
2
SC
Input
3 3 10
0 2 0
0 0 2
Output
10
CSSC
Input
3 3 2
0 1 1
1 1 0
Output
4
SCSC
Note
In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first eat a stone, then a candy.
Submitted Solution:
```
n, m, p = list(map(int, input().split()))
cands = list(map(int, input().split()))
stones = list(map(int, input().split()))
possbs = [[0] * n] * m
path = ''
for i in range(n): #candy
for j in range(m): #stone
if i == 0 and j == 0: possbs[j][i] = (cands[i] + stones[j])%p
elif i == 0: possbs[j][i] = possbs[j][i-1] + (stones[j])%p
elif j == 0: possbs[j][i] = possbs[j-1][i] + (cands[i])%p
else: possbs[j][i] = max(possbs[j][i-1], possbs[j-1][i]) + (cands[i] + stones[j])%p
m -= 1; n -= 1
print(possbs[m][n])
while n != 0 and m != 0:
if possbs[m][n] - (cands[n] + stones[m])%p == possbs[m][n-1]: path = 'C' + path; n -= 1
else: path = 'S' + path; m -= 1
if n == 0:
path = 'S'*m + path
else:
path = 'C'*n + path
print(path)
``` | instruction | 0 | 58,828 | 9 | 117,656 |
No | output | 1 | 58,828 | 9 | 117,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of n candies and a pile consisting of m stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat a candies and b stones. Then Mike awards Gerald f(a, b) prize points. Gerald during his move either eats a candy from the pile of candies or a stone from the pile of stones. As Mike sees that Gerald has eaten everything apart one candy and one stone, he awards points for the last time and the game ends. Gerald is not allowed to eat all the candies, and he is not allowed to eat all the stones too. Tell Gerald how to play to get the largest possible number of points: it is required to find one of the possible optimal playing strategies for Gerald.
Input
The first line contains three integers n, m, p (1 ≤ n, m ≤ 20000, 1 ≤ p ≤ 109). The second line contains n integers x0, x1, ..., xn - 1 (0 ≤ xi ≤ 20000). The third line contains m integers y0, y1, ..., ym - 1 (0 ≤ yi ≤ 20000). The value of f(a, b) is calculated as a remainder of the division of the sum xa + yb by number p.
Output
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of n + m - 2 characters, each of which is either a "C" or "S", the i-th character should be "C" if Gerald's i-th move should be eating a candy and "S" if he should eat a stone.
Examples
Input
2 2 10
0 0
0 1
Output
2
SC
Input
3 3 10
0 2 0
0 0 2
Output
10
CSSC
Input
3 3 2
0 1 1
1 1 0
Output
4
SCSC
Note
In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first eat a stone, then a candy.
Submitted Solution:
```
n, m, p = list(map(int, input().split()))
cands = list(map(int, input().split()))
stones = list(map(int, input().split()))
possbs = [[0] * n for _ in range(m)]
path = ''
for i in range(n): #candy
for j in range(m): #stone
if i == 0 and j == 0: possbs[j][i] = (cands[i] + stones[j])%p
elif i == 0: possbs[j][i] = possbs[j-1][i] + (stones[j])%p
elif j == 0: possbs[j][i] = possbs[j][i-1] + (cands[i])%p
else: possbs[j][i] = max(possbs[j][i-1], possbs[j-1][i]) + (cands[i] + stones[j])%p
m -= 1; n -= 1
print(possbs[m][n])
while n != 0 and m != 0:
if possbs[m][n] - (cands[n] + stones[m])%p == possbs[m][n-1]: path = 'C' + path; n -= 1
else: path = 'S' + path; m -= 1
if n == 0:
path = 'S'*m + path
else:
path = 'C'*n + path
print(path)
``` | instruction | 0 | 58,829 | 9 | 117,658 |
No | output | 1 | 58,829 | 9 | 117,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of n candies and a pile consisting of m stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat a candies and b stones. Then Mike awards Gerald f(a, b) prize points. Gerald during his move either eats a candy from the pile of candies or a stone from the pile of stones. As Mike sees that Gerald has eaten everything apart one candy and one stone, he awards points for the last time and the game ends. Gerald is not allowed to eat all the candies, and he is not allowed to eat all the stones too. Tell Gerald how to play to get the largest possible number of points: it is required to find one of the possible optimal playing strategies for Gerald.
Input
The first line contains three integers n, m, p (1 ≤ n, m ≤ 20000, 1 ≤ p ≤ 109). The second line contains n integers x0, x1, ..., xn - 1 (0 ≤ xi ≤ 20000). The third line contains m integers y0, y1, ..., ym - 1 (0 ≤ yi ≤ 20000). The value of f(a, b) is calculated as a remainder of the division of the sum xa + yb by number p.
Output
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of n + m - 2 characters, each of which is either a "C" or "S", the i-th character should be "C" if Gerald's i-th move should be eating a candy and "S" if he should eat a stone.
Examples
Input
2 2 10
0 0
0 1
Output
2
SC
Input
3 3 10
0 2 0
0 0 2
Output
10
CSSC
Input
3 3 2
0 1 1
1 1 0
Output
4
SCSC
Note
In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first eat a stone, then a candy.
Submitted Solution:
```
n, m, p = list(map(int, input().split()))
cands = list(map(int, input().split()))
stones = list(map(int, input().split()))
possbs = [[0] * n] * m
path = ''
for i in range(n): #candy
for j in range(m): #stone
if i == 0 and j == 0: possbs[j][i] = cands[i] + stones[j]
elif i == 0: possbs[j][i] = possbs[j][i-1] + stones[j]
elif j == 0: possbs[j][i] = possbs[j-1][i] + cands[i]
else: possbs[j][i] = max(possbs[j][i-1], possbs[j-1][i]) + cands[i] + stones[j]
m -= 1; n -= 1
print(possbs[m][n])
while n > 0 and m > 0:
if possbs[m][n] - cands[n] - stones[m] == possbs[j][i-1]: path = 'C' + path
else: path = 'S' + path
m -= 1; n -= 1
print(path)
``` | instruction | 0 | 58,830 | 9 | 117,660 |
No | output | 1 | 58,830 | 9 | 117,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of n candies and a pile consisting of m stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat a candies and b stones. Then Mike awards Gerald f(a, b) prize points. Gerald during his move either eats a candy from the pile of candies or a stone from the pile of stones. As Mike sees that Gerald has eaten everything apart one candy and one stone, he awards points for the last time and the game ends. Gerald is not allowed to eat all the candies, and he is not allowed to eat all the stones too. Tell Gerald how to play to get the largest possible number of points: it is required to find one of the possible optimal playing strategies for Gerald.
Input
The first line contains three integers n, m, p (1 ≤ n, m ≤ 20000, 1 ≤ p ≤ 109). The second line contains n integers x0, x1, ..., xn - 1 (0 ≤ xi ≤ 20000). The third line contains m integers y0, y1, ..., ym - 1 (0 ≤ yi ≤ 20000). The value of f(a, b) is calculated as a remainder of the division of the sum xa + yb by number p.
Output
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of n + m - 2 characters, each of which is either a "C" or "S", the i-th character should be "C" if Gerald's i-th move should be eating a candy and "S" if he should eat a stone.
Examples
Input
2 2 10
0 0
0 1
Output
2
SC
Input
3 3 10
0 2 0
0 0 2
Output
10
CSSC
Input
3 3 2
0 1 1
1 1 0
Output
4
SCSC
Note
In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first eat a stone, then a candy.
Submitted Solution:
```
n, m, p = list(map(int, input().split()))
cands = list(map(int, input().split()))
stones = list(map(int, input().split()))
possbs = [[0] * n] * m
path = ''
for i in range(n): #candy
for j in range(m): #stone
if i == 0 and j == 0: possbs[j][i] = cands[i] + stones[j]
elif i == 0: possbs[j][i] = possbs[j][i-1] + stones[j]
elif j == 0: possbs[j][i] = possbs[j-1][i] + cands[i]
else: possbs[j][i] = max(possbs[j][i-1], possbs[j-1][i]) + cands[i] + stones[j]
m -= 1; n -= 1
print(possbs[m][n])
while n != 0 and m != 0:
if possbs[m][n] - cands[n] - stones[m] == possbs[m][n-1]: path = 'C' + path; n -= 1
else: path = 'S' + path; m -= 1
if n == 0:
path = 'S'*m + path
else:
path = 'C'*n + path
print(path)
``` | instruction | 0 | 58,831 | 9 | 117,662 |
No | output | 1 | 58,831 | 9 | 117,663 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,946 | 9 | 117,892 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
ids = []
for i in range(n):
if a[i] == 1:
ids.append(i)
m = len(ids)
sum = m
if sum == 1:
print(-1)
exit()
def calculate(inc):
ret = 0
for i in range(0,m,inc):
mid = (i + (i + inc - 1)) // 2
for j in range(i,i+inc):
ret += abs(ids[j] - ids[mid])
return ret
ans = 10 ** 18
div = 2
while div <= sum:
if sum % div == 0:
get = calculate(div)
ans = min(ans, get)
div += 1
ans = min(ans, calculate(m))
print(ans)
``` | output | 1 | 58,946 | 9 | 117,893 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,947 | 9 | 117,894 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
m = 0
b = []
for i in range(n):
m += a[i]
if a[i] == 1:
b.append(i)
ans = n * m
for i in range(2, m + 1):
preans = 0
if m % i != 0:
continue
c = 0
for j in range(m):
wg = (j // i) * i + i // 2
if j != wg:
preans += abs(b[wg] - b[j])
ans = min(ans, preans)
if m == 1:
ans = -1
print(ans)
``` | output | 1 | 58,947 | 9 | 117,895 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,948 | 9 | 117,896 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
# ------------------------------
# f = open('./input.txt')
# sys.stdin = f
def main():
n = N()
arr = RLL()
sm = sum(arr)
up = ceil(sqrt(sm))
arro = []
for i in range(n):
if arr[i]==1:
arro.append(i)
def c(num):
if num==1: return INF
gp = sm//num
mid = num//2
res = 0
for i in range(gp):
for j in range(num*i, num*i+num):
res+=abs(arro[mid]-arro[j])
mid+=num
return res
res = c(sm)
for k in range(2, up+1):
if sm%k==0:
res = min(res, c(k), c(sm//k))
print(res if res!=INF else -1)
if __name__ == "__main__":
main()
``` | output | 1 | 58,948 | 9 | 117,897 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,949 | 9 | 117,898 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
import bisect
import sys
input = sys.stdin.readline
def make_prime_factors(n):
"""自然数nの素因数を列挙したリストを出力する
計算量: O(sqrt(N))
入出力例: 156 -> [2, 2, 3, 13]
"""
prime_factors = []
for k in range(2, int(n**0.5) + 1):
while n % k == 0:
prime_factors.append(k)
n = n // k
if n != 1:
prime_factors.append(n)
return prime_factors
n = int(input())
a = list(map(int, input().split()))
ruiseki = [0] * (n+1)
for i in range(n):
ruiseki[i+1] = ruiseki[i] + a[i]
sum_a = 0
for i in range(n):
sum_a += a[i]
li = make_prime_factors(sum_a)
def count(begin, end, num):
#print(ruiseki[begin+1:end+1])
#print(a[begin:end])
res = 0
l = bisect.bisect_left(ruiseki[begin+1:end+1], (ruiseki[begin+1]+ruiseki[end]+1)//2)
for i, num2 in enumerate(a[begin:end]):
if num2 == 1:
res += abs(i - l)
return res
if sum_a == 1:
print(-1)
exit()
ans = 10**18
for num in li:
tmp_ans = 0
cnt = 0
begin = 0
end = 0
while True:
if end == n:
break
cnt += a[end]
if cnt == num:
tmp_ans += count(begin, end + 1, num)
begin = end + 1
end = begin
cnt = 0
else:
end += 1
ans = min(tmp_ans, ans)
print(ans)
``` | output | 1 | 58,949 | 9 | 117,899 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,950 | 9 | 117,900 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
import itertools as it
import os
import sys
def calc_cost(cumsum, f):
return sum(map(lambda x: min(x % f, f - x % f), cumsum))
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i == 0:
factors.append(i)
while n % i == 0:
n //= i
else:
i += 1
if n > 1:
factors.append(n)
return factors
def f(xs):
cumsum = list(it.accumulate(xs))
s = cumsum[-1]
if s == 1:
return -1
return min(calc_cost(cumsum, f) for f in prime_factors(s))
def pp(input):
input()
xs = map(int, input().split())
print(f(xs))
if "paalto" in os.getcwd():
from string_source import string_source
pp(
string_source(
"""10
3 3 3 5 6 9 3 1 7 3"""
)
)
pp(
string_source(
"""5
3 10 2 1 5"""
)
)
s1 = string_source(
"""3
4 8 5"""
)
pp(s1)
pp(
string_source(
"""4
0 5 15 10"""
)
)
pp(
string_source(
"""1
1"""
)
)
else:
pp(sys.stdin.readline)
``` | output | 1 | 58,950 | 9 | 117,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,951 | 9 | 117,902 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
import math
n = int(input())
arr = [int(z) for z in input().split()]
def smallestfactor(n):
f = []
for i in range(1, int(math.sqrt(n)) + 2):
if not n % i:
if i > 1:
f.append(i)
if n//i > 1:
f.append(n//i)
if not len(f):
return -1
return f
def shortestroute(arr, mid):
o = 0
for i in range(len(arr)):
o += abs(arr[i] - arr[mid])
return o
s = sum(arr)
factors = smallestfactor(s)
if factors == -1:
print(-1)
exit()
res = 0
rn = 0
adj = []
boxes = []
for i in range(n):
if arr[i]:
rn += 1
boxes.append(i)
res = 10**18
for f in factors:
r = 0
for group in range(s//f):
adj = boxes[group*f:(group+1)*f]
if f % 2:
mid = f//2
r += shortestroute(adj, mid)
else:
mid1 = f//2
mid2 = f//2 - 1
r += min(shortestroute(adj, mid1), shortestroute(adj, mid2))
res = min(res, r)
print(res)
``` | output | 1 | 58,951 | 9 | 117,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,952 | 9 | 117,904 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
n = int(input())
l = list(map(int,input().split()))
s,pref = 0,[]
for i in range(n):
s+=l[i]
if l[i]:
pref.append(i)
if s==1:
print(-1)
exit()
divs = [s]
for i in range(2,int(s**0.5)+1):
if s%i==0:
divs.extend([i,s//i])
ans,value = 10**18,-3
for i in divs:
cnt=0
for j in range(0,s,i):
mid = (i+2*j-1)//2
for k in range(j,j+i):
cnt+=abs(pref[k]-pref[mid])
ans = min(ans,cnt)
print(ans)
``` | output | 1 | 58,952 | 9 | 117,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1 | instruction | 0 | 58,953 | 9 | 117,906 |
Tags: constructive algorithms, greedy, math, number theory, ternary search, two pointers
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
if sum(a) == 1:
print(-1)
exit()
sm = sum(a)
nmax = 10**5+10
eratos = [0 for i in range(nmax+1)]
prime = []
cnt = 2
while True:
while cnt <= nmax and eratos[cnt]:
cnt += 1
if cnt > nmax:
break
eratos[cnt] = 1
prime.append(cnt)
for i in range(cnt**2,nmax+1,cnt):
eratos[i] = 1
dvls = []
for i in prime:
if sm%i == 0:
dvls.append(i)
ansls = []
ls = []
cnti = 0
for dv in dvls:
ans = 0
if dv == 2:
for i in range(n):
if a[i]:
cnti += 1
if cnti%2:
pivot = i
else:
ans += i-pivot
else:
for i in range(n):
if a[i]:
cnti += 1
if 1 <= cnti%dv <= dv//2:
ls.append(i)
elif cnti%dv == dv//2+1:
pivot = i
for j in ls:
ans += pivot-j
ls.clear()
else:
ans += i-pivot
ansls.append(ans)
print(min(ansls))
``` | output | 1 | 58,953 | 9 | 117,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
def getAllFactors(x): #returns a sorted list of all the factors or divisors of x (including 1 and x)
factors=[]
for i in range(1,int(x**0.5)+1):
if x%i==0:
factors.append(i)
if x//i!=i:
factors.append(x//i)
return sorted(factors)
def main():
n=int(input())
a=readIntArr()
# observation: impossible if sum of a is 1 or zero
total=sum(a)
if total<=1:
print(-1)
return
ans=inf
# try all non-1 factors
for factor in getAllFactors(total)[1:]:
# keep forming groups the size of divisor, and move to median
tempans=0
cnt=0
indexes=[]
for i,x in enumerate(a):
if x==1:
indexes.append(i)
cnt+=1
if cnt==factor:
medianIndex=indexes[factor//2]
for j in indexes:
tempans+=abs(j-medianIndex)
indexes=[]
cnt=0
ans=min(ans,tempans)
print(ans)
return
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
# input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultVal,dimensionArr): # eg. makeArr(0,[n,m])
dv=defaultVal;da=dimensionArr
if len(da)==1:return [dv for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(x,y):
print('? {} {}'.format(x,y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print('! {}'.format(ans))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
for _abc in range(1):
main()
``` | instruction | 0 | 58,954 | 9 | 117,908 |
Yes | output | 1 | 58,954 | 9 | 117,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
def count(x):
ans = 0
for i in range(0, m, x):
st = (2 * i + x - 1) // 2
for j in range(i, x + i):
ans += abs(a[j] - a[st])
return ans
n = int(input())
data = list(map(int, input().split()))
a = []
m = 0
for i in range(n):
if data[i] == 1:
a.append(i)
m += 1
k = []
for i in range(2, m + 1):
if m % i == 0:
k.append(i)
l, r = 0, len(k) - 1
while r - l > 7:
m1 = l + (r - l) // 3
m2 = r - (r - l) // 3
if count(k[m1]) > count(k[m2]):
l = m1
else:
r = m2
t = 10 ** 18
for i in range(l, r + 1):
t = min(t, count(k[i]))
if t == 10 ** 18:
print(-1)
else:
print(t)
``` | instruction | 0 | 58,955 | 9 | 117,910 |
Yes | output | 1 | 58,955 | 9 | 117,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
n = int(input())
a = list(map(int,input().split()))
su = sum(a)
if su == 1:
return -1
fac = []
if not su%2:
fac.append(2)
while not su%2:
su //= 2
for i in range(3,int(su**0.5)+1,2):
if not su%i:
fac.append(i)
while not su%i:
su //= i
if su != 1:
fac.append(su)
ans = 10**20
for i in fac:
ans1,car = 0,0
for j in range(n-1):
if a[j]+car < 0:
x = abs(a[j]+car)
ans1 += x
car = -x
continue
be = (a[j]+car)%i
ab = i-be
if ab < be:
car = -ab
ans1 += ab
else:
car = be
ans1 += be
if a[-1]+car < 0:
ans1 += abs(a[-1]+car)
ans = min(ans,ans1)
return ans
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
print(main())
``` | instruction | 0 | 58,956 | 9 | 117,912 |
Yes | output | 1 | 58,956 | 9 | 117,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
# ------------------------------
# f = open('./input.txt')
# sys.stdin = f
def main():
n = N()
arr = RLL()
sm = sum(arr)
up = ceil(sqrt(sm))
arro = []
for i in range(n):
if arr[i]==1:
arro.append(i)
def c(num):
gp = sm//num
mid = num//2
res = 0
for i in range(gp):
for j in range(num*i, num*i+num):
res+=abs(arro[mid]-arro[j])
mid+=num
return res
res = c(sm) if sm!=1 else INF
for k in range(2, up+1):
if sm%k==0 and k!=sm:
res = min(res, c(k), c(sm//k))
print(res if res!=INF else -1)
if __name__ == "__main__":
main()
``` | instruction | 0 | 58,957 | 9 | 117,914 |
Yes | output | 1 | 58,957 | 9 | 117,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
n = int(input())
l = list(map(int,input().split()))
s = sum(l)
vals = [s]
for i in range(2,int(s**0.5)+1):
if s%i==0:
vals.append(i)
break
if sum(vals)==1:
print(-1)
exit()
ans = 10**6
for coup in vals:
mid = coup//2 + coup%2
cost,curr = 0,0
for i in range(n):
if curr:
cost += curr if curr<mid else (coup-curr)
if curr==coup:
curr=0
curr+=l[i]
ans = min(cost,ans)
print(ans)
``` | instruction | 0 | 58,958 | 9 | 117,916 |
No | output | 1 | 58,958 | 9 | 117,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
def prime(x):
y=2
while(y*y<=x):
if(x%y==0):
return y
y+=1
return x
n=int(input())
a=list(map(int,input().split()))
count=0
b=[0]
for i in range(n):
if(a[i]==1):
b.append(i+1)
count+=1
jump=0
i,j,k,add,inc,dj=0,0,0,0,0,0
if(count==1):
print(-1)
else:
jump=prime(count)
while True:
i=j+1
j+=jump
dj=j
k=i+jump//2
inc=1
if(i==count+1):
break
while inc<=jump//2:
add+=(abs(b[i]-b[k])+abs(b[dj]-b[k]))*inc
i+=1
dj-=1
inc+=1
print(add)
``` | instruction | 0 | 58,959 | 9 | 117,918 |
No | output | 1 | 58,959 | 9 | 117,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
# ------------------------------
# f = open('./input.txt')
# sys.stdin = f
def main():
n = N()
arr = RLL()
sm = sum(arr)
up = ceil(sqrt(sm))
def c(num):
rec = 0
tres = res = 0
mid = 0
for i in range(n):
if arr[i]==1:
rec += 1
if rec==num//2:
mid = i
if rec<num//2:
tres-=i
else:
tres+=i
if rec==num:
res+=tres
tres = 0
rec = num
res+=tres-mid*num
return res
res = INF
for k in range(2, up+1):
if sm%k==0:
res = min(res, c(k))
print(res if res!=INF else -1)
if __name__ == "__main__":
main()
``` | instruction | 0 | 58,960 | 9 | 117,920 |
No | output | 1 | 58,960 | 9 | 117,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems.
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.
Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, …, a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes.
Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of chocolate boxes.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1) — the number of chocolate pieces in the i-th box.
It is guaranteed that at least one of a_1, a_2, …, a_n is positive.
Output
If there is no way for Charlie to make Alice happy, print -1.
Otherwise, print a single integer x — the minimum number of seconds for Charlie to help Bob make Alice happy.
Examples
Input
3
1 0 1
Output
2
Input
1
1
Output
-1
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
m = 0
b = []
for i in range(n):
m += a[i]
if a[i] == 1:
b.append(i)
ans = n * m
for i in range(2, m + 1):
preans = 0
if m % i != 0:
continue
c = 0
for j in range(m):
wg = (j // m) * m + m // 2
if j != wg:
preans += abs(b[wg] - b[j])
ans = min(ans, preans)
if m == 1:
ans = -1
print(ans)
``` | instruction | 0 | 58,961 | 9 | 117,922 |
No | output | 1 | 58,961 | 9 | 117,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,233 | 9 | 118,466 |
Tags: brute force, greedy, math
Correct Solution:
```
import sys
f = sys.stdin
C, Hr, Hb, Wr, Wb = map(int, f.readline().strip().split())
if Hr/Wr < Hb/Wb:
Hr, Hb, Wr, Wb = Hb, Hr, Wb, Wr
if (C % Wr) == 0 and (C // Wr) > 0:
print((C // Wr)*Hr)
elif (C // Wr) == 0:
print((C // Wb)*Hb)
else:
nmax = (C // Wr)
pmax = nmax*Hr + ((C - nmax*Wr) // Wb) * Hb
dmax = ((C - (nmax-0)*Wr) % Wb)
#print(0, pmax, dmax)
#
#pm1 = (nmax-1)*Hr + ((C - (nmax-1)*Wr) // Wb) * Hb
#if pm1>pmax:
# pmax = pm1
if Hr/Wr > Hb/Wb:
dx = dmax * (Hb/Wb) / (Hr/Wr - Hb/Wb)
elif Hr/Wr < Hb/Wb:
dx = 0
else:
dx = Wb * Wr
if Wr<Wb:
nmax = (C // Wb)
pmax = nmax*Hb + ((C - nmax*Wb) // Wr) * Hr
if Wr>Wb:
nmax = (C // Wr)
pmax = nmax*Hr + ((C - nmax*Wr) // Wb) * Hb
if Wr>Wb and dx>0:
for k in range(1, C//Wr):
if k*Wr > dx:
break
pk = (nmax-k)*Hr + ((C - (nmax-k)*Wr) // Wb) * Hb
dk = ((C - (nmax-k)*Wr) % Wb)
#print(k, pmax, pk, dk)
if pk>pmax:
pmax = pk
if dk==0 :
break
elif Wr<Wb and dx>0:
for j in range(1, C//Wb+1):
k = nmax - (C-j*Wb)//Wr
if k*Wr > dx:
break
pk = (nmax-k)*Hr + ((C - (nmax-k)*Wr) // Wb) * Hb
dk = ((C - (nmax-k)*Wr) % Wb)
#print(j, k, pmax, pk, dk, (nmax-k), ((C - (nmax-k)*Wr) // Wb) )
if pk>pmax:
pmax = pk
#dmax = dk
if dk==0 :
break
# elif Wr<Wb and dx>0:
# for j in range(1, C//Wb+1):
# k = (j*Wb - dmax)//Wr
# if k*Wr > dx:
# break
# pk = (nmax-k)*Hr + ((C - (nmax-k)*Wr) // Wb) * Hb
# dk = ((C - (nmax-k)*Wr) % Wb)
# print(j, k, pmax, pk, dk, (nmax-k), ((C - (nmax-k)*Wr) // Wb) )
# if pk>pmax:
# pmax = pk
# #dmax = dk
# if dk==0 :
# break
print(pmax)
``` | output | 1 | 59,233 | 9 | 118,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,234 | 9 | 118,468 |
Tags: brute force, greedy, math
Correct Solution:
```
C, Hr, Hb, Wr, Wb = map(int, input().split())
ans = 0
for i in range(10 ** 5):
if Wr * i <= C:
ans = max(ans, Hr * i + (C - Wr * i) // Wb * Hb)
for i in range(10 ** 5):
if Wb * i <= C:
ans = max(ans, Hb * i + (C - Wb * i) // Wr * Hr)
print(ans)
``` | output | 1 | 59,234 | 9 | 118,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,235 | 9 | 118,470 |
Tags: brute force, greedy, math
Correct Solution:
```
m, h1, h2, w1, w2 = map(int, input().split())
if h2 / w2 > h1 / w1:
h1, h2 = h2, h1
w1, w2 = w2, w1
if w1 ** 2 >= m:
res = 0
for i in range(int(m ** 0.5 + 1)):
if i * w1 <= m:
new = i * h1 + (m - w1 * i) // w2 * h2
res = max(res, new)
print(res)
exit()
res = 0
for i in range(int(m ** 0.5 + 5)):
new_res = i * h2 + ((m - i * w2) // w1) * h1
res = max(res, new_res)
print(res)
``` | output | 1 | 59,235 | 9 | 118,471 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,236 | 9 | 118,472 |
Tags: brute force, greedy, math
Correct Solution:
```
import math
c, hr, hb, wr, wb = map(int, input().split())
s = int(math.sqrt(c))
ans = 0
for i in range(s):
if c >= i * wr:
ans = max(ans, hr * i + (c - i * wr) // wb * hb)
if c >= i * wb:
ans = max(ans, hb * i + (c - i * wb) // wr * hr)
print(ans)
``` | output | 1 | 59,236 | 9 | 118,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,237 | 9 | 118,474 |
Tags: brute force, greedy, math
Correct Solution:
```
C, Hr, Hb, Wr, Wb = map(int, input().split())
ans = 0
for i in range(10 ** 5):
if Wr * i <= C:
ans = max(ans, Hr * i + (C - Wr * i) // Wb * Hb)
for i in range(10 ** 5):
if Wb * i <= C:
ans = max(ans, Hb * i + (C - Wb * i) // Wr * Hr)
print(ans)
# Made By Mostafa_Khaled
``` | output | 1 | 59,237 | 9 | 118,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,238 | 9 | 118,476 |
Tags: brute force, greedy, math
Correct Solution:
```
C, Hr, Hb, Wr, Wb = map(int, input().split())
ans = 0
for i in range(5 * 10 ** 5):
if Wr * i <= C:
ans = max(ans, Hr * i + (C - Wr * i) // Wb * Hb)
for i in range(5 * 10 ** 5):
if Wb * i <= C:
ans = max(ans, Hb * i + (C - Wb * i) // Wr * Hr)
print(ans)
``` | output | 1 | 59,238 | 9 | 118,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,239 | 9 | 118,478 |
Tags: brute force, greedy, math
Correct Solution:
```
c, hr, hb, wr, wb = map(int, input().split())
s = 0
if wb < wr: hr, hb, wr, wb = hb, hr, wb, wr
if wb * wb > c:
for nb in range(c // wb + 1):
s = max(s, nb * hb + hr * ((c - wb * nb) // wr))
else:
if hr * wb < hb * wr: hr, hb, wr, wb = hb, hr, wb, wr
for nb in range(min(31625, c // wb + 1)):
s = max(s, nb * hb + hr * ((c - wb * nb) // wr))
print(s)
``` | output | 1 | 59,239 | 9 | 118,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | instruction | 0 | 59,240 | 9 | 118,480 |
Tags: brute force, greedy, math
Correct Solution:
```
C, Hr, Hb, Wr, Wb = map(int, input().split())
ans = 0
for i in range(3 * 10 ** 5):
if Wr * i <= C:
ans = max(ans, Hr * i + (C - Wr * i) // Wb * Hb)
for i in range(3 * 10 ** 5):
if Wb * i <= C:
ans = max(ans, Hb * i + (C - Wb * i) // Wr * Hr)
print(ans)
``` | output | 1 | 59,240 | 9 | 118,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
C, Pr, Pb, Wr, Wb = list(map(int, input().split()))
result = 0
if Wr * Wr >= C:
i = 0
while Wr * i <= C:
j = int((C - Wr * i) / Wb)
result = max(result, Pr * i + Pb * j)
i += 1
print(result)
exit(0)
if Wb * Wb >= C:
i = 0
while Wb * i <= C:
j = int((C - Wb * i) / Wr)
result = max(result, Pb * i + Pr * j)
i += 1
print(result)
exit(0)
Ab = Pb / Wb
Ar = Pr / Wr
if Ab < Ar:
i = 0
while i * i <= C:
j = int((C - Wb * i) / Wr)
result = max(result, Pb * i + Pr * j)
i += 1
print(result)
else:
i = 0
while i * i <= C:
j = int((C - Wr * i) / Wb)
result = max(result, Pr * i + Pb * j)
i += 1
print(result)
``` | instruction | 0 | 59,241 | 9 | 118,482 |
Yes | output | 1 | 59,241 | 9 | 118,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
def main():
c, hr, hb, wr, wb = map(int, input().split())
ans = 0
for i in range(10**6 + 1):
if i * wr <= c:
ans = max(ans, i * hr + ((c - i * wr) // wb) * hb)
if i * wb <= c:
ans = max(ans, i * hb + ((c - i * wb) // wr) * hr)
print(ans)
main()
``` | instruction | 0 | 59,242 | 9 | 118,484 |
Yes | output | 1 | 59,242 | 9 | 118,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
import math
def solve(c,hr,hb,wr,wb):
INF = math.ceil(math.sqrt(c))
if wb > wr:
wb,wr = wr,wb
hb,hr = hr,hb
if wr > INF:
ans = 0
i = 0
while i * wr <= c and i < INF:
ans = max(ans,i * hr + ((c - i * wr) // wb) * hb)
i+=1
return ans
else:
if wr * hb > hr * wb:
wb,wr = wr,wb
hb,hr = hr,hb
ans = 0
j = 0
while j * wb <= c and j <= wr:
ans = max(ans,j * hb + ((c - j * wb) // wr) * hr)
j+=1
return ans
c,hr,hb,wr,wb = list(map(int,input().split()))
print(solve(c,hr,hb,wr,wb))
``` | instruction | 0 | 59,243 | 9 | 118,486 |
Yes | output | 1 | 59,243 | 9 | 118,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
__author__ = 'trunghieu11'
def calc(n, h1, h2, w1, w2):
answer = 0
len = n // w1
for i in range(0, min(len, 100000) + 1):
answer = max(answer, i * h1 + (n - i * w1) // w2 * h2)
return answer
n, hR, hB, wR, wB = map(int, input().split())
print(max(calc(n, hR, hB, wR, wB), calc(n, hB, hR, wB, wR)))
``` | instruction | 0 | 59,244 | 9 | 118,488 |
Yes | output | 1 | 59,244 | 9 | 118,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
a = input().split()
canEat = int(a[0])
maxH = int(a[1])
minH = int(a[2])
maxW = int(a[3])
minW = int(a[4])
if maxH<minH:
#happieness a<->b
maxH += minH
minH = maxH-minH
maxH = maxH-minH
#weight a<->b
maxW += minW
minW = maxW-minW
maxW = maxW-minW
elif maxH==minH:
maxW=canEat+1
sx = canEat//maxW
if sx==0:
print((canEat//minW)*minH)
else:
x = sx
while (canEat-x*maxW<minW)&(x>0):
x-=1
if (x<sx)&(x>0):
print(x*maxH+((canEat-x*maxW)//minW)*minH)
else:
print(sx*maxH)
``` | instruction | 0 | 59,245 | 9 | 118,490 |
No | output | 1 | 59,245 | 9 | 118,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
def solvelinear(c,hr,hb,wr,wb):
ans = 0
if hb/hr == wb/wr:
for i in reversed(range(c+1)):
isint = False
for x in range(c//wr+1):
y = (i-wr*x)/wb
if int(y) == y:
isint = True
break
if isint == True:
ans = i
break
ans *= hb/wb
elif hb/hr < wb/wr:
for i in reversed(range(c+1)):
for x in reversed(range(c//wr+1)):
y = (i-wr*x)/wb
if y-int(y) < 0.01:
ans = max(ans, hr*x+hb*y)
break
if hb*(i-1) < wb*(ans):
break
else:
for i in reversed(range(c+1)):
for y in reversed(range(c//wb+1)):
x = (i-wb*y)/wr
if x-int(x) < 0.01:
ans = max(ans, hr*x+hb*y)
break
if hr*(i-1) < wr*(ans):
break
print(int(ans))
(c,hr,hb,wr,wb) = map(int, input().split())
solvelinear(c,hr,hb,wr,wb)
``` | instruction | 0 | 59,246 | 9 | 118,492 |
No | output | 1 | 59,246 | 9 | 118,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
m, h1, h2, w1, w2 = map(int, input().split())
if h2 / w2 > h1 / w1:
h1, h2 = h2, h1
w1, w2 = w2, w1
res = 0
for i in range(int(m ** 0.5 + 1)):
new_res = i * h2 + ((m - i * w2) // w1) * h1
res = max(res, new_res)
print(res)
``` | instruction | 0 | 59,247 | 9 | 118,494 |
No | output | 1 | 59,247 | 9 | 118,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
<image>
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
Input
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Output
Print a single integer — the maximum number of joy units that Om Nom can get.
Examples
Input
10 3 5 2 3
Output
16
Note
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Submitted Solution:
```
import fileinput
from collections import defaultdict
def gcd(a, b):
return gcd(b, a % b) if b != 0 else a
def lcm(a, b):
return (a * b) / gcd(a, b)
def solve(C, Ha, Hb, Wa, Wb):
if Ha > Hb:
Ha, Hb = Hb, Ha
Wa, Wb = Wb, Wa
equal = lcm(Wa, Wb)
#print(equal)
eqval = max(Ha * int(equal/Wa), Hb * int(equal/Wb))
#print(eqval)
C_eqval = int(C / equal) * eqval
C = (C % equal)
maxval = 0
branch_and_bound = defaultdict(lambda: 0)
for a in range(int(C/Wa) + 1):
for b in range(int((C - a*Wa)/Wb) + 1):
if a * Wa + b * Wb > C:
# print("naebalsya")
break
stored_val = branch_and_bound[a * Wa + b * Wb]
current_val = a * Ha + b * Hb
if stored_val > current_val:
# print(a, b, a * Wa + b * Wb, "Bound")
break
else:
# print(a, b, a * Wa + b * Wb, "Update")
branch_and_bound[a * Wa + b * Wb] = current_val
maxval = max(maxval, current_val)
return int(maxval + C_eqval)
if __name__ == '__main__':
data = list(iter(fileinput.input()))
C, Ha, Hb, Wa, Wb = map(int, data[0].split())
print(solve(C, Ha, Hb, Wa, Wb))
``` | instruction | 0 | 59,248 | 9 | 118,496 |
No | output | 1 | 59,248 | 9 | 118,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,299 | 9 | 118,598 |
Tags: binary search, brute force, implementation
Correct Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
e=[b[i]//a[i] for i in range(n)]
d=[b[i]%a[i] for i in range(n)]
while k:
i=e.index(min(e))
mi=min(k,a[i]-d[i])
d[i]+=mi
k-=mi
e[i]+=d[i]//a[i]
d[i]%=a[i]
print(min(e))
``` | output | 1 | 59,299 | 9 | 118,599 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,300 | 9 | 118,600 |
Tags: binary search, brute force, implementation
Correct Solution:
```
n,k = [int(x) for x in input().split(" ")]
a = [int(x) for x in input().split(" ")]
b = [int(x) for x in input().split(" ")]
l = 0
r = 2*(10e9)
while l<r:
mid = (l+r)//2+1
sm = sum([ max([ a[i]*mid-b[i] ,0]) for i in range(n) ])
if sm>k: r = mid-1
else: l = mid
print(int(l))
``` | output | 1 | 59,300 | 9 | 118,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,301 | 9 | 118,602 |
Tags: binary search, brute force, implementation
Correct Solution:
```
def minimum(l):
min_value = l[0]
min_index = 0
for i in range(1, len(l)):
if l[i] < min_value:
min_value = l[i]
min_index = i
return min_value, min_index
n, k = [int(s) for s in input().split()]
needs = [int(s) for s in input().split()]
has = [int(s) for s in input().split()]
can_bake = [int(has[i] / needs[i]) for i in range(n)]
# print(can_bake)
while k > 0:
min_value, min_index = minimum(can_bake)
has[min_index] += 1
k -= 1
can_bake[min_index] = int(has[min_index] / needs[min_index])
print(min(can_bake))
``` | output | 1 | 59,301 | 9 | 118,603 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,302 | 9 | 118,604 |
Tags: binary search, brute force, implementation
Correct Solution:
```
#===========Template===============
from io import BytesIO, IOBase
from math import sqrt
import sys,os
from os import path
inpl=lambda:list(map(int,input().split()))
inpm=lambda:map(int,input().split())
inpi=lambda:int(input())
inp=lambda:input()
rev,ra,l=reversed,range,len
P=print
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
def B(n):
return bin(n).replace("0b","")
def factors(n):
arr=[]
for i in ra(2,int(sqrt(n))+1):
if n%i==0:
arr.append(i)
if i*i!=n:
arr.append(int(n/i))
return arr
def dfs(arr,n):
pairs=[[i+1] for i in ra(n)]
vis=[0]*(n+1)
for i in ra(l(arr)):
pairs[arr[i][0]-1].append(arr[i][1])
pairs[arr[i][1]-1].append(arr[i][0])
comp=[]
for i in ra(n):
stack=[pairs[i]]
temp=[]
if vis[stack[-1][0]]==0:
while len(stack)>0:
if vis[stack[-1][0]]==0:
temp.append(stack[-1][0])
vis[stack[-1][0]]=1
s=stack.pop()
for j in s[1:]:
if vis[j]==0:
stack.append(pairs[j-1])
else:
stack.pop()
comp.append(temp)
return comp
#=========I/p O/p ========================================#
from bisect import bisect_left as bl
from bisect import bisect_right as br
import sys,operator,math,operator
from collections import Counter
if(path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
import random,heapq
#==============To chaliye shuru krte he ====================#
n,k=inpm()
li1,li2=inpl(),inpl()
heap=[]
for i in ra(n):
heap.append([li2[i]//li1[i],i])
heapq.heapify(heap)
while k>0:
p=heapq.heappop(heap)
a,b=p
if li2[b]%li1[b]==0:
li2[b]+=min(li1[b],k)
k-=min(li1[b],k)
heapq.heappush(heap,[li2[b]//li1[b],b])
else:
rem=li1[b]-(li2[b]%li1[b])
li2[b]+=min(rem,k)
k-=min(rem,k)
heapq.heappush(heap,[li2[b]//li1[b],b])
ans=heapq.heappop(heap)
P(int(ans[0]))
``` | output | 1 | 59,302 | 9 | 118,605 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,303 | 9 | 118,606 |
Tags: binary search, brute force, implementation
Correct Solution:
```
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 0
while True:
need = 0
for i in range(n):
if a[i] > b[i]:
need += abs(b[i]-a[i])
b[i] = 0
else:
b[i] -= a[i]
if need > k:
break
else:
k-= need
c+=1
print(c)
``` | output | 1 | 59,303 | 9 | 118,607 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,304 | 9 | 118,608 |
Tags: binary search, brute force, implementation
Correct Solution:
```
n, k=map(int,input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
ans=0
while True:
for i in range(n):
if b[i]<a[i]:
k-=a[i]-b[i]
b[i]=a[i]
b[i]-=a[i]
if k>=0:
ans+=1
else: break
print(ans)
``` | output | 1 | 59,304 | 9 | 118,609 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,305 | 9 | 118,610 |
Tags: binary search, brute force, implementation
Correct Solution:
```
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
d = {}
backlog = {}
for i in range(n):
if b[i]//a[i] in d:
d[b[i]//a[i]] += (a[i] - (b[i]%a[i]))
backlog[b[i]//a[i]] += a[i]
else:
d[b[i]//a[i]] = a[i] - (b[i]%a[i])
backlog[b[i]//a[i]] = a[i]
special = list(backlog.keys())
special.sort()
prev = [0]
total = 0
for i in range(len(special)):
total += backlog[special[i]]
prev.append(total)
prev.append(0)
start = special[0]
ans = start
for keyz in range(special[0],special[-1]):
if keyz not in d:
d[keyz] = 0
i = special[0]
j = 0
save = 0
while k>0 and j<len(special) and i <= special[-1]:
if d[i] > 0:
if d[i] + prev[j] <= k:
k -= d[i]+prev[j]
save = prev[j+1]
ans += 1
i += 1
j += 1
else:
k = -1
else:
if save <= k:
k -= save
ans += 1
i += 1
else:
k = -1
if k > 0:
ans += k//prev[-2]
print(ans)
``` | output | 1 | 59,305 | 9 | 118,611 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | instruction | 0 | 59,306 | 9 | 118,612 |
Tags: binary search, brute force, implementation
Correct Solution:
```
import math
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
check = math.ceil((sum(b) + k) / sum(a))
x, y = 0, check
#print(y)
while True:
#print(x, y)
if x == y:
break
mid = math.ceil((x + y) / 2)
#print(mid)
c = 0
for i in range(n):
if ((a[i] * mid) - b[i]) > 0:
c += ((a[i] * mid) - b[i])
#print(mid, c)
if c - k > 0:
y = mid - 1
elif c - k < 0:
x = mid
else:
x = mid
break
print(x)
``` | output | 1 | 59,306 | 9 | 118,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
Submitted Solution:
```
from collections import deque
from math import *
from itertools import accumulate
import os
import sys
from io import BytesIO, IOBase
import math
import bisect
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io ------------------
def good(mid,a,b,k):
for i in range(0,len(b)):
if b[i]-a[i]*mid<0:
if (b[i]+k)-a[i]*mid>=0:
diff=a[i]*mid-b[i]
k-=diff
else:
return False
return True
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
l=0
ans=0
r=10**6
while(l<=r):
mid=(l+r)//2
if good(mid,a,b,k)==True:
ans=mid
l=mid+1
else:
r=mid-1
print(ans)
``` | instruction | 0 | 59,307 | 9 | 118,614 |
Yes | output | 1 | 59,307 | 9 | 118,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
Submitted Solution:
```
n, k =input().split();
n=int(n)
k=int(k)
a=list(map(int,input().split()))
b=list(map(int,input().split()))
L = 0;
R = 10**10;
def check(idd) :
summ=0;
for i in range(0,n) :
if (a[i]*idd>b[i]) :
summ-=b[i]-a[i]*idd;
if summ<=k :
return True;
else :
return False;
while R-L !=1 :
mid = (L+R)/2;
mid=int(mid);
#print(mid)
if check(mid)==True :
L=mid;
else :
R=mid;
print(L)
``` | instruction | 0 | 59,308 | 9 | 118,616 |
Yes | output | 1 | 59,308 | 9 | 118,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
Submitted Solution:
```
import sys,math,bisect
sys.setrecursionlimit(10**5)
from random import randint
inf = float('inf')
mod = 10**9+7
"========================================"
def lcm(a,b):
return int((a/math.gcd(a,b))*b)
def gcd(a,b):
return int(math.gcd(a,b))
def tobinary(n):
return bin(n)[2:]
def binarySearch(a,x):
i = bisect.bisect_left(a,x)
if i!=len(a) and a[i]==x:
return i
else:
return -1
def lowerBound(a, x):
i = bisect.bisect_left(a, x)
if i:
return (i-1)
else:
return -1
def upperBound(a,x):
i = bisect.bisect_right(a,x)
if i!= len(a)+1 and a[i-1]==x:
return (i-1)
else:
return -1
def primesInRange(n):
ans = []
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, n+1):
if prime[p]:
ans.append(p)
return ans
def primeFactors(n):
factors = []
while n % 2 == 0:
factors.append(2)
n = n // 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
factors.append(i)
n = n // i
if n > 2:
factors.append(n)
return factors
def isPrime(n,k=5):
if (n <2):
return True
for i in range(0,k):
a = randint(1,n-1)
if(pow(a,n-1,n)!=1):
return False
return True
"========================================="
"""
n = int(input())
n,k = map(int,input().split())
arr = list(map(int,input().split()))
"""
from collections import deque,defaultdict,Counter
from heapq import heappush, heappop,heapify
import string
for _ in range(1):
n,k=map(int,input().split())
need=list(map(int,input().split()))
have=list(map(int,input().split()))
def canMake(need,have,toMake,k):
req = 0
for i in range(len(need)):
if need[i]*toMake <= have[i]:
pass
else:
req+= (need[i]*toMake)-have[i]
if req<=k:
return True
return False
ans = 0
l=1
r=10**9
while l<=r:
mid=(l+r)//2
if canMake(need,have,mid,k):
l=mid+1
ans=max(ans,mid)
else:
r=mid-1
print(ans)
``` | instruction | 0 | 59,309 | 9 | 118,618 |
Yes | output | 1 | 59,309 | 9 | 118,619 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
Submitted Solution:
```
n,k=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
ans=0
while(k>=0):
for i in range(n):
l2[i]=l2[i]-l1[i]
if l2[i]<0:
k=k+l2[i]
l2[i]=0
if k<0:
break
else:
ans+=1
print(ans)
``` | instruction | 0 | 59,310 | 9 | 118,620 |
Yes | output | 1 | 59,310 | 9 | 118,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
Submitted Solution:
```
n,k=map(int,input().strip().split(' '))
need=list(map(int,input().strip().split(' ')))
available=list(map(int,input().strip().split(' ')))
table=[]
for i in range(n):
table.append([available[i]//need[i],need[i]-(available[i]%need[i]),need[i]])
table.sort()
Need=0
least=0
for i in range(n):
if table[i][0]>=least:
if Need==0:
d=table[i][0]-least
else:
d=min(table[i][0]-least,k//Need)
total=d*Need
k-=total
least+=d
if k<Need:
break
else:
total=Need + table[i][1]
if total>k:
break
else:
least+=1
k-=total
Need+=table[i][2]
else:
if table[i][1]>k:
least=min(least,table[i][0])
break
else:
Need+=table[i][2]
k-=table[i][1]
if k<=0:
break
print(least)
``` | instruction | 0 | 59,311 | 9 | 118,622 |
No | output | 1 | 59,311 | 9 | 118,623 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.