message stringlengths 2 39.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 219 108k | cluster float64 11 11 | __index_level_0__ int64 438 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Department of economic development of IT City created a model of city development till year 2100.
To prepare report about growth perspectives it is required to get growth estimates from the model.
To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots.
The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.
Input
The only line of the input contains three integers a, b, c ( - 1000 β€ a, b, c β€ 1000) β the coefficients of ax2 + bx + c = 0 equation.
Output
In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10 - 6.
Examples
Input
1 30 200
Output
-10.000000000000000
-20.000000000000000
Submitted Solution:
```
from sys import stdin
line = stdin.readline().strip().split(" ")
a = int(line[0])
b = int(line[1])
c = int(line[2])
rt = (b * b - 4 * a * c) ** 0.5
denom = 2 * a
print((-b + rt) / denom)
print((-b - rt) / denom)
``` | instruction | 0 | 51,750 | 11 | 103,500 |
No | output | 1 | 51,750 | 11 | 103,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Department of economic development of IT City created a model of city development till year 2100.
To prepare report about growth perspectives it is required to get growth estimates from the model.
To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots.
The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.
Input
The only line of the input contains three integers a, b, c ( - 1000 β€ a, b, c β€ 1000) β the coefficients of ax2 + bx + c = 0 equation.
Output
In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10 - 6.
Examples
Input
1 30 200
Output
-10.000000000000000
-20.000000000000000
Submitted Solution:
```
import math
a, b, c = map(int, input().split(" "))
print((-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a))
print((-b - math.sqrt(b**2 - 4 * a * c)) / (2 * a))
``` | instruction | 0 | 51,751 | 11 | 103,502 |
No | output | 1 | 51,751 | 11 | 103,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Department of economic development of IT City created a model of city development till year 2100.
To prepare report about growth perspectives it is required to get growth estimates from the model.
To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots.
The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.
Input
The only line of the input contains three integers a, b, c ( - 1000 β€ a, b, c β€ 1000) β the coefficients of ax2 + bx + c = 0 equation.
Output
In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10 - 6.
Examples
Input
1 30 200
Output
-10.000000000000000
-20.000000000000000
Submitted Solution:
```
import math
a, b, c = map(int, input().split())
D = b * b - 4 * a * c
D = math.sqrt(D)
root1 = (-b + D) / 2 * a
root2 = (-b - D) / 2 * a
print (max(root1, root2))
print (min(root1, root2))
``` | instruction | 0 | 51,752 | 11 | 103,504 |
No | output | 1 | 51,752 | 11 | 103,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Department of economic development of IT City created a model of city development till year 2100.
To prepare report about growth perspectives it is required to get growth estimates from the model.
To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots.
The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.
Input
The only line of the input contains three integers a, b, c ( - 1000 β€ a, b, c β€ 1000) β the coefficients of ax2 + bx + c = 0 equation.
Output
In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10 - 6.
Examples
Input
1 30 200
Output
-10.000000000000000
-20.000000000000000
Submitted Solution:
```
from decimal import *
s=input().split()
a=int(s[0])
b=int(s[1])
c=int(s[2])
disc=Decimal(b*b-4*a*c)
print(Decimal((0-b+disc.sqrt())/(2*a)))
print(Decimal((0-b-disc.sqrt())/(2*a)))
``` | instruction | 0 | 51,753 | 11 | 103,506 |
No | output | 1 | 51,753 | 11 | 103,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program which reads an integer n and identifies the number of combinations of a, b, c and d (0 β€ a, b, c, d β€ 9) which meet the following equality:
a + b + c + d = n
For example, for n = 35, we have 4 different combinations of (a, b, c, d): (8, 9, 9, 9), (9, 8, 9, 9), (9, 9, 8, 9), and (9, 9, 9, 8).
Input
The input consists of several datasets. Each dataset consists of n (1 β€ n β€ 50) in a line. The number of datasets is less than or equal to 50.
Output
Print the number of combination in a line.
Example
Input
35
1
Output
4
4
Submitted Solution:
```
while True:
try:
n = int(input())
count = 0
for a in range(10):
for b in range(10):
for c in range(10):
for d in range(10):
if a + b + c + d == n:
count += 1
print(count)
except EOFError:
break
``` | instruction | 0 | 52,074 | 11 | 104,148 |
Yes | output | 1 | 52,074 | 11 | 104,149 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program which reads an integer n and identifies the number of combinations of a, b, c and d (0 β€ a, b, c, d β€ 9) which meet the following equality:
a + b + c + d = n
For example, for n = 35, we have 4 different combinations of (a, b, c, d): (8, 9, 9, 9), (9, 8, 9, 9), (9, 9, 8, 9), and (9, 9, 9, 8).
Input
The input consists of several datasets. Each dataset consists of n (1 β€ n β€ 50) in a line. The number of datasets is less than or equal to 50.
Output
Print the number of combination in a line.
Example
Input
35
1
Output
4
4
Submitted Solution:
```
while True:
try:
n = int(input())
except:
exit()
m = 9
c = 0
def loop(n,dep):
l = n - m*(3-dep)
u = n
if (l < 0):l = 0
if (m < u):u = 9
return l,u+1
il,iu= loop(n,0)
for i in range(il,iu):
jl,ju = loop(n-i,1)
for j in range(jl,ju):
ij = i+j
kl,ku = loop(n-ij,2)
for k in range(kl,ku):
c += 1
print(c)
``` | instruction | 0 | 52,076 | 11 | 104,152 |
Yes | output | 1 | 52,076 | 11 | 104,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program which reads an integer n and identifies the number of combinations of a, b, c and d (0 β€ a, b, c, d β€ 9) which meet the following equality:
a + b + c + d = n
For example, for n = 35, we have 4 different combinations of (a, b, c, d): (8, 9, 9, 9), (9, 8, 9, 9), (9, 9, 8, 9), and (9, 9, 9, 8).
Input
The input consists of several datasets. Each dataset consists of n (1 β€ n β€ 50) in a line. The number of datasets is less than or equal to 50.
Output
Print the number of combination in a line.
Example
Input
35
1
Output
4
4
Submitted Solution:
```
import sys
def comb(n):
count = 0
for i in range(10):
for j in range(10):
for k in range(10):
for l in range(10):
if i+j+k+l == n:
count += 1
continue
return count
while True:
line = sys.stdin.readline()
if not line: break
n = int(line.rstrip('\r\n'))
print(comb(n))
``` | instruction | 0 | 52,077 | 11 | 104,154 |
Yes | output | 1 | 52,077 | 11 | 104,155 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program which reads an integer n and identifies the number of combinations of a, b, c and d (0 β€ a, b, c, d β€ 9) which meet the following equality:
a + b + c + d = n
For example, for n = 35, we have 4 different combinations of (a, b, c, d): (8, 9, 9, 9), (9, 8, 9, 9), (9, 9, 8, 9), and (9, 9, 9, 8).
Input
The input consists of several datasets. Each dataset consists of n (1 β€ n β€ 50) in a line. The number of datasets is less than or equal to 50.
Output
Print the number of combination in a line.
Example
Input
35
1
Output
4
4
Submitted Solution:
```
import sys
def count(number) :
result = 0
for a in range(10) :
for b in range(10) :
for c in range(10) :
for d in range(10) :
if a + b + c + d == 35 : result += 1
return result
for make in sys.stdin.readlines():
print(count(make))
``` | instruction | 0 | 52,079 | 11 | 104,158 |
No | output | 1 | 52,079 | 11 | 104,159 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
for _ in range(int(input())):
input()
a=list(map(int,input().split()))
a=sorted(list(set(a)))
ans=max(a)
n=len(a)
for i in range(n):
x,y,z=0,0,a[i]
for j in reversed(range(i)):
if not y:
if z%a[j]!=0:
y=a[j]
elif not x:
if z%a[j]!=0 and y%a[j]!=0:
x=a[j]
else:
break
ans=max(ans,x+y+z)
print(ans)
``` | instruction | 0 | 52,290 | 11 | 104,580 |
Yes | output | 1 | 52,290 | 11 | 104,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
from sys import stdin
from itertools import tee
def input():
return stdin.readline()
def remove_divisors(x, xs):
return [y for y in xs if y%xs != 0]
q = int(input())
for _ in range(q):
n = int(input())
aas = list(set(map(int,input().split())))
aas.sort(reverse=True)
if len(aas) > 2 and aas[1]*2 > aas[0] and aas[2]*2 > aas[0]:
print(sum(aas[0:3]))
continue
ulim2 = list(map(sum, zip(aas[:-1], aas[1:]))) + [0]
ulim3 = list(map(sum, zip(aas[:-2], ulim2[1:]))) + [0,0]
it1 = iter(zip(aas, ulim3, ulim2))
answer = 0
try:
while True:
a, u3, _ = next(it1)
if u3 < answer:
break
it1, it2 = tee(it1)
def f1(i):
b, _, _ = i
return a%b != 0
it2 = filter(f1, it2)
tsum = 0
try:
while True:
b, _, u2 = next(it2)
if u2 < tsum:
break
it2, it3 = tee(it2)
def f2(i):
c, _, _ = i
return b%c != 0
it3 = filter (f2 , it3)
c,_,_ = next(it3, (0,0,0))
tsum = max(tsum, b+c)
except StopIteration:
pass
answer = max(answer, a + tsum)
except StopIteration:
pass
print(answer)
``` | instruction | 0 | 52,291 | 11 | 104,582 |
Yes | output | 1 | 52,291 | 11 | 104,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = [0, 0] + sorted(set(map(int, input().split())))
ans = a[-1]
for z in range(len(a)-1, 1, -1):
y = z-1
while a[y] > 0 and a[z] % a[y] == 0:
y -= 1
x = y-1
while a[x] > 0 and (a[z] % a[x] == 0 or a[y] % a[x] == 0):
x -= 1
ans = max(ans, a[x]+a[y]+a[z])
print(ans)
``` | instruction | 0 | 52,292 | 11 | 104,584 |
Yes | output | 1 | 52,292 | 11 | 104,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
# https://codeforces.com/contest/1183/problem/F
def solve(a_s):
a_s = set(a_s)
n = max(a_s)
largest = n
if n // 2 in a_s and n // 3 in a_s and n // 5 in a_s:
largest = n // 2 + n // 3 + n // 5
m = []
for _ in range(3):
if not a_s:
break
n = max(a_s)
a_s = set([j for j in a_s if n % j != 0])
m.append(n)
if sum(m) > largest:
largest = sum(m)
return largest
q = int(input())
for _ in range(q):
n = int(input())
a_s = list(map(int, input().split()))
print(solve(a_s))
``` | instruction | 0 | 52,293 | 11 | 104,586 |
Yes | output | 1 | 52,293 | 11 | 104,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
q = int(input())
for query in range(q):
n = int(input())
l = list(map(int,input().split()))
l.sort()
l.reverse()
if (len(l) > 3 and l[1] == l[0] / 2 and l[2] == l[0] / 3 and l[3] == l[0] / 5) or (len(l) > 4 and l[1] == l[0] / 2 and l[2] == l[0] / 3 and l[3] == l[0] /4 and l[4] == l[0] / 5):
print(l[1] + l[2] + l[3])
else:
nowa = [l[i] for i in range(n) if l[0] % l[i] != 0]
nowa.sort()
nowa.reverse()
if len(nowa) > 0:
part = 0
for i in range(1, len(nowa)):
if nowa[0] % nowa[i] !=0:
part = nowa[i]
break
wyn = l[0] + nowa[0] + part
else:
wyn = l[0]
print(wyn)
if len(l) > 100:
print(l[0],l[1],l[2],l[3])
``` | instruction | 0 | 52,294 | 11 | 104,588 |
No | output | 1 | 52,294 | 11 | 104,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
from sys import stdin,stdout
for _ in range(int(stdin.readline())):
n=int(stdin.readline())
l=list(map(int,stdin.readline().split()))
x,y,z=0,0,0
l.sort(reverse=True)
for i in l:
if x==0:
x=i
elif y==0 and x%i:
y=i
elif z==0 and x%i and y%i:
z=i
ans=x+y+z
if ans%30==0 and ans//2 in l and ans//3 in l and ans//5 in l:
ans=(31*ans)//30
print(ans)
``` | instruction | 0 | 52,295 | 11 | 104,590 |
No | output | 1 | 52,295 | 11 | 104,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
def calcSum(first):
global m
s = m[first]
k = 1
for i in range(first + 1, len(m)):
yes = True
for j in range(first, i):
if m[j] % m[i] == 0:
yes = False
break
if yes:
s += m[i]
k += 1
if k == 3: break
return s
import sys
nnn = int(input())
for _ in range(nnn):
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
if n == 1:
print(a[0])
continue
if n == 2:
if a[0]%a[1] == 0 or a[1]%a[0] == 0:
print(max(a))
else:
print(sum(a))
continue
a.sort(reverse = True)
m = [a[0], a[1]]
for i in range(2, len(a)):
yes = True
for j in range(2, i):
if a[j] % a[i] == 0:
yes = False
break
if yes:
m.append(a[i])
if len(m) >= 4:
break
## print(m)
s1 = calcSum(0)
s2 = calcSum(1)
s = max(s1, s2)
if nnn == 16383:
if _>300:
print(m, ' - ', a, '-', s)
else:
print(s)
``` | instruction | 0 | 52,296 | 11 | 104,592 |
No | output | 1 | 52,296 | 11 | 104,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One important contest will take place on the most famous programming platform (Topforces) very soon!
The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).
But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.
Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.
You have to answer q independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries.
The first line of the query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems.
The second line of the query contains n integers a_1, a_2, ..., a_n (2 β€ a_i β€ 2 β
10^5), where a_i is the prettiness of the i-th problem.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5.
Output
For each query print one integer β the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.
Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10
Submitted Solution:
```
from sys import stdin
from itertools import tee
def input():
return stdin.readline()
def remove_divisors(x, xs):
return [y for y in xs if y%xs != 0]
q = int(input())
for _ in range(q):
n = int(input())
aas = list(map(int,input().split()))
aas.sort(reverse=True)
if len(aas) > 2 and aas[1]*2 > aas[0] and aas[2]*2 > aas[0]:
print(sum(aas[0:3]))
continue
ulim2 = list(map(sum, zip(aas[:-1], aas[1:]))) + [0]
ulim3 = list(map(sum, zip(aas[:-2], ulim2[1:]))) + [0,0]
it1 = iter(zip(aas, ulim3, ulim2))
answer = 0
try:
while True:
a, u3, _ = next(it1)
if u3 < answer:
break
it1, it2 = tee(it1)
def f1(i):
b, _, _ = i
return a%b != 0
it2 = filter(f1, it2)
tsum = 0
try:
while True:
b, _, u2 = next(it2)
if u2 < tsum:
break
it2, it3 = tee(it2)
def f2(i):
c, _, _ = i
return b%c != 0
it3 = filter (f2 , it3)
c,_,_ = next(it3, (0,0,0))
tsum = max(tsum, b+c)
except StopIteration:
pass
answer = max(answer, a + tsum)
except StopIteration:
pass
print(answer)
``` | instruction | 0 | 52,297 | 11 | 104,594 |
No | output | 1 | 52,297 | 11 | 104,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
n,m=map(int,input().split())
a=[]
for i in range(n):
x=input()
a.append(x)
s=list(map(int,input().split()))
c=0
# print(a)
for i in range(m):
m={}
for j in range(n):
if a[j][i] in m:
m[a[j][i]]+=1
else:
m[a[j][i]]=1
# print(m)
v=list(m.values())
c+=max(v)*s[i]
print(c)
``` | instruction | 0 | 52,306 | 11 | 104,612 |
Yes | output | 1 | 52,306 | 11 | 104,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
x=input()
x=x.split()
n=int(x[0])
m=int(x[1])
q=[]
k=[]
total=0
for i in range(0,n):
q.append(input())
y=input()
y=y.split()
for i in range(0,m):
k.clear()
for j in q:
k.append(j[i])
a=k.count("A")
b=k.count("B")
c=k.count("C")
d=k.count("D")
e=k.count("E")
z=[a,b,c,d,e]
z.sort(reverse=True)
total+=int(y[i])*int(z[0])
print(total)
``` | instruction | 0 | 52,307 | 11 | 104,614 |
Yes | output | 1 | 52,307 | 11 | 104,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
maxsum=0
a,b=map(int, input().split())
tests=[]
for i in range(a):
tests+=[input()]
price=list(map(int, input().split()))
quantity=[0]*b
for i in range(b):
s=''
for j in range(a):
s+=tests[j][i]
maxsum+=price[i]*max(s.count('A'), s.count('B'), s.count('C'), s.count('D'), s.count('E'))
print(maxsum)
``` | instruction | 0 | 52,308 | 11 | 104,616 |
Yes | output | 1 | 52,308 | 11 | 104,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
R = lambda: map(int,input().split())
n,m = R()
L = []
for i in range(n):
L.append([i for i in input()])
C = list(R())
res = 0
for i in range(m):
f = [0]*5
for j in range(n):
f[ord(L[j][i])-ord('A')] += 1
res += (max(f)*C[i])
print(res)
``` | instruction | 0 | 52,309 | 11 | 104,618 |
Yes | output | 1 | 52,309 | 11 | 104,619 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
n, m = map(int, input().split())
sp = [list(input()) for _ in range(n)]
a = list(map(int, input().split()))
chet = 0
s = 0
for _ in range(m):
x = set()
for i in range(n):
x.add(sp[i][chet])
s += (n - len(x) + 1) * a[chet]
chet += 1
print(s)
``` | instruction | 0 | 52,310 | 11 | 104,620 |
No | output | 1 | 52,310 | 11 | 104,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
from collections import Counter
n, s_length = map(int, input().split())
us = {}
c = Counter()
for i in range(1, n + 1):
string = input()
for i in string:
c[i] += 1
x = [int(i) for i in input().split()]
l = 0
s = 0
for j in c.most_common(s_length):
s += x[l] * j[1]
l += 1
print(s)
``` | instruction | 0 | 52,311 | 11 | 104,622 |
No | output | 1 | 52,311 | 11 | 104,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
from functools import reduce
n,m=input().split()
n=int(n)
m=int(m)
l=[]
num=[]
l1=[]
for i in range(n):
l.append(input())
num=list(map(int, input().split()))
res = list(reduce(lambda i, j: i & j, (set(x) for x in l)))
val=sum(num)
for k in range(len(res)):
l2=[]
for i in range(len(l)):
for j in range(len(l[i])):
if k<len(res) and res[k]==l[i][j]:
l2.append(j)
z=all(x == l2[0] for x in l2)
if z==True:
val+=num[l2[0]]
print(val)
``` | instruction | 0 | 52,312 | 11 | 104,624 |
No | output | 1 | 52,312 | 11 | 104,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A class of students wrote a multiple-choice test.
There are n students in the class. The test had m questions, each of them had 5 possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question i worth a_i points. Incorrect answers are graded with zero points.
The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
Input
The first line contains integers n and m (1 β€ n, m β€ 1000) β the number of students in the class and the number of questions in the test.
Each of the next n lines contains string s_i (|s_i| = m), describing an answer of the i-th student. The j-th character represents the student answer (A, B, C, D or E) on the j-th question.
The last line contains m integers a_1, a_2, β¦, a_m (1 β€ a_i β€ 1000) β the number of points for the correct answer for every question.
Output
Print a single integer β the maximum possible total score of the class.
Examples
Input
2 4
ABCD
ABCE
1 2 3 4
Output
16
Input
3 3
ABC
BCD
CDE
5 4 12
Output
21
Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be 16.
In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is 5 + 4 + 12 = 21.
Submitted Solution:
```
n,m=map(int,input().split())
l=[]
for i in range(n):
s=input()
l.append(s)
a=list(map(int,input().split()))
ans=0
for i in range(len(l[0])):
ctr=0
for j in range(n):
if(l[0][i]==l[j][i]):
ctr+=1
ans+=a[i]*ctr
print(ans)
``` | instruction | 0 | 52,313 | 11 | 104,626 |
No | output | 1 | 52,313 | 11 | 104,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a bag of size n. Also you have m boxes. The size of i-th box is a_i, where each a_i is an integer non-negative power of two.
You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.
For example, if n = 10 and a = [1, 1, 32] then you have to divide the box of size 32 into two parts of size 16, and then divide the box of size 16. So you can fill the bag with boxes of size 1, 1 and 8.
Calculate the minimum number of divisions required to fill the bag of size n.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^{18}, 1 β€ m β€ 10^5) β the size of bag and the number of boxes, respectively.
The second line of each test case contains m integers a_1, a_2, ... , a_m (1 β€ a_i β€ 10^9) β the sizes of boxes. It is guaranteed that each a_i is a power of two.
It is also guaranteed that sum of all m over all test cases does not exceed 10^5.
Output
For each test case print one integer β the minimum number of divisions required to fill the bag of size n (or -1, if it is impossible).
Example
Input
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
Output
2
-1
0
Submitted Solution:
```
t = int(input())
for ijkl in range(t):
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
f = max(a)
f = max(f, n)
number = 1
power = 2
while power <= f:
power = power * 2
number += 1
have = [0 for i in range(number)]
for x in a:
copy = x
deg = 0
while copy > 1:
copy = copy // 2
deg += 1
have[deg] += 1
numbers = [0 for i in range(number)]
for i in range(number):
last = n % 2
numbers[i] = last
n = n // 2
#print(number, have, numbers)
ans = 0
ok = True
for i in range(number):
if numbers[i] == 1:
if have[i] >= 1:
have[i] -= 1
else:
j = i + 1
if j == number:
ok = False
#print(i, j, have)
break
while have[j] == 0:
j += 1
if j == number:
ok = False
#print(i, j)
break
ans += j - i
while j > i:
have[j] -= 1
have[j-1] += 2
j = j - 1
have[i] -= 1
if (i+1 != number):
have[i+1] += have[i] // 2
have[i] = 0
if ok:
print(ans)
else:
print(-1)
``` | instruction | 0 | 52,368 | 11 | 104,736 |
Yes | output | 1 | 52,368 | 11 | 104,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a bag of size n. Also you have m boxes. The size of i-th box is a_i, where each a_i is an integer non-negative power of two.
You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.
For example, if n = 10 and a = [1, 1, 32] then you have to divide the box of size 32 into two parts of size 16, and then divide the box of size 16. So you can fill the bag with boxes of size 1, 1 and 8.
Calculate the minimum number of divisions required to fill the bag of size n.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^{18}, 1 β€ m β€ 10^5) β the size of bag and the number of boxes, respectively.
The second line of each test case contains m integers a_1, a_2, ... , a_m (1 β€ a_i β€ 10^9) β the sizes of boxes. It is guaranteed that each a_i is a power of two.
It is also guaranteed that sum of all m over all test cases does not exceed 10^5.
Output
For each test case print one integer β the minimum number of divisions required to fill the bag of size n (or -1, if it is impossible).
Example
Input
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
Output
2
-1
0
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 19:24:32 2020
@author: dennis
"""
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
import math
def lowest_pair(li):
for i in range(61):
if li.count(i) >= 2:
return i
return -1
def closest_n(li, n):
for i in range(n, len(li)):
if li[i]:
return i
return -1
for _ in range(int(input())):
bag_size, n_boxes = [int(x) for x in input().split()]
boxes = [int(x) for x in input().split()]
# @profile
# def main():
# for _ in range(1):
# bag_size, n_boxes = 35184372088831, 100000
# boxes = [536870912]*n_boxes
log_boxes = [0]*61
for box in boxes:
log_boxes[int(math.log2(box))] += 1
if sum(boxes) < bag_size:
print(-1)
continue
bin_bag = [int(x) for x in bin(bag_size)[2:][::-1]]
divisions = 0
while 1 in bin_bag:
i = bin_bag.index(1)
if log_boxes[i]:
log_boxes[i] -= 1
bin_bag[i] = 0
else:
for j in range(0, i):
n = log_boxes[j]
log_boxes[j] = n%2
log_boxes[j+1] += n//2
if log_boxes[i]:
log_boxes[i] -= 1
bin_bag[i] = 0
else:
while not log_boxes[i]:
n = closest_n(log_boxes, i)
log_boxes[n] -= 1
log_boxes[n-1] += 2
divisions += 1
log_boxes[i] -= 1
bin_bag[i] = 0
print(divisions)
# main()
'''
'''
``` | instruction | 0 | 52,369 | 11 | 104,738 |
Yes | output | 1 | 52,369 | 11 | 104,739 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a bag of size n. Also you have m boxes. The size of i-th box is a_i, where each a_i is an integer non-negative power of two.
You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.
For example, if n = 10 and a = [1, 1, 32] then you have to divide the box of size 32 into two parts of size 16, and then divide the box of size 16. So you can fill the bag with boxes of size 1, 1 and 8.
Calculate the minimum number of divisions required to fill the bag of size n.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^{18}, 1 β€ m β€ 10^5) β the size of bag and the number of boxes, respectively.
The second line of each test case contains m integers a_1, a_2, ... , a_m (1 β€ a_i β€ 10^9) β the sizes of boxes. It is guaranteed that each a_i is a power of two.
It is also guaranteed that sum of all m over all test cases does not exceed 10^5.
Output
For each test case print one integer β the minimum number of divisions required to fill the bag of size n (or -1, if it is impossible).
Example
Input
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
Output
2
-1
0
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
from typing import List
"""
created by shhuan at 2020/3/19 14:23
"""
def solve(N, A):
if sum(A) < N:
return -1
nb = 64
needs = [0 for _ in range(nb)]
for i in range(nb):
if N & (1 << i):
needs[i] = 1
have = [0 for _ in range(65)]
for a in A:
b = 0
while a > 0:
a >>= 1
b += 1
have[b - 1] += 1
ans = 0
for i in range(nb):
if have[i] >= needs[i]:
have[i] -= needs[i]
have[i+1] += have[i] // 2
else:
for j in range(i+1, 64):
if have[j] > 0:
have[j] -= 1
for k in range(i, j):
have[k] += 1
ans += j-i
needs[i] = 0
break
if needs[i] > 0:
return -1
return ans
T = int(input())
for ti in range(T):
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
print(solve(N, A))
``` | instruction | 0 | 52,370 | 11 | 104,740 |
Yes | output | 1 | 52,370 | 11 | 104,741 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a bag of size n. Also you have m boxes. The size of i-th box is a_i, where each a_i is an integer non-negative power of two.
You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.
For example, if n = 10 and a = [1, 1, 32] then you have to divide the box of size 32 into two parts of size 16, and then divide the box of size 16. So you can fill the bag with boxes of size 1, 1 and 8.
Calculate the minimum number of divisions required to fill the bag of size n.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^{18}, 1 β€ m β€ 10^5) β the size of bag and the number of boxes, respectively.
The second line of each test case contains m integers a_1, a_2, ... , a_m (1 β€ a_i β€ 10^9) β the sizes of boxes. It is guaranteed that each a_i is a power of two.
It is also guaranteed that sum of all m over all test cases does not exceed 10^5.
Output
For each test case print one integer β the minimum number of divisions required to fill the bag of size n (or -1, if it is impossible).
Example
Input
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
Output
2
-1
0
Submitted Solution:
```
for _ in range(int(input())):
m,n=map(int,input().split())
a=list(map(int,input().split()))
mark=[0]*65
now=1
for indx in range(65):
for i in a:
if(i==now):
mark[indx]+=1
now<<=1
s=bin(m)[2:]
s=s[::-1]
ans=0
flg=1
carry=0
indx=0
for now in s:
if(now=='0'):
carry+=(1<<indx)*(mark[indx])
else:
if(mark[indx]):
carry+=(1<<indx)*(mark[indx]-1)
elif(carry>=(1<<indx)):
carry-=(1<<indx)
else:
j=indx
while(j<65):
if(mark[j]):
break
j+=1
if(j==65):
flg=0
break
mark[j]-=1
for h in range(j-1,indx,-1):
mark[h]+=1
ans+=1
ans+=1
indx+=1
if(flg):
print(ans)
else:
print(-1)
``` | instruction | 0 | 52,371 | 11 | 104,742 |
Yes | output | 1 | 52,371 | 11 | 104,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a bag of size n. Also you have m boxes. The size of i-th box is a_i, where each a_i is an integer non-negative power of two.
You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.
For example, if n = 10 and a = [1, 1, 32] then you have to divide the box of size 32 into two parts of size 16, and then divide the box of size 16. So you can fill the bag with boxes of size 1, 1 and 8.
Calculate the minimum number of divisions required to fill the bag of size n.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^{18}, 1 β€ m β€ 10^5) β the size of bag and the number of boxes, respectively.
The second line of each test case contains m integers a_1, a_2, ... , a_m (1 β€ a_i β€ 10^9) β the sizes of boxes. It is guaranteed that each a_i is a power of two.
It is also guaranteed that sum of all m over all test cases does not exceed 10^5.
Output
For each test case print one integer β the minimum number of divisions required to fill the bag of size n (or -1, if it is impossible).
Example
Input
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
Output
2
-1
0
Submitted Solution:
```
def lsb(N):
for i in range(64):
if N & (1 << i):
return i
assert False
def pay(top, N):
assert N != 0
lsb_pow = 1 << lsb(N)
res = 0
while top != lsb_pow:
res += 1
top //= 2
return res
def cost(N, nums):
nums.sort(reverse=True)
#print(N, nums)
res = 0
curr_sum = sum(nums)
if curr_sum < N: return -1
while N != 0:
#print((N, curr_sum, nums))
assert len(nums) > 0
assert curr_sum >= N
top = nums.pop()
if (1 << lsb(N)) == top:
N -= top
curr_sum -= top
elif top <= N:
assert top < N
assert len(nums) > 0
# take it if we have to:
if curr_sum - top < N:
N -= top
curr_sum -= top
continue
all_equal = [top]
while len(nums) > 0 and nums[-1] == top:
all_equal.append(nums.pop())
for i in range(len(all_equal) // 2):
nums.append(top*2)
curr_sum -= top * (len(all_equal) % 2)
else:
# We've reached the end. Have to pay.
return pay(top, N)
return 0
#cost(51, [16, 16, 16, 8, 2]) #[16, 16, 16, 16, 2])
#import random
#while True:
# N = random.randint(1, 100)
# M = 5
# lst = [2**random.randint(1, 5) for _ in range(M)]
# cost(N, lst)
T = int(input())
for _ in range(T):
N, _ = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]
print(cost(N, nums))
``` | instruction | 0 | 52,372 | 11 | 104,744 |
No | output | 1 | 52,372 | 11 | 104,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a bag of size n. Also you have m boxes. The size of i-th box is a_i, where each a_i is an integer non-negative power of two.
You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.
For example, if n = 10 and a = [1, 1, 32] then you have to divide the box of size 32 into two parts of size 16, and then divide the box of size 16. So you can fill the bag with boxes of size 1, 1 and 8.
Calculate the minimum number of divisions required to fill the bag of size n.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^{18}, 1 β€ m β€ 10^5) β the size of bag and the number of boxes, respectively.
The second line of each test case contains m integers a_1, a_2, ... , a_m (1 β€ a_i β€ 10^9) β the sizes of boxes. It is guaranteed that each a_i is a power of two.
It is also guaranteed that sum of all m over all test cases does not exceed 10^5.
Output
For each test case print one integer β the minimum number of divisions required to fill the bag of size n (or -1, if it is impossible).
Example
Input
3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8
Output
2
-1
0
Submitted Solution:
```
def problem(n,a):
if sum(a) < n:
return -1
bit = [0] * 61
for i in a:
bit[i.bit_length() - 1] += 1
i, r = 0, 0
while i < 60:
if n >> i & 1:
if bit[i]:
bit[i] -= 1
else:
while i < 60 and bit[i] == 0:
bit[i], i, r = bit[i] + 1, i + 1, r + 1
bit[i] -= 1
bit[i+1] += bit[i] >> 1
i += 1
return r
for i in range(int(input())):
n = int(input().split()[0])
a = list(map(int,input().split()))
print(problem(n, a))
``` | instruction | 0 | 52,374 | 11 | 104,748 |
No | output | 1 | 52,374 | 11 | 104,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,410 | 11 | 104,820 |
Tags: greedy, sortings
Correct Solution:
```
n=int(input())
a=str(input()).split()
a=map(int,a)
a=sorted(a)
s=10
x=0
t=0
for i in range(n):
x+=a[i]
if x<360:
k=n
s=0
else:
i=0
while 360 >= (s+a[i]):
s+=a[i]
i+=1
k=i
if k !=0:
a[i]=a[i]-360+s
s=0
while (t+a[i]) <=360:
t+=a[i]
s+=t
k+=1
i+=1
if i==n:
t=370
i-=1
else:
if a[0]>710:
s=0
k=0
else:
k=1
s=a[0]-350
print(k,' ',s)
``` | output | 1 | 52,410 | 11 | 104,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,411 | 11 | 104,822 |
Tags: greedy, sortings
Correct Solution:
```
n = input()
a = sorted(map(int, input().split()))
ans = 0; t = 10; pen = 0
for i in a:
t += i
if t > 720:
break
ans += 1
pen += max(0, t - 360)
print(ans, pen)
# Made By Mostafa_Khaled
``` | output | 1 | 52,411 | 11 | 104,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,412 | 11 | 104,824 |
Tags: greedy, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
time = 0
cnt = 0
while cnt < n and time <= 350:
if time + a[cnt] > 350:
break
else:
time += a[cnt]
cnt += 1
left_time = 350 - time
if cnt < n:
a[cnt] -= left_time
penalty = [0]
all_time = 0
while cnt < n and all_time <= 360:
if all_time + a[cnt] > 360:
break
else:
all_time += a[cnt]
penalty.append(penalty[-1] + a[cnt])
cnt += 1
print(cnt, sum(penalty))
``` | output | 1 | 52,412 | 11 | 104,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,413 | 11 | 104,826 |
Tags: greedy, sortings
Correct Solution:
```
import sys
from itertools import *
from math import *
def solve():
n = int(input())
a = list(map(int, input().split()))
a.sort()
time = -6*60 + 10
maxtime = 6 * 60
completed = 0
penalty = 0
for i, val in enumerate(a):
timefinish = time + val
if timefinish <= maxtime:
penalty += max(0, timefinish)
completed+=1
time = timefinish
print(completed, penalty)
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
solve()
``` | output | 1 | 52,413 | 11 | 104,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,414 | 11 | 104,828 |
Tags: greedy, sortings
Correct Solution:
```
n = input()
a = sorted(map(int, input().split()))
ans = 0; t = 10; pen = 0
for i in a:
t += i
if t > 720:
break
ans += 1
pen += max(0, t - 360)
print(ans, pen)
``` | output | 1 | 52,414 | 11 | 104,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,415 | 11 | 104,830 |
Tags: greedy, sortings
Correct Solution:
```
N = int(input())
nums = list(map(int, input().split()))
nums.sort()
fulltime = 720
start = 10
penalty = 0
probs = 0
for t in nums:
if t+start <= fulltime:
start += t
probs += 1
if start > 360:
penalty += (start - 360)
print(probs, penalty)
``` | output | 1 | 52,415 | 11 | 104,831 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,416 | 11 | 104,832 |
Tags: greedy, sortings
Correct Solution:
```
__author__ = 'Alex'
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
s = 10
i = 0
while i < n and a[i] + s <= 360:
s += a[i]
i += 1
ans = 0
while i < n and a[i] + s <= 720:
s += a[i]
ans += s - 360
i += 1
print(i, ans)
``` | output | 1 | 52,416 | 11 | 104,833 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | instruction | 0 | 52,417 | 11 | 104,834 |
Tags: greedy, sortings
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
a.sort()
s,ts,p = 0,10,0
for i in range(0,n):
if(ts+a[i]<=720):
ts=ts+a[i]
else:
break;
s=s+max(0,ts-360)
p=p+1
print(p,s)
``` | output | 1 | 52,417 | 11 | 104,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc
Submitted Solution:
```
from bisect import bisect_right
n = int(input())
t = list(map(int, input().split()))
t.sort()
for i in range(1, n):
t[i] += t[i - 1]
i = bisect_right(t, 350)
j = bisect_right(t, 710)
print(j, sum(t[k] for k in range(i, j)) - 350 * (j - i))
``` | instruction | 0 | 52,418 | 11 | 104,836 |
Yes | output | 1 | 52,418 | 11 | 104,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc
Submitted Solution:
```
n=int(input())
a=str(input()).split()
a=map(int,a)
a=sorted(a)
i=0
s=10
while s+a[i]<360:
s+=a[i]
i+=1
k=i
a[i]=a[i]-360+s
s=0
while s+a[i]<360:
s+=a[i]
i+=1
k+=1
print(k,' ',s)
``` | instruction | 0 | 52,419 | 11 | 104,838 |
No | output | 1 | 52,419 | 11 | 104,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc
Submitted Solution:
```
n=int(input())
a=str(input()).split()
a=map(int,a)
a=sorted(a)
s=10
x=0
for i in range(n):
x+=a[i]
if x<360:
k=n
s=0
else:
i=0
while 360 >= (s+a[i]):
s+=a[i]
i+=1
k=i
if k !=0:
a[i]=a[i]-360+s
s=0
while (s+a[i]) <=360:
s+=a[i]
k+=1
i+=1
if i==n:
x=s
s=370
i-=1
else:
if a[0]>710:
s=0
k=0
else:
k=1
s=a[0]-350
if s==370:
s=x
print(k,' ',s)
``` | instruction | 0 | 52,420 | 11 | 104,840 |
No | output | 1 | 52,420 | 11 | 104,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc
Submitted Solution:
```
n=int(input())
a=str(input()).split()
a=map(int,a)
a=sorted(a)
s=10
x=0
for i in range(n):
x+=a[i]
if x<360:
k=n
s=0
else:
i=0
while 360 >= (s+a[i]):
s+=a[i]
i+=1
k=i
if k !=0:
a[i]=a[i]-360+s
s=0
while (s+a[i]) <=360:
s+=a[i]
k+=1
i+=1
if i==n:
x=s
s=370
i-=1
else:
k=1
s=a[0]-350
if s==370:
s=x
print(k,' ',s)
``` | instruction | 0 | 52,421 | 11 | 104,842 |
No | output | 1 | 52,421 | 11 | 104,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of the problems. The next line contains n space-separated integers ai (1 β€ ai β€ 720) β each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers β the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc
Submitted Solution:
```
from collections import defaultdict
n, t = int(input()), input().split()
p = defaultdict(int)
for i in t:
p[i] += 1
if len(p) < 3: print(0)
else:
q = list(p.items())
a, b = q[0][1], q[1][1]
x, y = 0, 1
if a < b: x, y, a, b = y, x, b, a
for i in range(2, len(q)):
if q[i][1] > b:
if q[i][1] > a: x, y, a, b = i, x, q[i][1], a
else: y, b = i, q[i][1]
k = n // 3
if a > k:
c, s = n - a - b, n - a
if c > b: q[x], q[y], k = (q[x][0], (s >> 1)), (q[y][0], b - (s & 1)), (s >> 1)
#(q[y][0], b - (s & 1)), (s >> 1)
else: q[x], q[y], k = (q[x][0], c), (q[y][0], c), c
q = [(int(x), y) for x, y in q]
q.sort(reverse = True)
p, n = [], 2 * k
for i in q:
p += [str(i[0]) + ' '] * i[1]
print(k)
print('\n'.join(p[i] + p[k + i] + p[n + i] for i in range(k)))
#mas facil estaba examen de Cappo
``` | instruction | 0 | 52,422 | 11 | 104,844 |
No | output | 1 | 52,422 | 11 | 104,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A widely known among some people Belarusian sport programmer Lesha decided to make some money to buy a one square meter larger flat. To do this, he wants to make and carry out a Super Rated Match (SRM) on the site Torcoder.com. But there's a problem β a severe torcoder coordinator Ivan does not accept any Lesha's problem, calling each of them an offensive word "duped" (that is, duplicated). And one day they nearely quarrelled over yet another problem Ivan wouldn't accept.
You are invited to act as a fair judge and determine whether the problem is indeed brand new, or Ivan is right and the problem bears some resemblance to those used in the previous SRMs.
You are given the descriptions of Lesha's problem and each of Torcoder.com archive problems. The description of each problem is a sequence of words. Besides, it is guaranteed that Lesha's problem has no repeated words, while the description of an archive problem may contain any number of repeated words.
The "similarity" between Lesha's problem and some archive problem can be found as follows. Among all permutations of words in Lesha's problem we choose the one that occurs in the archive problem as a subsequence. If there are multiple such permutations, we choose the one with the smallest number of inversions. Then the "similarity" of a problem can be written as <image>, where n is the number of words in Lesha's problem and x is the number of inversions in the chosen permutation. Note that the "similarity" p is always a positive integer.
The problem is called brand new if there is not a single problem in Ivan's archive which contains a permutation of words from Lesha's problem as a subsequence.
Help the boys and determine whether the proposed problem is new, or specify the problem from the archive which resembles Lesha's problem the most, otherwise.
Input
The first line contains a single integer n (1 β€ n β€ 4) β the number of words in Lesha's problem. The second line contains n space-separated words β the short description of the problem.
The third line contains a single integer m (1 β€ m β€ 10) β the number of problems in the Torcoder.com archive. Next m lines contain the descriptions of the problems as "k s1 s2 ... sk", where k (1 β€ k β€ 20) is the number of words in the problem and si is a word of the problem description.
All words from all problem descriptions contain no more than 10 lowercase English letters.
Output
If Lesha's problem is brand new, print string "Brand new problem!" (without quotes).
Otherwise, on the first line print the index of the archive problem which resembles Lesha's problem most. If there are multiple such problems, print the one with the smallest index. On the second line print a string consisting of characters [:, character | repeated p times, and characters :], where p is the "similarity" between this problem and Lesha's one. The archive problems are numbered starting from one in the order in which they are given in the input.
Examples
Input
4
find the next palindrome
1
10 find the previous palindrome or print better luck next time
Output
1
[:||||||:]
Input
3
add two numbers
3
1 add
2 two two
3 numbers numbers numbers
Output
Brand new problem!
Input
4
these papers are formulas
3
6 what are these formulas and papers
5 papers are driving me crazy
4 crazy into the night
Output
1
[:||||:]
Input
3
add two decimals
5
4 please two decimals add
5 decimals want to be added
4 two add decimals add
4 add one two three
7 one plus two plus three equals six
Output
3
[:|||:]
Note
Let us remind you that the number of inversions is the number of pairs of words that follow in the permutation not in their original order. Thus, for example, if the original problem is "add two numbers", then permutation "numbers add two" contains two inversions β pairs of words "numbers" and "add", "numbers" and "two".
Sequence b1, b2, ..., bk is a subsequence of sequence a1, a2, ..., an if there exists such a set of indices 1 β€ i1 < i2 < ... < ik β€ n that aij = bj (in other words, if sequence b can be obtained from a by deleting some of its elements).
In the first test case the first problem contains the "find the palindrome next" permutation as a subsequence, in which the number of inversions equals 1 (words "palindrome" and "next").
In the second test case there is no problem that contains a permutation of words from Lesha's problem as a subsequence.
Submitted Solution:
```
import itertools
def check(curr_words, line):
if curr_words == []:
return True
for i in range(len(line)):
if line[i] == curr_words[0]:
return check(curr_words[1:], line[i+1:])
return False
n = int(input())
words = input().split()
m = int(input())
res, idx = 0, 0
for i in range(m):
line = input().split()[1:]
for p in itertools.permutations(range(n)):
curr_words = [words[j] for j in p]
cnt = 0
for j in range(n):
cnt += len([k for k in range(j+1, n) if p[k] < p[j]])
v = n * (n-1) // 2 - cnt + 1
if check(curr_words, line[:]) and v > res:
res, idx = v, i+1
if res > 0:
print(idx)
print('[:'+str('|'*res)+':]')
else:
print('Brand new wordslem!')
``` | instruction | 0 | 52,493 | 11 | 104,986 |
No | output | 1 | 52,493 | 11 | 104,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx β€ T β€ rx.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
Input
The first line contains one integer n (1 β€ n β€ 1000) β the number of problems. The second line contains n integers ai (1 β€ ai β€ 105) β the time Pasha needs to solve ith problem.
The third line contains one integer m (0 β€ m β€ 1000) β the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 β€ lj < rj β€ 105) β the starting and the ending moment of jth period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.
Output
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets).
Examples
Input
2
3 4
2
1 4
7 9
Output
7
Input
1
5
1
1 4
Output
-1
Input
1
5
1
1 5
Output
5
Note
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period.
Submitted Solution:
```
n = int(input())
sm = 0
for i in input().split(): sm += int(i)
m = int(input())
for i in range(m):
(a,b) = (int(j) for j in input().split())
if sm<=b:
if sm>=a:
print(sm)
else:
print(a)
sm = -1
break
if sm!=-1:
print('-1')
``` | instruction | 0 | 52,747 | 11 | 105,494 |
Yes | output | 1 | 52,747 | 11 | 105,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx β€ T β€ rx.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
Input
The first line contains one integer n (1 β€ n β€ 1000) β the number of problems. The second line contains n integers ai (1 β€ ai β€ 105) β the time Pasha needs to solve ith problem.
The third line contains one integer m (0 β€ m β€ 1000) β the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 β€ lj < rj β€ 105) β the starting and the ending moment of jth period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.
Output
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets).
Examples
Input
2
3 4
2
1 4
7 9
Output
7
Input
1
5
1
1 4
Output
-1
Input
1
5
1
1 5
Output
5
Note
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period.
Submitted Solution:
```
n=int(input())
m=sum(map(int,input().split()))
t=0
for _ in range(0,int(input())):
(a,b)=map(int,input().split())
if a>=m:
print(a)
t=1
break
if a<=m and b>=m:
print(m)
t=1
break
if t==0:print("-1")
``` | instruction | 0 | 52,748 | 11 | 105,496 |
Yes | output | 1 | 52,748 | 11 | 105,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx β€ T β€ rx.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
Input
The first line contains one integer n (1 β€ n β€ 1000) β the number of problems. The second line contains n integers ai (1 β€ ai β€ 105) β the time Pasha needs to solve ith problem.
The third line contains one integer m (0 β€ m β€ 1000) β the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 β€ lj < rj β€ 105) β the starting and the ending moment of jth period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.
Output
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets).
Examples
Input
2
3 4
2
1 4
7 9
Output
7
Input
1
5
1
1 4
Output
-1
Input
1
5
1
1 5
Output
5
Note
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = [0]
prev_r = 0
for i in range(m):
l, r = map(int, input().split())
b = b + (l - prev_r - 1) * [0]
b = b + (r - l + 1) * [1]
prev_r = r
s = sum(a)
for i in range(s, len(b)):
if b[i] == 1:
print(i)
exit()
print(-1)
``` | instruction | 0 | 52,749 | 11 | 105,498 |
Yes | output | 1 | 52,749 | 11 | 105,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx β€ T β€ rx.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
Input
The first line contains one integer n (1 β€ n β€ 1000) β the number of problems. The second line contains n integers ai (1 β€ ai β€ 105) β the time Pasha needs to solve ith problem.
The third line contains one integer m (0 β€ m β€ 1000) β the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 β€ lj < rj β€ 105) β the starting and the ending moment of jth period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.
Output
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets).
Examples
Input
2
3 4
2
1 4
7 9
Output
7
Input
1
5
1
1 4
Output
-1
Input
1
5
1
1 5
Output
5
Note
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period.
Submitted Solution:
```
def main():
n = int(input())
a = [int(s) for s in input().split()]
m = int(input())
if m == 0:
print(-1)
return
l, r = [], []
for _ in range(m):
x, y = [int(s) for s in input().split()]
l.append(x)
r.append(y)
total_time = sum(a)
period = 0
while period < m and total_time > r[period]:
period += 1
if period < m:
print(max(total_time, l[period]))
else:
print(-1)
main()
``` | instruction | 0 | 52,750 | 11 | 105,500 |
Yes | output | 1 | 52,750 | 11 | 105,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx β€ T β€ rx.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
Input
The first line contains one integer n (1 β€ n β€ 1000) β the number of problems. The second line contains n integers ai (1 β€ ai β€ 105) β the time Pasha needs to solve ith problem.
The third line contains one integer m (0 β€ m β€ 1000) β the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 β€ lj < rj β€ 105) β the starting and the ending moment of jth period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.
Output
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets).
Examples
Input
2
3 4
2
1 4
7 9
Output
7
Input
1
5
1
1 4
Output
-1
Input
1
5
1
1 5
Output
5
Note
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period.
Submitted Solution:
```
def competition(a, lst):
s = sum(a)
for elem in lst:
if s <= elem[0]:
if s >= elem[1]:
return s
else:
return elem[1]
return -1
n = int(input())
b = [int(i) for i in input().split()]
c = list()
m = int(input())
for z in range(m):
l, r = [int(x) for x in input().split()]
c.append([l, r])
print(competition(b, c))
``` | instruction | 0 | 52,751 | 11 | 105,502 |
No | output | 1 | 52,751 | 11 | 105,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx β€ T β€ rx.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
Input
The first line contains one integer n (1 β€ n β€ 1000) β the number of problems. The second line contains n integers ai (1 β€ ai β€ 105) β the time Pasha needs to solve ith problem.
The third line contains one integer m (0 β€ m β€ 1000) β the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 β€ lj < rj β€ 105) β the starting and the ending moment of jth period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.
Output
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets).
Examples
Input
2
3 4
2
1 4
7 9
Output
7
Input
1
5
1
1 4
Output
-1
Input
1
5
1
1 5
Output
5
Note
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
nn = int(input())
result = []
count = 0
if nn == 0:
print(-1)
exit()
for _ in range(nn):
ll, ul = list(map(int, input().split()))
result.append([ll, ul])
result = sorted(result)
s = sum(arr)
if s <= result[-1][1]:
count += 1
print(s)
else:
for k in result:
if k[1] >= sum(arr):
print(k[0])
count += 1
break
elif k[0] >= sum(arr):
print(k[0])
count += 1
break
if count == 0:
print(-1)
``` | instruction | 0 | 52,752 | 11 | 105,504 |
No | output | 1 | 52,752 | 11 | 105,505 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.