message stringlengths 2 22.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 145 109k | cluster float64 9 9 | __index_level_0__ int64 290 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lavrenty, a baker, is going to make several buns with stuffings and sell them.
Lavrenty has n grams of dough as well as m different stuffing types. The stuffing types are numerated from 1 to m. Lavrenty knows that he has ai grams left of the i-th stuffing. It takes exactly bi grams of stuffing i and ci grams of dough to cook a bun with the i-th stuffing. Such bun can be sold for di tugriks.
Also he can make buns without stuffings. Each of such buns requires c0 grams of dough and it can be sold for d0 tugriks. So Lavrenty can cook any number of buns with different stuffings or without it unless he runs out of dough and the stuffings. Lavrenty throws away all excess material left after baking.
Find the maximum number of tugriks Lavrenty can earn.
Input
The first line contains 4 integers n, m, c0 and d0 (1 β€ n β€ 1000, 1 β€ m β€ 10, 1 β€ c0, d0 β€ 100). Each of the following m lines contains 4 integers. The i-th line contains numbers ai, bi, ci and di (1 β€ ai, bi, ci, di β€ 100).
Output
Print the only number β the maximum number of tugriks Lavrenty can earn.
Examples
Input
10 2 2 1
7 3 2 100
12 3 1 10
Output
241
Input
100 1 25 50
15 5 20 10
Output
200
Note
To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing.
In the second sample Lavrenty should cook 4 buns without stuffings.
Submitted Solution:
```
from sys import stdin, setrecursionlimit
from collections import *
import threading
def arr_inp(n):
if n == 1:
return [int(x) for x in stdin.readline().split()]
elif n == 2:
return [float(x) for x in stdin.readline().split()]
else:
return list(stdin.readline()[:-1])
def dp(i, rem):
if rem == 0:
return 0
if i >= len(arr):
if rem:
return d0 * (rem // c0)
else:
return 0
if mem[i][rem] != -1:
return mem[i][rem]
ans1, ans2, ans3 = dp(i + 1, rem), 0, 0
if rem - arr[i][0] >= 0:
ans2 = arr[i][1] + dp(i + 1, rem - arr[i][0])
if rem - c0 >= 0:
ans3 = d0 + dp(i + 1, rem - c0)
mem[i][rem] = max(ans1, ans2, ans3)
return mem[i][rem]
def main():
ans = 0
for j in range(len(arr)):
mem[j][n] = 0
if arr:
mem[0][n - arr[0][0]] = arr[0][1]
for j in range(1, len(arr)):
for i in range(n + 1):
mem[j][i] = mem[j - 1][i]
if i + arr[j][0] <= n:
if mem[j - 1][i + arr[j][0]] != -1:
mem[j][i] = max(mem[j][i], mem[j - 1][i + arr[j][0]] + arr[j][1])
ans = max(ans, mem[j][i])
# print(dp(0, n))
for j in range(len(arr) + 1):
for i in range(n, -1, -1):
ext = (i // c0) * d0
if mem[j][i] == -1:
mem[j][i] = ext
else:
mem[j][i] += ext
ans = max(ans, mem[j][i])
print(ans)
if __name__ == '__main__':
n, m, c0, d0 = arr_inp(1)
staff, arr = [arr_inp(1) for i in range(m)], []
for a in staff:
arr.extend([[a[2], a[3]] for i in range(a[0] // a[1])])
mem = [[-1 for i in range(n + 1)] for j in range(len(arr) + 1)]
main()
``` | instruction | 0 | 68,958 | 9 | 137,916 |
No | output | 1 | 68,958 | 9 | 137,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 68,998 | 9 | 137,996 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
n, m = input().split()
boy_min = input().split()
gir_max = input().split()
for i in range(len(boy_min)):
boy_min[i] = int(boy_min[i])
for j in range(len(gir_max)):
gir_max[j] = int(gir_max[j])
boy_min.sort()
gir_max.sort()
if gir_max[0] < boy_min[-1]:
print(-1)
else:
total = sum(gir_max[1:])
if gir_max[0] == boy_min[-1]:
total += gir_max[0]
for i in range(len(boy_min)-1):
total += boy_min[i]*len(gir_max)
else:
total += boy_min[-1]
total += gir_max[0]
total += boy_min[-2]*(len(gir_max)-1)
for i in range(len(boy_min)-2):
total += boy_min[i]*len(gir_max)
print(total)
``` | output | 1 | 68,998 | 9 | 137,997 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 68,999 | 9 | 137,998 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
n,m=map(int,input().split())
b=list(map(int,input().split()))
g=list(map(int,input().split()))
mx=max(b)
mn=min(g)
if(mn>=mx):
b.sort()
g.sort()
sb=sum(b)
sg=sum(g)
ans=sb*m
if(mn==mx):
ans=ans-(m)*mx+sg
else:
ans=ans-b[n-1]*(m-1)+sg-g[0]-b[n-2]+g[0]
print(ans)
else:
print("-1")
``` | output | 1 | 68,999 | 9 | 137,999 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 69,000 | 9 | 138,000 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
n,m=list(map(int,input().split()))
b=list(map(int,input().split()))
g=list(map(int,input().split()))
y=max(b)
r=sum(b)
z=m*r
flag=0
if max(b)>min(g):
print(-1)
else:
flag=1
b.sort()
u=0
v=0
for i in g:
if i>y:
u+=1
t=i-y
z+=t
else:
v+=1
pass
if u>1 and v==0:
b.pop()
z=z+(y-b[-1])
print(z)
``` | output | 1 | 69,000 | 9 | 138,001 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 69,001 | 9 | 138,002 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
n,m = map(int,input().split())
Boys = [int(x) for x in input().split()]
Girls = [int(x) for x in input().split()]
if(min(Girls)<max(Boys)):
print(-1)
else:
Boys.sort(reverse = True)
Girls.sort(reverse = True)
Ans = 0
for i in range(n):
Ans += Boys[i] * m
g = 0
b = 0
bins = 1
while(g<m):
Ans += Girls[g] - Boys[b]
g+=1
bins+=1
if(bins==m):
if(g<m and Girls[g] - Boys[b]<=0):
g+=1
bins = 1
b+=1
print(Ans)
``` | output | 1 | 69,001 | 9 | 138,003 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 69,002 | 9 | 138,004 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
import sys
n, m = map(int, input().split())
boys = sorted(map(int, sys.stdin.readline().split()), reverse=True)
girls = sorted(map(int, sys.stdin.readline().split()), reverse=True)
if min(girls) < max(boys):
print(-1)
else:
total = m * sum(boys)
i = 0
j = 0
while i < m and girls[i] > max(boys):
total += sum([girls[k] - boys[j] for k in range(i, min(i + m-1, m))])
j += 1
i += m - 1
print(total)
``` | output | 1 | 69,002 | 9 | 138,005 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 69,003 | 9 | 138,006 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
s = sorted(list(map(int, input().split())))
if a[-1] > s[0]:
print(-1)
else:
if a[-1] == s[0]:
print(sum(a[:-1])*m+sum(s))
else:
print(sum(a[:-2])*m+a[-2]*(m-1)+sum(s)+a[-1])
``` | output | 1 | 69,003 | 9 | 138,007 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 69,004 | 9 | 138,008 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
import bisect
def party_sweet(b, g):
maxb = max(b)
ming = min(g)
if maxb > ming:
return -1
elif maxb == ming:
return (sum(b) - maxb)* len(g) + sum(g)
else:
return (sum(b))* len(g) + sum(g) - maxb * (len(g) - 1) - sorted(b)[-2]
def main():
n, m = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
print(party_sweet(b, g))
if __name__ == '__main__':
main()
``` | output | 1 | 69,004 | 9 | 138,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4. | instruction | 0 | 69,005 | 9 | 138,010 |
Tags: binary search, constructive algorithms, greedy, implementation, math, sortings, two pointers
Correct Solution:
```
n, m = map(int, input().split())
b = list(map(int, input().split()))
g = list(map(int, input().split()))
ind = 0; p = m - 1
ans = sum(b) * m
b.sort(reverse = 1); g.sort(reverse = 1)
for i in range(m):
if g[i] < b[0]:
print(-1)
exit()
if g[i] > b[0]:
if ind == n:
print(-1)
exit()
ans += g[i] - b[ind]
p -= 1
if p == 0:
ind += 1
p = m - 1
print(ans)
``` | output | 1 | 69,005 | 9 | 138,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if max(a)>min(b):
print (-1)
exit()
a.sort(reverse=True)
b.sort()
j = m-1
i = 0
ans = 0
while j>=0:
if j==0:
if b[j]==a[i]:
ans += b[j]
flag = 1
break
else:
ans += a[i]
flag = 0
break
if b[j]>=a[i]:
ans += b[j]
j -= 1
# print (ans)
for i in range(1,n):
ans += a[i]*m
# print (ans)
if flag:
print (ans)
else:
print (ans + b[0]-a[1])
``` | instruction | 0 | 69,006 | 9 | 138,012 |
Yes | output | 1 | 69,006 | 9 | 138,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
def main():
n, m = map(int, input().split())
bs = list(map(int, input().split()))
gs = list(map(int, input().split()))
num_sweets = m * sum(bs)
b = sorted(bs)
del bs
remaining_slots = [m-1] * n
for j, g in enumerate(gs):
lval_index = n-1
while lval_index >= 0:
if b[lval_index] > g:
return -1
if b[lval_index] == g or remaining_slots[lval_index]:
break
lval_index -= 1
if lval_index < 0:
return -1
else:
b_val = b[lval_index]
if b_val != g:
remaining_slots[lval_index] -= 1
num_sweets = num_sweets + (g - b_val)
return num_sweets
num_sweets = main()
print(num_sweets)
``` | instruction | 0 | 69,007 | 9 | 138,014 |
Yes | output | 1 | 69,007 | 9 | 138,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
dop = sum(a) * m
a.sort()
b.sort()
if a[-1] > b[0]:
print(-1)
else:
ans = 0
for i in range(m):
ans += b[i]
if a[-1] == b[0]:
ans -= a[-1] * m
else:
ans -= a[-1] * (m - 1)
ans -= a[-2]
ans = ans + dop
print(ans)
``` | instruction | 0 | 69,008 | 9 | 138,016 |
Yes | output | 1 | 69,008 | 9 | 138,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
################om namh shivay##################37
###############(BHOLE KI FAUJ KREGI MAUJ)############37
from sys import stdin,stdout
import math,queue,heapq
fastinput=stdin.readline
fastout=stdout.write
def isPrime(n) :
if (n <= 1) :
return False
if (n <= 3) :
return True
if (n % 2 == 0 or n % 3 == 0) :
return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def computeLPSArray(pat, M, lps):
len = 0
lps[0]
i = 1
while i < M:
if pat[i]== pat[len]:
len += 1
lps[i] = len
i += 1
else:
if len != 0:
len = lps[len-1]
else:
lps[i] = 0
i += 1
#t=int(fastinput())
m=10**9 +7
t=1
while t:
t-=1
#n=int(fastinput())
n,m=map(int,fastinput().split())
a=list(map(int,fastinput().split()))
b=list(map(int,fastinput().split()))
a.sort()
b.sort(reverse=True)
total=0
for i in a:
total+=m*i
for i in b[:-1]:
if i<a[-1]:
print(-1)
exit()
else:
total+=i-a[-1]
if b[-1]==a[-1]:
print(total)
elif b[-1]>a[-1]:
print(total+b[-1]-a[-2])
else:
print(-1)
#matrix=[list(map(int,fastinput().split())) for _ in range(n)]
``` | instruction | 0 | 69,009 | 9 | 138,018 |
Yes | output | 1 | 69,009 | 9 | 138,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
n,m=map(int,input().split())
b=list(map(int,input().split()))
g=list(map(int,input().split()))
b.sort()
g.sort()
if b[-1]>g[-1] or b[0]<g[0] or g[0]<b[-1]:
print(-1)
else:
ans=m*sum(b)
for i in g:
if i==b[-1]:
m-=1
if m%2==0:
c=(m//2)*(b[-1]+b[-2])
else:
c=b[-1]+(m//2)*(b[-1]+b[-2])
print(ans-c+sum(g[-m:]))
``` | instruction | 0 | 69,010 | 9 | 138,020 |
No | output | 1 | 69,010 | 9 | 138,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
n,m=map(int,input().split())
b=list(map(int,input().split()))
g=list(map(int,input().split()))
b.sort()
g.sort()
if max(b)>min(g):
print(-1)
else:
for i in g:
if i==b[-1]:
m-=1
b.reverse()
c=0
for i in range(m):
x=i%n
c+=b[x]
print(m*sum(b)+sum(g)-c)
``` | instruction | 0 | 69,011 | 9 | 138,022 |
No | output | 1 | 69,011 | 9 | 138,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
n, m = map(int,input().split())
b = sorted(list(map(int,input().split())),reverse=True)
g = sorted(list(map(int,input().split())),reverse=True)
if g[-1] < b[0]:
print(-1)
exit(0)
total = sum(b) *m
g_idx = 0
b_idx = 0
while g_idx < m:
temp = 0
while g_idx < m and temp < m-1:
if g[g_idx] == b[b_idx]:
g_idx +=1
continue
total += (g[g_idx] - b[b_idx])
g_idx+=1
temp+=1
b_idx+=1
print(total)
``` | instruction | 0 | 69,012 | 9 | 138,024 |
No | output | 1 | 69,012 | 9 | 138,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
n boys and m girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 1 to n and all girls are numbered with integers from 1 to m. For all 1 β€ i β€ n the minimal number of sweets, which i-th boy presented to some girl is equal to b_i and for all 1 β€ j β€ m the maximal number of sweets, which j-th girl received from some boy is equal to g_j.
More formally, let a_{i,j} be the number of sweets which the i-th boy give to the j-th girl. Then b_i is equal exactly to the minimum among values a_{i,1}, a_{i,2}, β¦, a_{i,m} and g_j is equal exactly to the maximum among values b_{1,j}, b_{2,j}, β¦, b_{n,j}.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of a_{i,j} for all (i,j) such that 1 β€ i β€ n and 1 β€ j β€ m. You are given the numbers b_1, β¦, b_n and g_1, β¦, g_m, determine this number.
Input
The first line contains two integers n and m, separated with space β the number of boys and girls, respectively (2 β€ n, m β€ 100 000). The second line contains n integers b_1, β¦, b_n, separated by spaces β b_i is equal to the minimal number of sweets, which i-th boy presented to some girl (0 β€ b_i β€ 10^8). The third line contains m integers g_1, β¦, g_m, separated by spaces β g_j is equal to the maximal number of sweets, which j-th girl received from some boy (0 β€ g_j β€ 10^8).
Output
If the described situation is impossible, print -1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
Examples
Input
3 2
1 2 1
3 4
Output
12
Input
2 2
0 1
1 0
Output
-1
Input
2 3
1 0
1 1 2
Output
4
Note
In the first test, the minimal total number of sweets, which boys could have presented is equal to 12. This can be possible, for example, if the first boy presented 1 and 4 sweets, the second boy presented 3 and 2 sweets and the third boy presented 1 and 1 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 12.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to 4. This can be possible, for example, if the first boy presented 1, 1, 2 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 4.
Submitted Solution:
```
n, m = map(int, input().split())
*a, = map(int, input().split())
*b, = map(int, input().split())
s = -1
if min(b) < max(a):
pass
else:
s = sum(a)*len(b)
a.sort(reverse=True)
j = 0
k = 0
for i in b:
s += i - a[j]
if i - a[j] > 0:
k += 1
if k == len(b):
j += 1
if j == len(a):
s = -1
break
k = 0
print(s)
``` | instruction | 0 | 69,013 | 9 | 138,026 |
No | output | 1 | 69,013 | 9 | 138,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | instruction | 0 | 69,396 | 9 | 138,792 |
Tags: data structures
Correct Solution:
```
import sys
from bisect import bisect
def input():
return sys.stdin.readline().strip()
def solve():
n, q = map(int, input().split())
was = set()
Q = [None]*q
all = [0]*(2*q)
for i in range(q):
x, y, t = input().split()
x, y = int(x), int(y)
Q[i] = (x, y, t)
all[2*i] = x
all[2*i+1] = y
all.sort()
i = 0
p = -1
F = dict()
for j in range(2*q):
v = all[j]
if v != p:
all[i] = v
F[v] = i
i += 1
p = v
sz = i
all = all[:sz]
V = [0]*(2*sz)
H = [0]*(2*sz)
for x, y, t in Q:
if (x,y) in was:
print(0)
else:
was.add((x,y))
if t == 'L':
TA = H
TB = V
else:
x, y = y, x
TA = V
TB = H
v = F[y] + sz
r = 0
while v > 0:
r = max(r, TA[v])
v //= 2
c = x - r
print(c)
r = F[x] + sz
l = bisect(all, x - c) + sz
while l <= r:
if l % 2 == 1:
TB[l] = max(TB[l], y)
if r % 2 == 0:
TB[r] = max(TB[r], y)
l = (l+1)//2
r = (r-1)//2
solve()
``` | output | 1 | 69,396 | 9 | 138,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | instruction | 0 | 69,397 | 9 | 138,794 |
Tags: data structures
Correct Solution:
```
import sys
from bisect import bisect
def input():
return sys.stdin.readline().strip()
def solve():
n, q = map(int, input().split())
was = set()
Q = [None]*q
all = [0]*(2*q)
for i in range(q):
x, y, t = input().split()
x, y = int(x), int(y)
Q[i] = (x, y, t)
all[2*i] = x
all[2*i+1] = y
all.sort()
i = 0
p = -1
for j in range(2*q):
v = all[j]
if v != p:
all[i] = v
i += 1
p = v
sz = i
all = all[:sz]
V = [0]*(2*sz)
H = [0]*(2*sz)
for x, y, t in Q:
if (x,y) in was:
print(0)
else:
was.add((x,y))
if t == 'L':
TA = H
TB = V
else:
x, y = y, x
TA = V
TB = H
v = bisect(all, y) - 1 + sz
r = 0
while v > 0:
r = max(r, TA[v])
v //= 2
c = x - r
print(c)
r = bisect(all, x) - 1 + sz
l = bisect(all, x - c) + sz
while l <= r:
if l % 2 == 1:
TB[l] = max(TB[l], y)
if r % 2 == 0:
TB[r] = max(TB[r], y)
l = (l+1)//2
r = (r-1)//2
solve()
``` | output | 1 | 69,397 | 9 | 138,795 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | instruction | 0 | 69,398 | 9 | 138,796 |
Tags: data structures
Correct Solution:
```
import sys
from bisect import bisect
def input():
return sys.stdin.readline().strip()
def solve():
n, q = map(int, input().split())
was = set()
Q = [None]*q
all = [0]*(2*q)
for i in range(q):
x, y, t = input().split()
x, y = int(x), int(y)
Q[i] = (x, y, t)
all[2*i] = x
all[2*i+1] = y
all.sort()
sz = 2*q
V = [0]*(2*sz)
H = [0]*(2*sz)
for x, y, t in Q:
if (x,y) in was:
print(0)
else:
was.add((x,y))
if t == 'L':
TA = H
TB = V
else:
x, y = y, x
TA = V
TB = H
v = bisect(all, y) - 1 + sz
r = 0
while v > 0:
r = max(r, TA[v])
v //= 2
c = x - r
print(c)
r = bisect(all, x) - 1 + sz
l = bisect(all, x - c) + sz
while l <= r:
if l % 2 == 1:
TB[l] = max(TB[l], y)
if r % 2 == 0:
TB[r] = max(TB[r], y)
l = (l+1)//2
r = (r-1)//2
solve()
``` | output | 1 | 69,398 | 9 | 138,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
Submitted Solution:
```
print('smd');
``` | instruction | 0 | 69,399 | 9 | 138,798 |
No | output | 1 | 69,399 | 9 | 138,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
Submitted Solution:
```
import sys
sys.setrecursionlimit(1000000000)
n,q=map(int,input().split())
v=[]
t=[]
for i in range(q):
s=input().split()
y=int(s[0])
x=int(s[1])
j=0
i1=i
i2=i
bb=True
while bb and len(v)>j:
(a,b,c,d)=v[j]
if x<=a:
v.insert(j,(x,y,s[2],i))
i2=j
bb=False
else:j+=1
if bb:v+=[(x,y,s[2],i)]
j=0
bb=True
while bb and len(t)>j:
(a,b,c,d)=t[j]
if x>=a:
t.insert(j,(x,y,s[2],i))
i1=j
bb=False
else:j+=1
if bb:t+=[(x,y,s[2],i)]
if s[2]=="U":
j=i1+1
bb=True
aa=0
while bb and j<len(t):
(a,b,c,d)=t[j]
if a==x or c=="L":
bb=False
aa=a
else:j+=1
print(x-aa)
t[i1]=(x,y,s[2],x-aa)
if s[2]=="L":
j=i2+1
bb=True
aa=0
while bb and j<len(v):
(a,b,c,d)=v[j]
if a==x or c=="U":
bb=False
aa=b
else:j+=1
print(y-aa)
v[i2]=(x,y,s[2],y-aa)
``` | instruction | 0 | 69,400 | 9 | 138,800 |
No | output | 1 | 69,400 | 9 | 138,801 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
Submitted Solution:
```
import sys
sys.setrecursionlimit(1000000000)
n,q=map(int,input().split())
v=[]
t=[]
for i in range(q):
s=input().split()
y=int(s[0])
x=int(s[1])
j=0
i1=q-1
i2=q-1
bb=True
while bb and len(v)>j:
(a,b,c,d)=v[j]
if x<=a:
v.insert(j,(x,y,s[2],i))
i2=j
bb=False
else:j+=1
if bb:v+=[(x,y,s[2],i)]
j=0
bb=True
while bb and len(t)>j:
(a,b,c,d)=t[j]
if x>=a:
t.insert(j,(x,y,s[2],i))
i1=j
bb=False
else:j+=1
if bb:t+=[(x,y,s[2],i)]
if s[2]=="U":
j=i1+1
bb=True
aa=0
while bb and j<len(t):
(a,b,c,d)=t[j]
if a==x or c=="L":
bb=False
aa=a
else:j+=1
print(x-aa)
if s[2]=="L":
j=i2+1
bb=True
aa=0
while bb and j<len(v):
(a,b,c,d)=v[j]
if a==x or c=="U":
bb=False
aa=b
else:j+=1
print(y-aa)
``` | instruction | 0 | 69,401 | 9 | 138,802 |
No | output | 1 | 69,401 | 9 | 138,803 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
Submitted Solution:
```
import bisect
N, Q = map(int, input().split())
row, column = [-1], [-1]
s = set()
for _ in range(Q):
x, y, c = input().split()
x, y = int(x)-1, int(y)-1
if (x, y, c) in s:
print(0)
continue
s.add((x, y, c))
if c == 'L':
# print(x, bisect.bisect(column, x))
print(x - column[bisect.bisect(column, x)-1])
bisect.insort(row, y)
elif c == 'U':
# print(y, bisect.bisect(row, y))
print(y - row[bisect.bisect(row, y)-1])
bisect.insort(column, x)
# print(row, column)
``` | instruction | 0 | 69,402 | 9 | 138,804 |
No | output | 1 | 69,402 | 9 | 138,805 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,734 | 9 | 139,468 |
"Correct Solution:
```
while True:
n,k = map(int, input().split())
if n == k == 0:
break
s = list(map(int, input().split()))
for _ in range(n):
b = list(map(int, input().split()))
for i in range(k):
s[i] -= b[i]
print("Yes" if min(s) >= 0 else "No")
``` | output | 1 | 69,734 | 9 | 139,469 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,735 | 9 | 139,470 |
"Correct Solution:
```
while 1:
n,k=map(int,input().split())
if n==0:break
a=[int(i) for i in input().split()]
for _ in range(n):
b=list(map(int,input().split()))
for j,c in enumerate(b):
a[j]-=c
f=0
for i in range(k):
if a[i]<0:
f=1
break
print('No' if f else 'Yes')
``` | output | 1 | 69,735 | 9 | 139,471 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,736 | 9 | 139,472 |
"Correct Solution:
```
while True:
n,k = map(int, input().split())
if n == k == 0:
break
s = [int(x) for x in input().split()]
for _ in range(n):
b = [int(x) for x in input().split()]
for i in range(k):
s[i] -= b[i]
print("Yes" if min(s) >= 0 else "No")
``` | output | 1 | 69,736 | 9 | 139,473 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,737 | 9 | 139,474 |
"Correct Solution:
```
while True:
N, K = map(int, input().split())
if N == 0:
break
S = [0] + list(map(int, input().split()))
flag = True
for _ in range(N):
B = list(map(int, input().split()))
for i, b in enumerate(B):
S[i+1] -= b
if S[i+1] < 0:
flag = False
break
print("Yes" if flag else "No")
``` | output | 1 | 69,737 | 9 | 139,475 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,738 | 9 | 139,476 |
"Correct Solution:
```
# AOJ 1019: Vampirish Night
# Python3 2018.7.4 bal4u
while True:
n, k = map(int, input().split())
if n == 0: break
s = list(map(int, input().split()))
b = [0]*k
for i in range(n):
a = list(map(int, input().split()))
for j in range(k): b[j] += a[j]
ans = True
for i in range(k):
if b[i] > s[i]:
ans = False
break
print("Yes" if ans else "No")
``` | output | 1 | 69,738 | 9 | 139,477 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,739 | 9 | 139,478 |
"Correct Solution:
```
if __name__ == '__main__':
while True:
N, K = list(map(int, input().strip().split()))
if N == 0 and K == 0:
break
lims = list(map(int, input().strip().split()))
totals = [ 0 for _ in range(K) ]
for _ in range(N):
arr = list(map(int, input().strip().split()))
for i in range(K):
totals[i] += arr[i]
tarimasu = True
for i in range(K):
if totals[i] > lims[i]:
tarimasu = False
break
print("Yes" if tarimasu else "No")
``` | output | 1 | 69,739 | 9 | 139,479 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,740 | 9 | 139,480 |
"Correct Solution:
```
while True :
N, K = map(int, input().split())
if N == 0 and K == 0 :
break
S = list(map(int, input().split()))
total_B = [0] * K
for i in range(N) :
B = list(map(int, input().split()))
for j in range(K) :
total_B[j] += B[j]
ans = 'Yes'
for i in range(K) :
if S[i] < total_B[i] :
ans = 'No'
break
print(ans)
``` | output | 1 | 69,740 | 9 | 139,481 |
Provide a correct Python 3 solution for this coding contest problem.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No | instruction | 0 | 69,741 | 9 | 139,482 |
"Correct Solution:
```
while True:
n, k = map(int, input().split())
if n == 0:
break
s_list = list(map(int, input().split()))
for _ in range(n):
b_list = list(map(int, input().split()))
for i in range(k):
s_list[i] -= b_list[i]
for s in s_list:
if s < 0:
print("No")
break
else:
print("Yes")
``` | output | 1 | 69,741 | 9 | 139,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No
Submitted Solution:
```
while True:
n, k = map(int, input().split())
if n + k == 0: break
s = list(map(int, input().split()))
for _ in range(n):
b = list(map(int, input().split()))
for j, _b in enumerate(b):
s[j] = s[j] - _b
bool_list = list(map(lambda x: x >= 0,s))
if False in bool_list:
print('No')
else:
print('Yes')
``` | instruction | 0 | 69,742 | 9 | 139,484 |
Yes | output | 1 | 69,742 | 9 | 139,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No
Submitted Solution:
```
while True:
N, K = [int(n) for n in input().split()]
if (N == 0 and K == 0):
quit()
fridge = [int(n) for n in input().split()]
vampires = []
for x in range(N):
blood = [int(n) for n in input().split()]
fridge = [fridge[i] - blood[i] for i in range(K)]
if len([n for n in fridge if n < 0]) >= 1:
print("NO")
else:
print("YES")
``` | instruction | 0 | 69,743 | 9 | 139,486 |
No | output | 1 | 69,743 | 9 | 139,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No
Submitted Solution:
```
while True:
N, K = [int(n) for n in input().split()]
if (N == 0 and K == 0):
break
fridge = [int(n) for n in input().split()]
vampires = []
for x in range(N):
blood = [int(n) for n in input().split()]
fridge = [fridge[i] - blood[i] for i in range(K)]
if len([n for n in fridge if n < 0]) >= 1:
print("NO")
else:
print("YES")
``` | instruction | 0 | 69,744 | 9 | 139,488 |
No | output | 1 | 69,744 | 9 | 139,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No
Submitted Solution:
```
import numpy as np
while True:
N, K = [int(n) for n in input().split()]
if (N == 0 and K == 0):
quit()
fridge = np.array([int(n) for n in input().split()])
vampires = []
for x in range(N):
blood = np.array([int(n) for n in input().split()])
fridge -= blood
if len([n for n in fridge if n < 0]) >= 1:
print("NO")
else:
print("YES")
``` | instruction | 0 | 69,745 | 9 | 139,490 |
No | output | 1 | 69,745 | 9 | 139,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a vampire family of N members. Vampires are also known as extreme gourmets. Of course vampires' foods are human blood. However, not all kinds of blood is acceptable for them. Vampires drink blood that K blood types of ones are mixed, and each vampire has his/her favorite amount for each blood type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your first task is to write a program that judges if all family members' dinner can be prepared using blood in the fridge.
Constraints
* Judge data includes at most 100 data sets.
* 1 β€ N β€ 100
* 1 β€ K β€ 100
* 0 β€ Si β€ 100000
* 0 β€ Bij β€ 1000
Input
Input file consists of a number of data sets. One data set is given in following format:
N K
S1 S2 ... SK
B11 B12 ... B1K
B21 B22 ... B2K
:
BN1 BN2 ... BNK
N and K indicate the number of family members and the number of blood types respectively.
Si is an integer that indicates the amount of blood of the i-th blood type that is in a fridge.
Bij is an integer that indicates the amount of blood of the j-th blood type that the i-th vampire uses.
The end of input is indicated by a case where N = K = 0. You should print nothing for this data set.
Output
For each set, print "Yes" if everyone's dinner can be prepared using blood in a fridge, "No" otherwise (without quotes).
Example
Input
2 3
5 4 5
1 2 3
3 2 1
3 5
1 2 3 4 5
0 1 0 1 2
0 1 1 2 2
1 0 3 1 1
0 0
Output
Yes
No
Submitted Solution:
```
while True:
a,b = map(int,input().split())
if a == 0 and b == 0:
break
data = []
for i in range(0,a+1):
data.append(list(map(int,input().split())))
sum = []
for i in range(0,b):
sum.append(0)
for x in data[1:]:
for y in range(0,b):
sum[y] += x[y]
if all((data[0][x] > sum[x] for x in (0,len(sum)-1))):
print("Yes")
else:
print("No")
``` | instruction | 0 | 69,746 | 9 | 139,492 |
No | output | 1 | 69,746 | 9 | 139,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,910 | 9 | 139,820 |
Tags: greedy, sortings
Correct Solution:
```
from collections import defaultdict
from sys import stdin, stdout
q = int(stdin.readline())
for it in range(q):
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
d = [0]*n
for x in a:
d[x-1]+=1
d.sort(reverse=True)
# print(l)
ans = d[0]
cur = d[0]
for x in d[1:]:
if x < cur:
ans += x
cur = x
else:
ans += cur - 1
cur = cur - 1
if cur == 0 or x == 0:
break
stdout.write(str(ans)+"\n")
``` | output | 1 | 69,910 | 9 | 139,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,911 | 9 | 139,822 |
Tags: greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
Q=int(input())
for testcase in range(Q):
n=int(input())
A=Counter((map(int,input().split())))
S=sorted(A.values(),reverse=True)
#print(A)
#print(S)
NOW=10**10
ANS=0
for s in S:
M=min(NOW,s)
ANS+=M
NOW=M-1
if NOW==0:
break
print(ANS)
``` | output | 1 | 69,911 | 9 | 139,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,912 | 9 | 139,824 |
Tags: greedy, sortings
Correct Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def func(self,a,b):
if a[0]>b[0]:
return a
return b
def __init__(self, data, default=(0,0)):
"""initialize the segment tree with data"""
func=self.func
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: max(a , b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] < key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
def count(s,n,a):
st=0
end=n-1
ans=0
while(st<=end):
mid=(st+end)//2
if a<=s[mid]:
ans=mid+1
st=mid+1
else:
end=mid-1
return ans
for ik in range(int(input())):
n=int(input())
d=[[0,0] for i in range(n+1)]
for i in range(n):
a,f=map(int,input().split())
d[a][0]+=1
d[a][1]+=f
d.sort(reverse=True)
ans=0
last=n*n
for i in range(n):
if last<=0:
break
ans+=min(d[i][0],last)
last=min(d[i][0],last)-1
s1=[]
e=[]
for i in range(len(d)):
s1.append(d[i][0])
e.append((d[i][1],i))
s=SegmentTree1(e)
an=0
for i in range(n+1,-1,-1):
b=count(s1,len(s1),i)
if b==0:
continue
w=s.query(0,b-1)
an+=min(i,w[0])
s.__setitem__(w[1],(0,w[1]))
print(ans,an)
``` | output | 1 | 69,912 | 9 | 139,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,913 | 9 | 139,826 |
Tags: greedy, sortings
Correct Solution:
```
from sys import stdin
for _ in range(int(stdin.readline())):
n=int(stdin.readline())
s=[0]*(n+1)
a=list(map(int, stdin.readline().split()))
x=0
for i in range(n):
s[a[i]]+=1
if s[a[i]]>x:
x=s[a[i]]
z=[0]*(x+1)
p=0
for i in range(1, n+1):
x=s[i]
if not z[x]:
p+=x
z[x]=1
else:
while x:
if not z[x]:
p+=x
z[x]=1
break
x-=1
print(p)
``` | output | 1 | 69,913 | 9 | 139,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,914 | 9 | 139,828 |
Tags: greedy, sortings
Correct Solution:
```
q = int(input())
R = lambda:map(int,input().split())
answers = []
def solve(a):
groupsNumber = dict()
for groupName in a:
if groupName in groupsNumber:
groupsNumber[groupName] += 1
else:
groupsNumber[groupName] = 1
values = list(groupsNumber.values())
values.sort(reverse=True)
sumResult = 0
lastAddedValue = values[0]
for i in range(0, len(values)):
if i == 0:
sumResult += lastAddedValue
continue
if values[i] >= lastAddedValue:
lastAddedValue -= 1
if lastAddedValue == 0:
return sumResult
sumResult += lastAddedValue
else:
lastAddedValue = values[i]
sumResult += lastAddedValue
return sumResult
for i in range(0, q):
n = R()
confets = list(R())
result = solve(confets)
answers.append(result)
[print(x) for x in answers]
``` | output | 1 | 69,914 | 9 | 139,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,915 | 9 | 139,830 |
Tags: greedy, sortings
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(n):
try:
if dic[a[i]]:
dic[a[i]]+=1
except KeyError:
dic[a[i]]=1
temp=[]
for i in dic:
temp.append(dic[i])
temp.sort(reverse=True)
last=10000000;res=0
for i in temp:
if i>=last:
if last-1<=0:
break
res+=(last-1)
last=last-1
else:
res+=i
last=i
print(res)
``` | output | 1 | 69,915 | 9 | 139,831 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,916 | 9 | 139,832 |
Tags: greedy, sortings
Correct Solution:
```
from collections import defaultdict
q = int(input())
for i in range(q):
n = int(input())
l = list(map(int,input().split()))
# l.sort()/
hash = defaultdict(int)
for i in l:
hash[i]+=1
la = []
for i in hash.keys():
la.append(hash[i])
la.sort(reverse=True)
ans= [la[0]]
prev = la[0]
for i in la[1:]:
z = ans[-1]
if i >= z:
if z-1>0:
ans.append(z-1)
else:
ans.append(i)
print(sum(ans))
``` | output | 1 | 69,916 | 9 | 139,833 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid. | instruction | 0 | 69,917 | 9 | 139,834 |
Tags: greedy, sortings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
from queue import Queue
import collections
import itertools
import bisect
import heapq
#sys.setrecursionlimit(100000)
#^^^TAKE CARE FOR MEMORY LIMIT^^^
import random
def main():
pass
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def binary(n):
return (bin(n).replace("0b", ""))
def decimal(s):
return (int(s, 2))
def pow2(n):
p = 0
while (n > 1):
n //= 2
p += 1
return (p)
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
l.append(i)
n = n / i
if n > 2:
l.append(int(n))
return (l)
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
def maxPrimeFactors(n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
def countcon(s, i):
c = 0
ch = s[i]
for i in range(i, len(s)):
if (s[i] == ch):
c += 1
else:
break
return (c)
def lis(arr):
n = len(arr)
lis = [1] * n
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
maximum = 0
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
def isSubSequence(str1, str2):
m = len(str1)
n = len(str2)
j = 0
i = 0
while j < m and i < n:
if str1[j] == str2[i]:
j = j + 1
i = i + 1
return j == m
def maxfac(n):
root = int(n ** 0.5)
for i in range(2, root + 1):
if (n % i == 0):
return (n // i)
return (n)
def p2(n):
c=0
while(n%2==0):
n//=2
c+=1
return c
def seive(n):
primes=[True]*(n+1)
primes[1]=primes[0]=False
i=2
while(i*i<=n):
if(primes[i]==True):
for j in range(i*i,n+1,i):
primes[j]=False
i+=1
pr=[]
for i in range(0,n+1):
if(primes[i]):
pr.append(i)
return pr
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def denofactinverse(n,m):
fac=1
for i in range(1,n+1):
fac=(fac*i)%m
return (pow(fac,m-2,m))
def numofact(n,m):
fac=1
for i in range(1,n+1):
fac=(fac*i)%m
return(fac)
def sod(n):
s=0
while(n>0):
s+=n%10
n//=10
return s
for xyz in range(0,int(input())):
n=int(input())
l=list(map(int,input().split()))
cnt=[0]*(max(l)+1)
for i in l:
cnt[i]+=1
cnt.sort(reverse=True)
cv=10**9
ans=0
for i in range(0,len(cnt)):
cv=min(cv-1,cnt[i])
ans+=cv
if(cv==0):
break
print(ans)
``` | output | 1 | 69,917 | 9 | 139,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
d = Counter(a)
s = sorted(d,key = lambda x:(-d[x]))
upper_limit = float("inf")
ans = 0
for i in s:
if upper_limit==float("inf"):
ans+=d[i]
upper_limit = d[i]
else:
if upper_limit<=1:
break
ans+=min(d[i],upper_limit-1)
upper_limit = min(d[i],upper_limit-1)
print(ans)
``` | instruction | 0 | 69,918 | 9 | 139,836 |
Yes | output | 1 | 69,918 | 9 | 139,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid.
Submitted Solution:
```
# -*- coding: utf-8 -*-
# @Date : 2019-06-27 10:37:07
# @Author : raj lath (oorja.halt@gmail.com)
# @Link : link
# @Version : 1.0.0
import sys
sys.setrecursionlimit(10**5+1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
nb_test = RI()
for _ in range(nb_test):
lens = RI()
vals = RMI()
cnts = [0] * lens
for i in vals:
cnts[i - 1] += 1
cnts.sort()
maxv = cnts[-1] + 1
answer = 0
for i in range(lens - 1, -1, -1):
if cnts[i] >= maxv: cnts[i] = maxv - 1
maxv = cnts[i]
answer += max(0, cnts[i])
print(answer)
``` | instruction | 0 | 69,919 | 9 | 139,838 |
Yes | output | 1 | 69,919 | 9 | 139,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid.
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
fre=[0]*n
for i in map(int,input().split()):
fre[i-1]+=1
ans=0
val=10**6
fre.sort(reverse=True)
for i in fre:
val=max(0,min(val-1,i))
ans+=val
print(ans)
``` | instruction | 0 | 69,920 | 9 | 139,840 |
Yes | output | 1 | 69,920 | 9 | 139,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid.
Submitted Solution:
```
from collections import *
t=int(input())
while(t):
t-=1
n=int(input())
a=list(map(int,input().split()))
r1=[0]*n
s=0
for i in a:
r1[i-1]+=1
r1.sort(reverse=True)
s=0
m=100000000000
for i in r1:
m=max(min(m-1,i),0)
s+=m
print(s)
``` | instruction | 0 | 69,921 | 9 | 139,842 |
Yes | output | 1 | 69,921 | 9 | 139,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is actually a subproblem of problem G from the same contest.
There are n candies in a candy box. The type of the i-th candy is a_i (1 β€ a_i β€ n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
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. Each query is represented by two lines.
The first line of each query contains one integer n (1 β€ n β€ 2 β
10^5) β the number of candies.
The second line of each query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the type of the i-th candy in the box.
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 size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.
Note that this is not the only possible solution β taking two candies of type 4 and one candy of type 6 is also valid.
Submitted Solution:
```
from collections import defaultdict
from math import sqrt
def solve(d, n):
s = 0
used = [False] * (n + 1)
for key in d:
value = d[key]
while used[value] and value > 0:
value -= 1
s += value
used[value] = True
return s
def go():
n = int(input())
d_all = defaultdict(int)
d_ones = defaultdict(int)
for i in range(n):
item, ok = map(int, input().split(' '))
if ok == 1:
d_ones[item] += 1
d_all[item] += 1
return f'{solve(d_all, n)} {solve(d_ones, n)}'
o = ''
Q = int(input())
for _ in range(Q):
o += str(go()) + '\n'
print(o)
``` | instruction | 0 | 69,922 | 9 | 139,844 |
No | output | 1 | 69,922 | 9 | 139,845 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.