text stringlengths 1.02k 43.5k | conversation_id int64 853 107k | embedding list | cluster int64 24 24 |
|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Tags: data structures, implementation
Correct Solution:
```
from sys import stdin
input = stdin.buffer.readline
t=int(input())
for i in range(t):
n=int(input())
arr=[int(x) for x in input().split()]
cnt=0
m=arr[n-1]
for i in range(n-2,-1,-1):
if arr[i]>m:
cnt=cnt+1
else:
m=arr[i]
print(cnt)
```
| 58,915 | [
0.22314453125,
0.30224609375,
0.1533203125,
0.1871337890625,
0.02349853515625,
-0.363037109375,
-0.14990234375,
-0.01059722900390625,
0.438720703125,
0.904296875,
0.76025390625,
0.145751953125,
0.0323486328125,
-0.76708984375,
-0.76904296875,
-0.2169189453125,
-0.640625,
-0.7167968... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Tags: data structures, implementation
Correct Solution:
```
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
mi=a[n-1]
j=n-2
ans=0
while j>-1:
if a[j]>mi:
ans+=1
elif a[j]<mi:
mi=a[j]
j=j-1
print(ans)
```
| 58,916 | [
0.2288818359375,
0.26953125,
0.1368408203125,
0.15478515625,
0.01390838623046875,
-0.385009765625,
-0.1400146484375,
-0.03167724609375,
0.459716796875,
0.8857421875,
0.74072265625,
0.150390625,
0.067138671875,
-0.73388671875,
-0.732421875,
-0.2176513671875,
-0.638671875,
-0.6655273... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Tags: data structures, implementation
Correct Solution:
```
t=int(input())
# print(t)
for i in range(t):
n=input()
arr = input()
arr = [int(a) for a in arr.split(" ")]
# print(arr)
min_price=arr[len(arr)-1]
count=0
if(len(arr)>1):
for a in arr[::-1][1:]:
if min_price<a:
count+=1
else:
min_price=a
print(count)
```
| 58,917 | [
0.2440185546875,
0.295166015625,
0.1285400390625,
0.1832275390625,
0.01904296875,
-0.3798828125,
-0.1302490234375,
-0.014862060546875,
0.445556640625,
0.88037109375,
0.7548828125,
0.12164306640625,
0.0350341796875,
-0.76806640625,
-0.80615234375,
-0.2491455078125,
-0.66552734375,
-... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Tags: data structures, implementation
Correct Solution:
```
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
l = l[::-1]
sm = l[0]
c = 0
for i in l:
if i > sm:
c += 1
sm = min(sm, i)
print(c)
```
| 58,918 | [
0.232666015625,
0.303955078125,
0.169677734375,
0.1824951171875,
-0.01277923583984375,
-0.36181640625,
-0.158935546875,
0.0059661865234375,
0.41650390625,
0.88525390625,
0.75927734375,
0.135498046875,
0.05322265625,
-0.73193359375,
-0.76318359375,
-0.21240234375,
-0.66015625,
-0.70... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Tags: data structures, implementation
Correct Solution:
```
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int,input().split()))
tmp = arr[-1]
ans = 0
j= n-2
while(j>=0):
if tmp <arr[j]:
ans += 1
else:
tmp = arr[j]
j -= 1
print(ans)
```
| 58,919 | [
0.2254638671875,
0.2783203125,
0.139892578125,
0.170654296875,
0.0256500244140625,
-0.38720703125,
-0.13623046875,
-0.024322509765625,
0.467041015625,
0.8828125,
0.76318359375,
0.1392822265625,
0.06341552734375,
-0.744140625,
-0.767578125,
-0.23046875,
-0.6494140625,
-0.67333984375... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Tags: data structures, implementation
Correct Solution:
```
user_input = int(input())
for i in range(user_input):
a = int(input())
b = list(map(int, input().split()))
count = 0
minimum = b[-1]
for j in range(a-2, -1, -1):
if b[j] > minimum:
count += 1
minimum = min(b[j], minimum)
print(count)
```
| 58,920 | [
0.2476806640625,
0.28173828125,
0.1407470703125,
0.2164306640625,
0.00310516357421875,
-0.387451171875,
-0.1156005859375,
0.00525665283203125,
0.4296875,
0.88720703125,
0.7822265625,
0.1177978515625,
0.03680419921875,
-0.748046875,
-0.79150390625,
-0.224365234375,
-0.66796875,
-0.6... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Tags: data structures, implementation
Correct Solution:
```
n = int(input())
for i in range(n):
a = int(input())
li = list(map(int,input().split()))
pivot = li[-1]
count = 0
for i in range(-2,-(len(li))-1,-1):
if pivot<li[i]:
count+=1
else:
pivot = li[i]
print(count)
```
| 58,921 | [
0.253662109375,
0.2440185546875,
0.09771728515625,
0.1531982421875,
0.05316162109375,
-0.391845703125,
-0.128662109375,
-0.0024700164794921875,
0.51416015625,
0.84033203125,
0.85888671875,
0.1031494140625,
0.08148193359375,
-0.68798828125,
-0.7412109375,
-0.1051025390625,
-0.61962890... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
t=int(input())
while(t>0):
n=int(input())
a=list(map(int,input().split()))
min=a[n-1]
ans=0
for i in range(n-1,-1,-1):
if(a[i]>min):
ans+=1
else:
min=a[i]
print(ans)
t-=1
```
Yes
| 58,922 | [
0.263427734375,
0.39208984375,
-0.039459228515625,
0.2066650390625,
-0.096923828125,
-0.2919921875,
-0.1705322265625,
0.06781005859375,
0.40478515625,
0.93359375,
0.734375,
0.2044677734375,
0.0122528076171875,
-0.73291015625,
-0.6923828125,
-0.19482421875,
-0.5693359375,
-0.7412109... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
A=list(map(int,input().split()))
c=0
r=1000000000000000000000000000000000000
for i in range(n-1,-1,-1):
if(A[i]>r):
c+=1
r=min(r,A[i]);
print(c)
```
Yes
| 58,923 | [
0.2666015625,
0.394287109375,
-0.056488037109375,
0.2315673828125,
-0.0771484375,
-0.299560546875,
-0.1556396484375,
0.0743408203125,
0.426513671875,
0.94140625,
0.73095703125,
0.20703125,
0.014404296875,
-0.75244140625,
-0.6806640625,
-0.1636962890625,
-0.560546875,
-0.7421875,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
c=0
arr=[int(x) for x in str(input()).split()]
mini = arr[-1]
for i in arr[::-1]:
#print(i)
if(mini>=i):
mini=i
else:
c+=1
print(c)
```
Yes
| 58,924 | [
0.270751953125,
0.39111328125,
-0.05718994140625,
0.22119140625,
-0.062286376953125,
-0.278076171875,
-0.1690673828125,
0.057769775390625,
0.44775390625,
0.8994140625,
0.73486328125,
0.1966552734375,
0.01076507568359375,
-0.724609375,
-0.66943359375,
-0.192626953125,
-0.57080078125,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
t = int(input())
for i in range(t) :
n = int(input())
a = list(map(int,input().strip().split()))[:n]
count = 0
mn = a[n-1]
for j in range(n-1,-1,-1) :
if a[j] > mn :
count += 1
mn = min(mn,a[j])
print(count)
```
Yes
| 58,925 | [
0.265625,
0.38671875,
-0.04705810546875,
0.2142333984375,
-0.086181640625,
-0.288330078125,
-0.1829833984375,
0.06787109375,
0.420166015625,
0.93505859375,
0.7294921875,
0.2138671875,
0.0098114013671875,
-0.724609375,
-0.67431640625,
-0.169189453125,
-0.55908203125,
-0.736328125,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
t = int(input())
for i in range(t):
n = int(input())
i = list(map(int, input().split()))
bd_count = 1
min_num = i[-1]
# last_index = len(i)-1
for day in i[::-1]:
if day < min_num:
bd_count += 1
# last_index -=1
min_num = day
print(len(i)-bd_count)
```
No
| 58,926 | [
0.2734375,
0.402587890625,
-0.05181884765625,
0.2213134765625,
-0.08941650390625,
-0.277099609375,
-0.1888427734375,
0.07525634765625,
0.43310546875,
0.93408203125,
0.7099609375,
0.2039794921875,
0.004047393798828125,
-0.72265625,
-0.7041015625,
-0.19287109375,
-0.56982421875,
-0.7... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
for _ in range(int(input())):
count = 0
i = 0
n = int(input())
a = list(map(int, input().split()))
while i != n:
i = a.index(max(a[i:]))
i += 1
count += 1
print(count)
```
No
| 58,927 | [
0.266845703125,
0.376953125,
-0.0438232421875,
0.2239990234375,
-0.0771484375,
-0.30126953125,
-0.1685791015625,
0.0950927734375,
0.423583984375,
0.93505859375,
0.755859375,
0.2080078125,
0.0141448974609375,
-0.71240234375,
-0.6796875,
-0.1834716796875,
-0.57373046875,
-0.709960937... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
import heapq
for i in range(int(input())):
N = int(input())
A = [int(_) for _ in input().split()]
count = 0
flag = True
l_i = -1
j = 0
S_A = sorted(A)
dic = {}
for p in range(N):
dic[A[p]] = p
while(flag):
value = S_A[j]
index = dic[value]
if index>l_i:
count += (index-l_i-1)
l_i = index
if index==N-1:
flag = False
j+=1
print(count)
```
No
| 58,928 | [
0.263671875,
0.336669921875,
-0.09515380859375,
0.308837890625,
-0.0261688232421875,
-0.251220703125,
-0.1627197265625,
-0.007366180419921875,
0.46337890625,
0.96630859375,
0.72509765625,
0.148681640625,
-0.01509857177734375,
-0.7587890625,
-0.693359375,
-0.1796875,
-0.5810546875,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a_1, a_2, ..., a_n, where a_i is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n=6 and a=[3, 9, 4, 6, 7, 5], then the number of days with a bad price is 3 — these are days 2 (a_2=9), 4 (a_4=6) and 5 (a_5=7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input
The first line contains an integer t (1 ≤ t ≤ 10000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.
Each input data set consists of two lines. The first line contains an integer n (1 ≤ n ≤ 150000) — the number of days. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6), where a_i is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output
Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.
Example
Input
5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5
Output
3
0
1
8
2
Submitted Solution:
```
t=int(input())
for i in range(t):
count=0
n=int(input())
a=input().split()
Min=a[n-1]
for j in range(n-2,-1,-1):
if a[j]>Min:
count+=1
elif a[j]<Min:
Min=a[j]
print(count)
```
No
| 58,929 | [
0.2646484375,
0.39306640625,
-0.0587158203125,
0.24267578125,
-0.097412109375,
-0.313232421875,
-0.16357421875,
0.07147216796875,
0.411865234375,
0.947265625,
0.7275390625,
0.208251953125,
-0.006626129150390625,
-0.72021484375,
-0.67578125,
-0.177978515625,
-0.5595703125,
-0.740722... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
def ans(p, h):
n = len(p)
for i in range(len(h)-len(p)+1):
if sorted(h[i:i+n]) == sorted(p):
return "YES"
return "NO"
for u in range(int(input())):
p=input()
h = input()
print(ans(p,h))
```
| 58,962 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
n = int(input())
for i in range(n):
a = list(input())
b = list(input())
dict_a = {}
dict_b = {}
l = len(a)
L = len(b)
yes = False
for j in range(l):
let = a[j]
dict_a.setdefault(let, 0)
dict_a[let] += 1
for j in range(L):
dict_b.clear()
if j + l <= L:
for k in range(j, j + l):
let = b[k]
dict_b.setdefault(let, 0)
dict_b[let] += 1
if set(dict_a.items()) == set(dict_b.items()):
yes = True
break
if yes == True:
print('YES')
else:
print('NO')
```
| 58,963 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
t=int(input())
for _ in range(t):
p=str(input())
h=str(input())
c={}
for i in p:
if i not in c:
c[i]=1
else:
c[i] += 1
ok=False
for i in range(0,len(h)-len(p)+1):
cc={}
k=h[i:i+len(p)]
for j in k:
if j not in cc:
cc[j]=1
else:
cc[j] += 1
if cc==c:
ok=True
if ok:
print("YES")
else:
print("NO")
```
| 58,964 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
def hashs(password,hashed):
IsHashedForm = "No"
password = sorted(password)
for i in range(len(hashed)):
for j in range(len(hashed),0,-1):
if sorted(hashed[i:j]) == password:
IsHashedForm = "Yes"
return IsHashedForm
return IsHashedForm
def main():
n = eval(input())
for i in range(n):
password = input()
hashed = input()
var = hashs(password,hashed)
print(var)
main()
```
| 58,965 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
t = int(input())
for i in range(t):
word = input()
wordhash = input()
b = True
for j in range(len(wordhash) - len(word) + 1):
a = True
test = wordhash[j:(j+len(word))]
for letter in word:
if word.count(letter) != test.count(letter):
a = False
if a:
print("YES")
b = False
break
if b: print("NO")
```
| 58,966 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
t=int(input())
for i in range(0,t):
p=input()
l=len(p)
h=input()
L=len(h)
flag=0
arr=[0 for x in range(0,27)]
for i in p:
n=ord(i)
arr[n-97]=arr[n-97]+1
if(L<l):
print('NO')
elif (L==l):
arr_2=[0 for x in range(0,27)]
for i in h:
n=ord(i)
arr_2[n-97]=arr_2[n-97]+1
if arr==arr_2:
print('YES')
else:
print('NO')
else:
for i in range(0,L-l+1):
test=h[0+i:l+i]
arr_2=[0 for x in range(0,27)]
for i in test:
n=ord(i)
arr_2[n-97]=arr_2[n-97]+1
if arr==arr_2:
print('YES')
flag=1
break
if flag==0:
print('NO')
```
| 58,967 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
t = int(input())
for _ in range(t):
p = sorted(input())
h = input()
for i in range(len(h)-len(p)+1):
if sorted(h[i:i+len(p)])==p:
print("YES")
break
else:
print("NO")
```
| 58,968 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Tags: brute force, implementation, strings
Correct Solution:
```
t = int(input())
for i in range(t):
p = input()
h = input()
f = True
for i in range(len(h) - len(p) + 1):
if sorted(h[i: i + len(p)]) == sorted(p):
print('YES')
f = False
break
if f:
print('NO')
```
| 58,969 | [
0.1788330078125,
0.07281494140625,
0.42138671875,
-0.276611328125,
-0.271240234375,
-0.199951171875,
-0.156005859375,
0.20751953125,
0.2435302734375,
0.50927734375,
0.634765625,
-0.2100830078125,
0.386474609375,
-1.060546875,
-0.90625,
-0.07073974609375,
-0.2322998046875,
-0.361572... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
from collections import Counter
for _ in range(int(input())):
p = input()
h = input()
cnt = Counter(h[:len(p)])
cnt1 = Counter(p)
diff = {k for k in cnt1.keys() if cnt[k] != cnt1[k]}
for i in range(len(h) - len(p)):
if not diff:
break
c = h[i]
cnt[c] -= 1
if cnt[c] == cnt1[c]:
diff.discard(c)
else:
diff.add(c)
c = h[i + len(p)]
cnt[c] += 1
if cnt[c] == cnt1[c]:
diff.discard(c)
else:
diff.add(c)
if diff:
print("NO")
else:
print("YES")
```
Yes
| 58,970 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
for _ in range(int(input())):
s=list(input())
h=list(input())
a,b=len(h),len(s)
if a<b:
print("NO")
elif a==b:
d={}
c=0
dd={}
for i in s:
d[i]=d.get(i,0)+1
for i in h:
dd[i]=dd.get(i,0)+1
for i in d:
if i not in dd:
c=1
break
if i in d:
if d[i]!=dd[i]:
c=1
break
if c:
print("NO")
else:
print("YES")
else:
x=sorted(s)
c=0
for i in range(a-b+1):
if sorted(h[i:i+b])==x:
c=1
break
if c:
print("YES")
else:
print("NO")
```
Yes
| 58,971 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
'''
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
'''
tt = int(input())
for ghg in range(tt):
word = str(input())
after = str(input())
dicts = {}
for i in word:
if i not in dicts:
dicts[i] = 1
else:
dicts[i] += 1
n = len(word)
n2 = len(after)
flag = 0
for i in range(n2):
tmp = {}
count = n
for j in range(i,n2):
if after[j] not in dicts:
break
j = after[j]
if j not in tmp:
tmp[j] = 1
count -= 1
else:
tmp[j] += 1
if tmp[j] > dicts[j]:
break
count -=1
if count == 0:
# print(tmp)
# print(dicts)
flag = 1
break
if flag == 1:
print("YES")
break
if flag == 0:
print("NO")
```
Yes
| 58,972 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
from collections import Counter
numTestCases = int(input())
answers = []
for test in range(numTestCases):
answer = "NO"
password = input()
passwordShuffled = input()
passwordLetterFrequency = Counter(password)
for i in range(len(passwordShuffled) - len(password) + 1):
if Counter(passwordShuffled[i : len(password) + i]) == passwordLetterFrequency:
answer = "YES"
break
answers.append(answer)
for answer in answers:
print(answer)
```
Yes
| 58,973 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
t = int(input())
ans = []
for i in range(t):
p = input()
h = input()
pw_length = len(h)
p_set = set(p)
right_lock = 0
left_lock = 0
right_index = 0
left_index = pw_length - 1
for j in range(pw_length):
if h[j] in p_set and right_lock == 0:
right_index = j
right_lock = 1
if h[pw_length - j - 1] in p_set and left_lock == 0:
left_index = pw_length - j - 1
left_lock = 1
if left_lock == 1 and right_lock == 1: break
f = h[right_index : left_index + 1]
dif = abs(len(f) - len(p))
if(len(f) < len(p)): ans.append("no"); continue
p_list = list(p)
p_list.sort()
lock = 0
for j in range(len(f) - dif):
a = list(f[j : len(f)])
a.sort()
if a == p_list: ans.append("yes"); lock = 1; break
if lock == 0: ans.append("no")
for i in range(t): print(ans[i])
```
No
| 58,974 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
def substr(ps,hash):
x=0
j=len(hash)-1
ps2=""
for i in range(len(hash)):
if hash[x] not in ps:
x+=1
if hash[j] not in ps:
j-=1
ps2=list(hash[x:j+1])
ps=list(ps)
ps2.sort()
ps.sort()
if ps2==ps:
print("YES")
else:
print("NO")
def runner():
val=input()
for i in range(int(val)):
ans=input()
an2=input()
substr(ans,an2)
runner()
```
No
| 58,975 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
def freq(test_str):
all_freq = {}
for i in test_str:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
return all_freq
def compare_freq(p_freq: dict, h_freq: dict):
ret_val = 0
if p_freq.keys() != h_freq.keys():
return -1
for key in p_freq.keys():
if p_freq[key] != h_freq[key]:
return -1
# if p_freq[key] < h_freq[key]:
# ret_val = 1
return ret_val
if __name__ == "__main__":
for t in range(int(input())):
password = input()
hashstr = input()
if len(password) > len(hashstr):
print('NO')
break
password_freq = freq(password)
hash_freq = freq(hashstr)
for i in range(len(password)):
try:
temp_h = hashstr[i:i + len(password)]
temp_freq = freq(temp_h)
if compare_freq(password_freq, temp_freq) == 0:
print('YES')
break
except :
print('NO')
break
else:
print('NO')
```
No
| 58,976 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
1. take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
2. generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
3. the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Submitted Solution:
```
print("""YES
YES
NO
YES
NO
""")
```
No
| 58,977 | [
0.203125,
0.130126953125,
0.348876953125,
-0.282470703125,
-0.3837890625,
-0.054168701171875,
-0.255859375,
0.259033203125,
0.10784912109375,
0.486328125,
0.52685546875,
-0.1419677734375,
0.27978515625,
-1.0517578125,
-0.91748046875,
-0.10809326171875,
-0.1981201171875,
-0.39892578... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
n = input()
c = {}
for x in [int(x) for x in input().split()]:
u, t = x, 0
while 0 == u%3:
u //= 3
t += 1
c[t] = c.get(t, []) + [x]
for x in sorted(c)[::-1]:
for y in sorted(c[x]):
print(y, end=' ')
```
| 59,399 | [
0.39501953125,
-0.08355712890625,
-0.047637939453125,
-0.05194091796875,
-0.1485595703125,
-0.452392578125,
-0.6396484375,
0.10198974609375,
0.24658203125,
0.95751953125,
1.0693359375,
-0.09259033203125,
0.12103271484375,
-0.78515625,
-0.499755859375,
-0.1260986328125,
-0.658203125,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
input()
v = list(map(int,input().split()))
# v = [2999999999999999997 ,999999999999999999]
# # v = [9, 8, 6, 3, 12, 4]
def find_last_position(li):
li = []
for i in v:
if (i % 3 == 0) and (i // 3 in v):
li.append(i)
elif (i * 2 in v):
li.append(i)
for i in v:
if i not in li:
return i
def find_path(li):
v_new = []
e = find_last_position(li)
v_new.append(e)
li.remove(e)
while True:
if (e*3 in li):
e = e*3
v_new.append(e)
li.remove(e)
elif(e>>1 in li):
e = e>>1
v_new.append(e)
li.remove(e)
else:break
return v_new
for i in reversed(find_path(v)):
print(i,end=' ')
```
| 59,400 | [
0.385498046875,
-0.08111572265625,
-0.06903076171875,
-0.058624267578125,
-0.1641845703125,
-0.44482421875,
-0.640625,
0.1090087890625,
0.2427978515625,
0.94775390625,
1.0546875,
-0.12078857421875,
0.1441650390625,
-0.78076171875,
-0.501953125,
-0.134033203125,
-0.6591796875,
-0.93... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
f = int(input())
c = input().split()
d = []
e = 0
for i in c:
d.append(int(i))
# This first for loop gets the first element in the ordered list
for i in range(f):
check = 0
# in this next loop, we find the unique element which is not divisible by 2
# and 2 * any other element is equal to it, and you can't multiple it by 3 to get
# any other element
for j in range(f):
if d[j] == 3 * d[i]:
check = 1
if d[i] % 2 == 0 and d[i] == 2 * d[j]:
check = 1
if check == 0:
e = d[i]
# This next for loop goes through and finds all remaining elements
print(e, end = ' ')
for i in range(f-1):
# f-1 because we already found one element
for j in range(f):
# since we know we have the correct first element, our next element is e/3
if 3*d[j] == e:
e = d[j]
break
# to find the element to multiply by 2, we update e and look for e*2 in our series
elif 2*e == d[j]:
e = d[j]
break
print(e, end = ' ')
```
| 59,401 | [
0.385009765625,
-0.0859375,
-0.06732177734375,
-0.049560546875,
-0.1673583984375,
-0.43505859375,
-0.6396484375,
0.11309814453125,
0.235107421875,
0.93701171875,
1.076171875,
-0.11090087890625,
0.1417236328125,
-0.767578125,
-0.53955078125,
-0.11895751953125,
-0.6845703125,
-0.9482... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
# _
#####################################################################################################################
def main():
n, sequence = input(), set(map(int, input().split()))
firstElement = nThrees = 0
for element in sequence:
value = element
for i in range(39):
if value%3:
if nThrees < i:
firstElement, nThrees = element, i
break
value //= 3
if not nThrees:
return ' '.join(map(str, sorted(sequence)))
return ' '.join(map(str, rearrangeElements(sequence, firstElement)))
def rearrangeElements(sequence, firstElement):
newSequence = []
if not firstElement%2:
elementBeforeFirst = firstElement//2
while elementBeforeFirst in sequence:
newSequence.append(elementBeforeFirst)
elementBeforeFirst //= 2
newSequence.reverse()
while True:
if firstElement not in sequence:
return newSequence
while firstElement in sequence:
newSequence.append(firstElement)
firstElement += firstElement
firstElement //= 6
if __name__ == '__main__':
print(main())
# main()
```
| 59,402 | [
0.4052734375,
-0.09295654296875,
-0.0693359375,
-0.01103973388671875,
-0.159912109375,
-0.461669921875,
-0.62890625,
0.1221923828125,
0.247802734375,
0.943359375,
1.072265625,
-0.10003662109375,
0.141845703125,
-0.77392578125,
-0.5087890625,
-0.11962890625,
-0.650390625,
-0.9433593... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
z,zz,dgraphs=input,lambda:list(map(int,z().split())),{}
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
p=ceil(x**.5)+1
for i in range(2,p):
if x%i==0:return 0
return 1
def dfs(u,visit,graph):
visit[u]=True
for i in graph[u]:
if not visit[i]:
dfs(i,visit,graph)
################################################################################
"""
led=(6,2,5,5,4,5,6,3,7,6)
vowel={'a':0,'e':0,'i':0,'o':0,'u':0}
color4=["G", "GB", "YGB", "YGBI", "OYGBI" ,"OYGBIV",'ROYGBIV' ]
z,zz,dgraphs=input,lambda:list(map(int,z().split())),{}
d,l,r,b=[],0,1,{}
for _ in range(int(z())):d.append(z().split())
for i in d:
id_ =int(i[1])
if i[0]=='L':
b[id_]=l
l-=1
if i[0]=='R':
b[id_]=r
r+=1
if i[0]=='?':
p = b[id_]
print(min(p-l-1,r-p-1))
"""
###########################---START-CODING---####################################
num=int(z())
l=zz()
for i in sorted(l)[::-1]:
p=[i]
for _ in range(num):
if i%3==0 and i//3 in l:
p.append(i//3)
i=i//3
elif i*2 in l:
p.append(i*2)
i=i*2
else:break
if sorted(p)==sorted(l):
break
print(*p)
```
| 59,403 | [
0.3955078125,
-0.07733154296875,
-0.05615234375,
-0.0291595458984375,
-0.158935546875,
-0.453125,
-0.634765625,
0.104248046875,
0.2459716796875,
0.9443359375,
1.0537109375,
-0.10675048828125,
0.1402587890625,
-0.76806640625,
-0.51025390625,
-0.12445068359375,
-0.67236328125,
-0.925... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
from collections import Counter,defaultdict,deque
from math import factorial as fact
def primes(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
def count3(x):
c = 0
while x%3==0:
x//=3
c+=1
return c
def k(x):
return count3(x)*10**19-x
n = int(input())
a = [int(x) for x in input().split()]
a.sort(key =k,reverse=True)
print(*a)
#n,m = [int(x) for x in input().split()]
```
| 59,404 | [
0.371337890625,
-0.098388671875,
-0.048095703125,
-0.051513671875,
-0.1534423828125,
-0.4306640625,
-0.62109375,
0.10540771484375,
0.2666015625,
0.939453125,
1.0673828125,
-0.135986328125,
0.1488037109375,
-0.767578125,
-0.5126953125,
-0.130859375,
-0.64892578125,
-0.91748046875,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
import math
from decimal import *
import random
from datetime import datetime
import time
def k(n):
ans = 0
while(n%3==0):
ans+=1
n//=3
return ans
n = int(input())
arr = list(map(int, input().split()))
ans = []
arr = sorted(arr, key = lambda x: (-k(x), x))
for i in range(n):
arr[i] = str(arr[i])
print(' '.join(arr))
```
| 59,405 | [
0.38134765625,
-0.09259033203125,
-0.043182373046875,
-0.072021484375,
-0.1676025390625,
-0.43408203125,
-0.611328125,
0.1097412109375,
0.2484130859375,
0.9853515625,
1.046875,
-0.1607666015625,
0.145263671875,
-0.74560546875,
-0.52587890625,
-0.09722900390625,
-0.6474609375,
-0.92... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Tags: dfs and similar, math, sortings
Correct Solution:
```
n = int(input())
nums = list(map(int,input().split()))
nums.sort()
ans = []
for i in range(n):
aux = nums[:]
a = [nums[i]]
j = 0
while a[j]//3 in aux or a[j]*2 in aux:
b = a[j]//3
c = a[j]*2
if c in aux:
a.append(c)
aux.remove(c)
elif a[j] == 4:
break
else:
a.append(b)
aux.remove(b)
j += 1
ans.append(a)
ans.sort(key = len)
for item in ans[-1]:
print(item, end =' ')
```
| 59,406 | [
0.405029296875,
-0.07818603515625,
-0.0853271484375,
-0.054229736328125,
-0.1751708984375,
-0.446044921875,
-0.625,
0.10040283203125,
0.247802734375,
0.94677734375,
1.0703125,
-0.10870361328125,
0.1199951171875,
-0.783203125,
-0.5263671875,
-0.111572265625,
-0.642578125,
-0.9501953... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
import sys, os
# import numpy as np
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))
# read_f = lambda file: list(map(int, file.readline().strip().split()))
# from time import time
# sys.setrecursionlimit(5*10**6)
MOD = 10**9 + 7
def main():
# file1 = open("C:\\Users\\shank\\Desktop\\Comp_Code\\input.txt", "r")
# n = int(file1.readline().strip());
# arr = list(map(int, file1.read().strip().split(" ")))
# file1.close()
n = int(input()); arr = read()
curr = arr[0]
i = 0
while True:
# print(curr)
if curr*3 in arr:curr *= 3
elif curr%2 == 0 and curr//2 in arr:curr //= 2
else:break
i += 1
if i > 100:break
ans = []; i =0
while True:
ans.append(curr)
# print(ans, curr)
if curr%3 == 0 and curr//3 in arr:curr //= 3
elif curr*2 in arr:curr *= 2
else:break
i += 1
if i > 100:break
print(*ans)
# file = open("output.txt", "w")
# file.write(ans+"\n")
# file.close()
if __name__ == "__main__":
main()
"""
1 3 2 X
2 3 1 X
1 2 3 *
dp[1, n-1][1, n] =
for i in range(1, n+1):dp[0][i] = 1
for i in range(1, n):
if arr[i] == "<":
for j in range(1, n+1):
for k in range(1, j):
dp[i][j] += dp[i-1][k]
else:
for j in range(1, n+1):
for k in range(j+1, n+1):
dp[i][j] += dp[i-1][k]
"""
```
Yes
| 59,407 | [
0.412841796875,
0.038970947265625,
-0.1434326171875,
0.025390625,
-0.27978515625,
-0.36962890625,
-0.68310546875,
0.1937255859375,
0.193603515625,
0.9794921875,
0.97314453125,
-0.082275390625,
0.0082244873046875,
-0.8310546875,
-0.45654296875,
-0.1143798828125,
-0.56591796875,
-0.9... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input())
S = list(map(int,input().split()))
def neighbours(node,s=S):
out = [node<<1]
if node % 3 == 0:
out.append(node//3)
return set(x for x in out if x in s)
G = {}
for x in S:
G[x] = neighbours(x)
def hamilton(G, size, pt, path=None):
if path == None:
path = []
if pt not in set(path):
path.append(pt)
if len(path)==size:
return path
for pt_next in G.get(pt, []):
res_path = [i for i in path]
candidate = hamilton(G, size, pt_next, res_path)
if candidate is not None:
return candidate
for x in S:
P = hamilton(G,N,x)
if P:
print(" ".join(map(str,P)))
break
```
Yes
| 59,408 | [
0.3984375,
0.034027099609375,
-0.1287841796875,
0.0217437744140625,
-0.297119140625,
-0.36767578125,
-0.69873046875,
0.2020263671875,
0.1651611328125,
0.98828125,
0.95654296875,
-0.0914306640625,
0.0218048095703125,
-0.84912109375,
-0.466064453125,
-0.1162109375,
-0.56982421875,
-0... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
import sys
from collections import defaultdict
input = sys.stdin.readline
def main():
n = int(input())
a = list(map(int, input().split()))
for x in a:
d = defaultdict(lambda: 0)
for y in a:
d[y] += 1
bad = False
ans = [x]
for _ in range(n - 1):
if d[2 * x] > 0:
d[2 * x] -= 1
x *= 2
elif x % 3 == 0 and d[x // 3] > 0:
d[x // 3] -= 1
x //= 3
else:
bad = True
break
ans.append(x)
if not bad:
print(" ".join(map(str, ans)))
return
assert(0)
main()
```
Yes
| 59,409 | [
0.407470703125,
0.0372314453125,
-0.141845703125,
0.007335662841796875,
-0.287841796875,
-0.357421875,
-0.66162109375,
0.2027587890625,
0.198486328125,
0.97021484375,
0.97412109375,
-0.09857177734375,
0.016876220703125,
-0.841796875,
-0.470703125,
-0.1322021484375,
-0.5849609375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
from collections import deque
n = int(input())
nums = [int(x) for x in input().split(" ")]
nums.sort()
answer = deque()
answer.append(nums.pop())
k = 0
while len(nums) != 0:
nowLeft = answer[0]
nowRight = answer[len(answer) - 1]
swap_pos = 0
left = 0
for i, num in enumerate(nums):
if nowLeft == num * 2 or (num % 3 == 0 and num // 3 == nowLeft):
swap_pos = i
left = 1
break
if nowRight * 2 == num or (nowRight % 3 == 0 and num == nowRight // 3):
swap_pos = i
left = -1
break
if left == 1:
answer.appendleft(nums[swap_pos])
nums[swap_pos], nums[len(nums) - 1] = nums[len(nums) - 1], nums[swap_pos]
nums.pop()
elif left == -1:
answer.append(nums[swap_pos])
nums[swap_pos], nums[len(nums) - 1] = nums[len(nums) - 1], nums[swap_pos]
nums.pop()
for num in answer:
print(num, end=" ")
```
Yes
| 59,410 | [
0.389404296875,
0.0819091796875,
-0.1475830078125,
0.00798797607421875,
-0.284912109375,
-0.314697265625,
-0.63916015625,
0.16650390625,
0.2568359375,
0.98681640625,
0.9765625,
-0.052398681640625,
0.017547607421875,
-0.82763671875,
-0.486083984375,
-0.109375,
-0.5595703125,
-0.8486... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
def found_sequence(possible_sequence, lookup):
# print(f'possible_sequence: {possible_sequence}')
if len(possible_sequence) == n:
print(" ".join(list(map(str, possible_sequence))))
return True
else:
last_element = possible_sequence[-1]
if last_element//3 in lookup and lookup[last_element//3] >= 1:
lookup[last_element//3] -= 1
possible_sequence.append(last_element//3)
if found_sequence(possible_sequence, lookup):
return True
if last_element*2 in lookup and lookup[last_element*2] >= 1:
lookup[last_element*2] -= 1
possible_sequence.append(last_element*2)
if found_sequence(possible_sequence, lookup):
return True
return False
if __name__ == '__main__':
n = int(input())
given_sequence = list(map(int, input().split()))
lookup = {}
result = []
for element in given_sequence:
if element in lookup:
lookup[element] += 1
else:
lookup[element] = 1
# print(f'lookup: {lookup}')
for index in range(len(given_sequence)):
if found_sequence([given_sequence[index]], dict(lookup)):
break
```
No
| 59,411 | [
0.397705078125,
0.02349853515625,
-0.138671875,
0.0224456787109375,
-0.3173828125,
-0.379150390625,
-0.6767578125,
0.26806640625,
0.2274169921875,
1.0029296875,
0.9921875,
-0.09466552734375,
0.0100860595703125,
-0.82666015625,
-0.467041015625,
-0.10809326171875,
-0.556640625,
-0.86... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
n = int(input())
nums = list(map(int,input().split()))
ans = []
for i in range(n):
aux = nums[:]
a = [nums[i]]
j = 0
while a[j]//3 in aux or a[j]*2 in aux:
b = a[j]/3
c = a[j]*2
if c in aux:
a.append(c)
aux.remove(c)
elif b in aux:
a.append(int(b))
aux.remove(int(b))
else:
break
j += 1
ans.append(a)
ans.sort(key = len)
for item in ans[-1]:
print(item, end =' ')
```
No
| 59,412 | [
0.427001953125,
0.031494140625,
-0.158935546875,
0.03228759765625,
-0.299560546875,
-0.384521484375,
-0.6533203125,
0.212890625,
0.1822509765625,
0.9697265625,
0.986328125,
-0.053680419921875,
0.00836944580078125,
-0.85009765625,
-0.46826171875,
-0.12115478515625,
-0.58056640625,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
n=int(input())
a=[int (a) for a in input().split()]
ans=[]
while (a):
for i in a:
if ((i/2 not in a) and (i*3 not in a)):
ans.append(i)
a.remove(i)
print (ans)
```
No
| 59,413 | [
0.4287109375,
0.0469970703125,
-0.1707763671875,
0.02069091796875,
-0.2890625,
-0.380126953125,
-0.6435546875,
0.22314453125,
0.1798095703125,
0.95263671875,
0.9814453125,
-0.06573486328125,
0.0117034912109375,
-0.85400390625,
-0.46875,
-0.128173828125,
-0.587890625,
-0.904296875,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n - 1 operations of the two kinds:
* divide the number x by 3 (x must be divisible by 3);
* multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2 ≤ n ≤ 100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 3 ⋅ 10^{18}) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8
Input
4
42 28 84 126
Output
126 42 84 28
Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9, 3, 6, 12, 4, 8]. It can match possible Polycarp's game which started with x = 9.
Submitted Solution:
```
n = int(input())
l = sorted([int(x) for x in input().split()])
m = l[-1]
a = []
b = []
def solve_before(l, m, k):
if k > m or k in b:
return
b.append(k)
if k % 2 == 0:
solve_before(1, m, k // 2)
else:
solve_before(1, m, k * 3)
return
def solve_after(l, m, k):
if k > m or k in a:
return False
a.append(k)
if k % 3 == 0:
solve_after(l, m, k // 3)
else:
solve_after(l, m, k * 2)
return True
solve_before(l, m, m)
solve_after(l, m, m)
ans = list(reversed(b)) + a[1:]
print(" ".join(str(x) for x in ans))
```
No
| 59,414 | [
0.42333984375,
0.046478271484375,
-0.177734375,
0.022125244140625,
-0.288330078125,
-0.377685546875,
-0.6435546875,
0.23095703125,
0.179931640625,
0.95166015625,
0.98095703125,
-0.06475830078125,
0.01059722900390625,
-0.85693359375,
-0.471435546875,
-0.12322998046875,
-0.58984375,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
def main():
d={}
nums=[None]*31
for i in range(31):
nums[i]=2**i
d[nums[i]]=0
n,q=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
d[a[i]]+=1
for i in range(31):
if d[nums[i]]==0:
del d[nums[i]]
keys=sorted(list(d.keys()))[::-1]
leng=len(keys)
ans=[0]*q
for i in range(q):
val=int(input())
for j in range(leng):
if keys[j]<=val:
coin=min(d[keys[j]],val//keys[j])
ans[i]+=coin
val-=keys[j]*coin
if val==0:
break
else:
ans[i]=-1
print('\n'.join(list(map(str,ans))))
main()
```
| 59,718 | [
0.383056640625,
0.346435546875,
0.1947021484375,
0.13134765625,
-0.7294921875,
-0.066162109375,
-0.1436767578125,
0.04656982421875,
0.296142578125,
0.69091796875,
0.716796875,
-0.06732177734375,
-0.09295654296875,
-0.6513671875,
-0.736328125,
0.226806640625,
-0.55419921875,
-0.6318... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
# aadiupadhyay
import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')
def st(): return list(sys.stdin.readline().strip())
def li(): return list(map(int, sys.stdin.readline().split()))
def mp(): return map(int, sys.stdin.readline().split())
def inp(): return int(sys.stdin.readline())
def pr(n): return sys.stdout.write(str(n)+"\n")
def prl(n): return sys.stdout.write(str(n)+" ")
if os.path.exists('input.txt'):
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
# python not passed
def solve():
n, m = mp()
ans = []
l = li()
d = Counter(l)
for i in range(m):
x = inp()
cur = 30
now = 0
while cur >= 0 and x > 0:
val = 1 << cur
have = d[val]
want = x//val
mi = min(have, want)
x -= mi * val
now += mi
cur -= 1
if x == 0:
ans.append(now)
else:
ans.append(-1)
print(*ans, sep='\n')
for _ in range(1):
solve()
```
| 59,719 | [
0.426025390625,
0.279541015625,
0.267333984375,
0.130126953125,
-0.75634765625,
0.0180816650390625,
-0.11083984375,
-0.0235137939453125,
0.290771484375,
0.70556640625,
0.67578125,
-0.08050537109375,
-0.06964111328125,
-0.71142578125,
-0.72021484375,
0.264892578125,
-0.5380859375,
-... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
#####################################
import atexit, io, sys, collections, math, heapq, fractions,copy, os, functools
import sys
import random
import collections
from io import BytesIO, IOBase
##################################### python 3 START
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")
##################################### python 3 END
n,nq = map(int, input().split())
ais = list(map(int, input().split()))
s = list(set(ais))
s.sort(key = lambda x:-x)
cc = collections.Counter(ais)
def f(s,cc ,q):
ans = 0
for ai in s:
if q >= ai:
t = min(cc[ai], q // ai)
q-= t*ai
ans +=t
if q == 0:
break
return ans if q == 0 else -1
for _ in range(nq):
q = int(input())
print (f(s,cc, q))
```
| 59,720 | [
0.367431640625,
0.295166015625,
0.2001953125,
0.201904296875,
-0.7900390625,
-0.045318603515625,
-0.178466796875,
-0.0301361083984375,
0.3642578125,
0.642578125,
0.62451171875,
-0.094970703125,
-0.0946044921875,
-0.63037109375,
-0.76708984375,
0.284912109375,
-0.583984375,
-0.64355... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
from sys import stdout, stdin, setrecursionlimit
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from re import *
from os import *
####################################---fast-input-output----#########################################
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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
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:
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")
stdin, stdout = IOWrapper(stdin), IOWrapper(stdout)
def fast(): return stdin.readline().strip()
def zzz(): return [int(i) for i in fast().split()]
z, zz = fast, lambda: (map(int, z().split()))
szz, graph, mod, szzz = lambda: sorted(
zz()), {}, 10**9 + 7, lambda: sorted(zzz())
def lcd(xnum1, xnum2): return (xnum1 * xnum2 // gcd(xnum1, xnum2))
def output(answer, end='\n'): stdout.write(str(answer) + end)
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
#################################################---Some Rule For Me To Follow---#################################
"""
--instants of Reading problem continuously try to understand them.
--If you Know some-one , Then you probably don't know him !
--Try & again try
"""
##################################################---START-CODING---###############################################
n,k = zzz()
arr = zzz()
lst = [0]*31
for i in arr:
lst[int(log2(i))]+=1
# print(lst)
for i in range(k):
x=int(z())
ans=0
for j in range(30,-1,-1):
cnt=min(x>>j,lst[j])
ans+=cnt
x-=cnt<<j
if x==0:
output(ans)
else:
output(-1)
```
| 59,721 | [
0.3583984375,
0.30078125,
0.22802734375,
0.22216796875,
-0.78076171875,
-0.0650634765625,
-0.1905517578125,
-0.1527099609375,
0.314453125,
0.70458984375,
0.59716796875,
-0.1092529296875,
-0.037872314453125,
-0.732421875,
-0.72607421875,
0.2509765625,
-0.5830078125,
-0.75634765625,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
import sys
input=sys.stdin.readline
from collections import defaultdict
n,q=list(map(int,input().split()))
a=list(map(int,input().split()))
b=defaultdict(int)
for i in range(n):
b[a[i]]+=1
c=[]
for i in b:
c.append([i,b[i]])
c.sort(reverse=True)
t=len(c)
for i in range(q):
b=int(input())
d=0
for j in range(t):
d+=min(b//c[j][0],c[j][1])
b=b-c[j][0]*min(b//c[j][0],c[j][1])
if b==0:
print(d)
else:print(-1)
```
| 59,722 | [
0.33544921875,
0.3291015625,
0.271240234375,
0.132080078125,
-0.767578125,
-0.025634765625,
-0.1990966796875,
0.0012693405151367188,
0.277587890625,
0.7119140625,
0.66748046875,
-0.14599609375,
-0.09814453125,
-0.63720703125,
-0.787109375,
0.2265625,
-0.5244140625,
-0.62548828125,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
# @oj: codeforces
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-10-14 16:44
# @url:https://codeforc.es/contest/1003/problem/D
import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio
BUFSIZE = 8192
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")
# ------------------------------
## 注意嵌套括号!!!!!!
## 先有思路,再写代码,别着急!!!
## 先有朴素解法,不要有思维定式,试着换思路解决
## 精度 print("%.10f" % ans)
def main():
n,q=map(int,input().split())
a=list(map(int,input().split()))
d=collections.Counter(a)
keys=sorted(d.keys(),reverse=True)
# print (keys,d)
for i in range(q):
ans=0
b=int(input())
for k in keys:
cnt=b//k
ans+=min(cnt,d[k])
b-=min(cnt,d[k])*k
# if cnt<=d[k]:
# ans+=cnt
# b-=cnt*k
# else:
# ans+=d[k]
# b-=d[k]*k
if b>0:
print (-1)
else:
print (ans)
if __name__ == "__main__":
main()
```
| 59,723 | [
0.451416015625,
0.27587890625,
0.15966796875,
0.25146484375,
-0.75830078125,
-0.08477783203125,
-0.1358642578125,
-0.039764404296875,
0.31689453125,
0.66552734375,
0.6435546875,
-0.1259765625,
-0.09954833984375,
-0.6728515625,
-0.73681640625,
0.2147216796875,
-0.544921875,
-0.58056... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
n,q=map(int,input().split())
a=list(map(int,input().split()))
li=[0]*32
for i in a:
li[i.bit_length()-1]+=1
#print(li)
queries=[int(input()) for _ in range(q)]
for k in queries:
ans=0
for i in range(31,-1,-1):
mini=min(k>>i,li[i])
ans+=mini
k-=mini<<i
ans=ans if k==0 else -1
print(ans)
```
| 59,724 | [
0.360107421875,
0.359130859375,
0.2119140625,
0.11865234375,
-0.720703125,
-0.0894775390625,
-0.1405029296875,
0.0433349609375,
0.292724609375,
0.69091796875,
0.70068359375,
-0.0709228515625,
-0.06390380859375,
-0.6650390625,
-0.72900390625,
0.2484130859375,
-0.5234375,
-0.62792968... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Tags: greedy
Correct Solution:
```
# aadiupadhyay
import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')
def st(): return list(sys.stdin.readline().strip())
def li(): return list(map(int, sys.stdin.readline().split()))
def mp(): return map(int, sys.stdin.readline().split())
def inp(): return int(sys.stdin.readline())
def pr(n): return sys.stdout.write(str(n)+"\n")
def prl(n): return sys.stdout.write(str(n)+" ")
if os.path.exists('input.txt'):
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def solve():
n, m = mp()
ans = []
l = li()
d = Counter(l)
for i in range(m):
x = inp()
cur = 30
now = 0
while cur >= 0 and x > 0:
val = 1 << cur
have = d[val]
want = x//val
mi = min(have, want)
x -= mi * val
now += mi
cur -= 1
if x == 0:
ans.append(now)
else:
ans.append(-1)
print(*ans, sep='\n')
for _ in range(1):
solve()
```
| 59,725 | [
0.426025390625,
0.279541015625,
0.267333984375,
0.130126953125,
-0.75634765625,
0.0180816650390625,
-0.11083984375,
-0.0235137939453125,
0.290771484375,
0.70556640625,
0.67578125,
-0.08050537109375,
-0.06964111328125,
-0.71142578125,
-0.72021484375,
0.264892578125,
-0.5380859375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
#!/usr/bin/env python3
# encoding: utf-8
#----------
# Constants
#----------
DEGREE_ARRAY_SIZE = 32
#----------
# Functions
#----------
def convert(a):
from collections import Counter
b = [ 0 for i in range(DEGREE_ARRAY_SIZE) ]
for val, cnt in Counter(a).items():
b[val.bit_length()-1] += cnt
start = 0
for i, cnt in enumerate(reversed(b)):
if cnt != 0:
start = DEGREE_ARRAY_SIZE - i
break
return b, start
def calc(q, b):
ans = 0
val_power = (len(b) - 1)
for cnt in reversed(b):
c = min(cnt, q >> val_power)
q -= c * (1 << val_power)
ans += c
#if q == 0:
# break
val_power -= 1
return ans if q == 0 else -1
# Reads a string from stdin, splits it by space chars, converts each
# substring to int, adds it to a list and returns the list as a result.
def get_ints():
return [ int(n) for n in input().split() ]
# Reads a string from stdin, splits it by space chars, converts each substring
# to floating point number, adds it to a list and returns the list as a result.
def get_floats():
return [ float(n) for n in input().split() ]
#----------
# Execution start point
#----------
if __name__ == "__main__":
a = get_ints()
assert len(a) == 2
n, q = a[0], a[1]
a = get_ints()
assert len(a) == n
b, start = convert(a)
# print(str(b))
# print(total)
b = b[:start]
DEGREE_ARRAY_SIZE = start
# print(str(b))
q = [int(input()) for _ in range(q)]
for i in q:
ans = calc(i, b)
print(ans)
```
Yes
| 59,726 | [
0.447265625,
0.232666015625,
0.18310546875,
0.06927490234375,
-0.79931640625,
0.033721923828125,
-0.219482421875,
0.1475830078125,
0.260009765625,
0.6806640625,
0.64453125,
-0.03466796875,
-0.082763671875,
-0.60546875,
-0.740234375,
0.17041015625,
-0.56640625,
-0.60791015625,
-0.... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque
'''D Coins and Queries'''
def main():
n,q=rm()
a=rl()
b=[]
for i in range(q):
b.append(r())
coins=defaultdict(int)
for i in a:
coins[i]+=1
for i in b:
ans=0
if coins[i]>0:
print(1)
continue
for j in range(30,-1,-1):
temp=min(i//(2**j),coins[2**j])
i-=temp*(2**j)
ans+=temp
if i<=0:
break
if i>0:
print(-1)
else:
print(ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
def r():
return int(input())
def rm():
return map(int,input().split())
def rl():
return list(map(int,input().split()))
main()
```
Yes
| 59,727 | [
0.4599609375,
0.241943359375,
0.1453857421875,
0.06829833984375,
-0.7529296875,
0.1070556640625,
-0.1949462890625,
0.0267486572265625,
0.27783203125,
0.7255859375,
0.64794921875,
-0.08831787109375,
-0.1123046875,
-0.60107421875,
-0.7548828125,
0.185791015625,
-0.54052734375,
-0.597... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
import sys
from math import log
input = sys.stdin.readline
n,q = map(int,input().split())
coins = list(map(int,input().split()))
freq = [0 for i in range(32)]
for coin in coins: freq[int(log(coin, 2))]+=1
for j in range(q):
b = int(input())
c = 0
for i in range(31, -1, -1):
k = b//2**i
b -= min(k, freq[i])*(2**i)
c += min(k, freq[i])
if b: print(-1)
else: print(c)
```
Yes
| 59,728 | [
0.468505859375,
0.316162109375,
0.152099609375,
0.09698486328125,
-0.74853515625,
0.08819580078125,
-0.30810546875,
0.0855712890625,
0.23974609375,
0.71875,
0.6865234375,
-0.0285797119140625,
-0.0848388671875,
-0.66650390625,
-0.7490234375,
0.179931640625,
-0.51806640625,
-0.620117... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import math
def function(z):
x=int(math.log(z,2))
for i in range(x,-1,-1):
if lis[i]>0:
a=power[i]
b=z//a
c=min(lis[i],b)
lis[i]-=c
z-=(a*c)
arr[0]+=c
if z==0:
return True
return False
n,q=list(map(int,input().split()))
arr=list(map(int,input().split()))
power=[]
for i in range(32):
power.append(2**i)
coins=[0]*(32)
for i in range(n):
temp=int(math.log(arr[i],2))
coins[temp]+=1
for i in range(q):
lis=[]
for j in range(32):
lis.append(coins[j])
b=int(input())
a=bin(b)[2:]
l=len(a)
num=[]
p=0
for j in range(l):
if a[j]=="1":
num.append(l-j-1)
p+=1
arr=[0]
s=0
for j in range(p):
if function(power[num[j]]):
continue
else:
s+=1
break
if s==1:
print(-1)
else:
print(arr[0])
```
Yes
| 59,729 | [
0.43505859375,
0.2724609375,
0.19580078125,
0.0643310546875,
-0.79443359375,
0.0548095703125,
-0.253662109375,
0.0999755859375,
0.256103515625,
0.7080078125,
0.6748046875,
-0.045166015625,
-0.1190185546875,
-0.60498046875,
-0.73681640625,
0.1597900390625,
-0.57177734375,
-0.5502929... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
#!/usr/bin/env python3
# encoding: utf-8
#----------
# Constants
#----------
DEGREE_ARRAY_SIZE = 32
VALUES = { 2**i: i for i in range(DEGREE_ARRAY_SIZE) }
#----------
# Functions
#----------
def convert(a):
b = [ 0 for i in range(DEGREE_ARRAY_SIZE) ]
total = 0
for val in a:
b[VALUES[val]] += 1
total += val
start = 0
for i, cnt in enumerate(reversed(b)):
if cnt != 0:
start = DEGREE_ARRAY_SIZE - i
break
return b, total, start
def calc(q, b):
ans = 0
val = 2 ** (len(b) - 1)
for cnt in reversed(b):
if q >= val * cnt:
q -= val * cnt
ans += cnt
if q == 0:
break
if q >= val:
# c = min(cnt, q // val)
# q -= c * val
# ans += c
r = q % val
d = q // val
if cnt < d:
r += (d - cnt) * val
d = cnt
q = r
ans += d
if q == 0:
break
val //= 2
return ans if q == 0 else -1
# Reads a string from stdin, splits it by space chars, converts each
# substring to int, adds it to a list and returns the list as a result.
def get_ints():
return [ int(n) for n in input().split() ]
# Reads a string from stdin, splits it by space chars, converts each substring
# to floating point number, adds it to a list and returns the list as a result.
def get_floats():
return [ float(n) for n in input().split() ]
#----------
# Execution start point
#----------
if __name__ == "__main__":
a = get_ints()
assert len(a) == 2
n, q = a[0], a[1]
a = get_ints()
assert len(a) == n
qj = [int(input()) for i in range(q)]
assert len(qj) == q
b, total, start = convert(a)
b = b[:start]
assert sum(b) == n
DEGREE_ARRAY_SIZE = start
for i in qj:
if i < total:
ans = calc(i, b)
elif i == total:
ans = n
else:
ans = -1
print(ans)
```
No
| 59,730 | [
0.449951171875,
0.235107421875,
0.20556640625,
0.07415771484375,
-0.80224609375,
0.06463623046875,
-0.214599609375,
0.1517333984375,
0.25537109375,
0.66455078125,
0.65185546875,
-0.042633056640625,
-0.07391357421875,
-0.619140625,
-0.73583984375,
0.1483154296875,
-0.5478515625,
-0.... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
input=__import__('sys').stdin.readline
from math import log
n,q = map(int,input().split())
lis = list(map(int,input().split()))
has1=[0]*(30)
for i in lis:
a = int(log(i,2))
has1[a]+=1
c=1
for _ in range(q):
no = int(input())
c=1
flag=1
ans=0
has=has1[:]
for i in range(30):
if c & no:
# print(c,i)
if has[i]>0:
has[i]-=1
ans+=1
else:
k=0
c1=2
for j in range(i-1,-1,-1):
# print(j,'i')
k+=has[j]/c1
c1*=2
if k<1:
flag=False
break
k=0
c1=2
for j in range(i-1,-1,-1):
req = 1-k
# print(req,has[j],j)
if req>=has[j]/c1:
ans+=has[j]
k+=has[j]/c1
has[j]=0
else:
ans+=(req)*c1
has[j]=(has[j]/c1-req)*c1
c1*=2
c*=2
# print(ans,flag,no)
if flag:
print(int(ans))
else:
print(-1)
```
No
| 59,731 | [
0.4521484375,
0.297119140625,
0.1943359375,
0.06317138671875,
-0.74853515625,
0.096923828125,
-0.30615234375,
0.09783935546875,
0.255126953125,
0.70703125,
0.65966796875,
-0.02557373046875,
-0.08074951171875,
-0.66552734375,
-0.734375,
0.1622314453125,
-0.552734375,
-0.521484375,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
# @oj: codeforces
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-10-14 16:44
# @url:https://codeforc.es/contest/1003/problem/D
import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio
BUFSIZE = 8192
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")
# ------------------------------
## 注意嵌套括号!!!!!!
## 先有思路,再写代码,别着急!!!
## 先有朴素解法,不要有思维定式,试着换思路解决
## 精度 print("%.10f" % ans)
def main():
n,q=map(int,input().split())
a=list(map(int,input().split()))
d=collections.Counter(a)
keys=sorted(d.keys(),reverse=True)
# print (keys,d)
for i in range(q):
ans=0
b=int(input())
for k in keys:
cnt=b//k
if cnt<=d[k]:
ans+=cnt
b-=cnt*k
else:
ans+=d[k]
b-=cnt*d[k]
if b>0:
print (-1)
else:
print (ans)
if __name__ == "__main__":
main()
```
No
| 59,732 | [
0.488037109375,
0.216796875,
0.12213134765625,
0.1304931640625,
-0.79150390625,
0.004241943359375,
-0.288330078125,
0.07757568359375,
0.2279052734375,
0.65966796875,
0.626953125,
-0.10198974609375,
-0.10687255859375,
-0.63818359375,
-0.70703125,
0.11956787109375,
-0.52294921875,
-0... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is the minimum number of coins that is necessary to obtain the value b_j using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value b_j, the answer to the j-th query is -1.
The queries are independent (the answer on the query doesn't affect Polycarp's coins).
Input
The first line of the input contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the number of coins and the number of queries.
The second line of the input contains n integers a_1, a_2, ..., a_n — values of coins (1 ≤ a_i ≤ 2 ⋅ 10^9). It is guaranteed that all a_i are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).
The next q lines contain one integer each. The j-th line contains one integer b_j — the value of the j-th query (1 ≤ b_j ≤ 10^9).
Output
Print q integers ans_j. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value b_j the answer to the j-th query is -1.
Example
Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
Submitted Solution:
```
import random, math, sys
from copy import deepcopy as dc
from bisect import bisect_left, bisect_right
from collections import Counter
input = sys.stdin.readline
def power(x, y):
if (y == 0): return 1
elif (int(y % 2) == 0):
return (power(x, int(y / 2)) *
power(x, int(y / 2)))
else:
return (x * power(x, int(y / 2)) *
power(x, int(y / 2)))
# Function to call the actual solution
def solution(li, q):
ma = Counter(li)
pot = []
for i in range(34):
pot.append(1<<i)
for i in range(q):
num = int(input())
if num % 2 != 0 and num != 1:
print(-1)
else:
p = int(math.log2(num)) + 1
s = 0
ma1 = ma
inc = {}
while num > 0 and p >= 0:
po = pot[p]
if po <= num and po in ma1 and ma1[po] >= (num//po):
num -= (num//po)*po
ma1[po] -= (num//po)
s += 1
inc[po] = inc.get(po, 0) + (num//po)
else:
p -= 1
for i in inc:
ma[i] += inc[i]
# print(p, "a", num)
if p >= 0 and num == 0:
print(s)
else:
print(-1)
# Function to take input
def input_test():
# for _ in range(int(input())):
# n = int(input())
n, q = map(int, input().strip().split(" "))
# a, b, c = map(int, input().strip().split(" "))
li = list(map(int, input().strip().split(" ")))
out = solution(li, q)
# print(out)
# Function to test my code
def test():
pass
input_test()
# test()
```
No
| 59,733 | [
0.45263671875,
0.26416015625,
0.1851806640625,
0.062408447265625,
-0.8330078125,
0.019989013671875,
-0.276611328125,
0.045074462890625,
0.24365234375,
0.72509765625,
0.66845703125,
-0.0162200927734375,
-0.0535888671875,
-0.70751953125,
-0.7509765625,
0.1513671875,
-0.55517578125,
-... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
t = int( input().strip() )
for _ in range( t ):
s, d = map( int, input().strip().split(' ') )
diff = max(s, d) - min(s, d)
if diff > min(s, d):
ans = min(s, d)
else:
ans = diff
tmp = min(s, d) - diff
ans += (tmp // 3)*2 + (0,1)[tmp % 3 == 2]
print(ans)
```
| 59,884 | [
0.349853515625,
0.091064453125,
-0.1878662109375,
0.006267547607421875,
-0.75830078125,
-0.43896484375,
-0.482177734375,
0.2191162109375,
0.30615234375,
0.7705078125,
0.7861328125,
0.043914794921875,
0.416748046875,
-0.70556640625,
-0.326171875,
-0.002895355224609375,
-0.5556640625,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
import math
t = int(input())
for j in range(t):
temp = input()
a, b = temp.split(' ')
a = int(a)
b = int(b)
if a==0 or b == 0 :
print(0)
elif a>=2*b or b>=2*a:
print(min(a,b))
else:
print(int((a+b)/3))
```
| 59,885 | [
0.398681640625,
0.0831298828125,
-0.2254638671875,
0.0103912353515625,
-0.70849609375,
-0.393798828125,
-0.405029296875,
0.25634765625,
0.297607421875,
0.669921875,
0.78271484375,
0.034881591796875,
0.36572265625,
-0.7314453125,
-0.354736328125,
-0.006778717041015625,
-0.472900390625... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
def readintlst():
return list(map(int, input().split(' ')))
def readn():
return int(input())
N = readn()
for _ in range(N):
a, b = readintlst()
allx = min(a//2, b)
ally = min(a, b//2)
ans = max(allx, ally)
if a >= 2 * b or b >= 2 * a:
print(ans)
else:
print((a + b) //3)
```
| 59,886 | [
0.354248046875,
0.08721923828125,
-0.267578125,
0.032379150390625,
-0.75830078125,
-0.390625,
-0.418212890625,
0.286376953125,
0.336181640625,
0.59423828125,
0.7880859375,
0.0479736328125,
0.4345703125,
-0.6591796875,
-0.314697265625,
0.0025177001953125,
-0.58349609375,
-0.55078125... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
for _ in range(int(input())):
a, b = list(map(int, input().split()))
m = min(a, b)
M = max(a, b)
if 2*m <= M:
print(m)
else:
print((m+M)//3)
```
| 59,887 | [
0.362548828125,
0.06927490234375,
-0.197021484375,
0.048187255859375,
-0.75439453125,
-0.42919921875,
-0.456298828125,
0.26611328125,
0.3193359375,
0.6474609375,
0.83935546875,
0.0262603759765625,
0.410400390625,
-0.689453125,
-0.334716796875,
0.046783447265625,
-0.53271484375,
-0.... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
t=int(input())
for _ in range(t):
a,b=map(int,input().split())
if(a==0 or b==0):
print("0")
else:
c=min(a,b)
c1=(a+b)//3
print(min(c,c1))
```
| 59,888 | [
0.37353515625,
0.081787109375,
-0.2003173828125,
0.01319122314453125,
-0.728515625,
-0.45361328125,
-0.42041015625,
0.2509765625,
0.271728515625,
0.6630859375,
0.8359375,
0.015594482421875,
0.398681640625,
-0.71337890625,
-0.33251953125,
0.0099334716796875,
-0.51513671875,
-0.53417... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
# sys.setrecursionlimit(200000)
# mod = 10**9+7
# mod = 998244353
decimal.getcontext().prec = 46
def primeFactors(n):
prime = set()
while n % 2 == 0:
prime.add(2)
n = n//2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
prime.add(i)
n = n//i
if n > 2:
prime.add(n)
return list(prime)
def getFactors(n) :
factors = []
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
if (n // i == i) :
factors.append(i)
else :
factors.append(i)
factors.append(n//i)
i = i + 1
return factors
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
num = []
for p in range(2, n+1):
if prime[p]:
num.append(p)
return num
def lcm(a,b):
return (a*b)//math.gcd(a,b)
def sort_dict(key_value):
return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]))
def list_input():
return list(map(int,input().split()))
def num_input():
return map(int,input().split())
def string_list():
return list(input())
def decimalToBinary(n):
return bin(n).replace("0b", "")
def binaryToDecimal(n):
return int(n,2)
def DFS(n,s,adj):
visited = [False for i in range(n)]
stack = []
stack.append(s)
while (len(stack)):
s = stack[-1]
stack.pop()
if (not visited[s]):
visited[s] = True
for node in adj[s]:
if (not visited[node]):
stack.append(node)
def solve():
a,b = num_input()
if a == 0 or b == 0:
print(0)
else:
ans = (a+b)//3
print(min(a,b,ans))
t = int(input())
#t = 1
for _ in range(t):
solve()
```
| 59,889 | [
0.304443359375,
-0.0384521484375,
-0.179443359375,
0.054718017578125,
-0.72119140625,
-0.35498046875,
-0.42529296875,
0.13623046875,
0.28515625,
0.64111328125,
0.8095703125,
-0.10711669921875,
0.457275390625,
-0.654296875,
-0.273193359375,
0.1885986328125,
-0.61328125,
-0.611328125... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
for _ in range(int(input())):
a, b = map(int, input().split())
r = min(a, b)
if r == 0:
print(0)
else:
print(min(min(a, b), (a + b) // 3))
```
| 59,890 | [
0.38330078125,
0.05108642578125,
-0.2178955078125,
0.033660888671875,
-0.736328125,
-0.446044921875,
-0.44677734375,
0.263671875,
0.28125,
0.6484375,
0.86962890625,
0.0148162841796875,
0.385009765625,
-0.66845703125,
-0.33837890625,
0.04595947265625,
-0.51953125,
-0.51123046875,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Tags: binary search, greedy, math
Correct Solution:
```
t = int(input())
for _ in range(t):
a, b = [int(x) for x in input().split()]
if max(a, b) >= 2 * min(a, b):
print(min(a, b))
continue
x = min(a, b)
y = max(a, b)
p = max(2 * x - y, 0)
ans = (p // 3) * 2
x -= 3*(p // 3)
y -= 3*(p // 3)
ans += min(x,y//2)
print(ans)
```
| 59,891 | [
0.3759765625,
0.0772705078125,
-0.1640625,
0.0675048828125,
-0.75390625,
-0.446044921875,
-0.452392578125,
0.270751953125,
0.322021484375,
0.67529296875,
0.79833984375,
0.04644775390625,
0.372314453125,
-0.68505859375,
-0.339111328125,
0.050201416015625,
-0.54248046875,
-0.58789062... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
N = int(input())
for _ in range(N):
a,b = map(int,input().split())
res=0
sim=0
if (a == 0) or (b == 0):
print(0)
continue
if a>b:
if 2*b <= a:
print(b)
sim = 1
else:
if 2*a <= b:
print(a)
sim = 1
if sim:
continue
if a > b:
aux = a-b
print(aux+((2*(b-aux))//3))
else:
aux = b-a
print(aux+((2*(a-aux))//3))
```
Yes
| 59,892 | [
0.36962890625,
0.0936279296875,
-0.3203125,
0.03302001953125,
-0.84716796875,
-0.32958984375,
-0.484619140625,
0.397705078125,
0.30029296875,
0.64306640625,
0.7548828125,
0.0721435546875,
0.38232421875,
-0.6640625,
-0.30712890625,
-0.059783935546875,
-0.54931640625,
-0.63330078125,... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
for _ in range(int(input())):
s,d = map(int,input().split())
print(min((s+d)//3,min(s,d)))
```
Yes
| 59,893 | [
0.42724609375,
0.07623291015625,
-0.36376953125,
0.044464111328125,
-0.89697265625,
-0.302978515625,
-0.50732421875,
0.439208984375,
0.325439453125,
0.61474609375,
0.75341796875,
0.048797607421875,
0.361328125,
-0.6201171875,
-0.32861328125,
-0.07958984375,
-0.52490234375,
-0.55957... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
if __name__ == '__main__':
n = int(input())
for i in range(0, n):
sticks, diamonds = [int(i) for i in input().split()]
print(min(sticks, diamonds, (sticks + diamonds) // 3))
```
Yes
| 59,894 | [
0.41650390625,
0.047760009765625,
-0.32861328125,
0.0138092041015625,
-0.89892578125,
-0.290283203125,
-0.5185546875,
0.452880859375,
0.326904296875,
0.5693359375,
0.79638671875,
0.05517578125,
0.331298828125,
-0.66259765625,
-0.36962890625,
-0.07049560546875,
-0.5322265625,
-0.561... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
for _ in range(int(input())):
a, b = map(int, input().split())
if a > b:
a, b = b, a
cnt = min(b - a, a)
a = max(a - (b - a), 0)
cnt += a // 3 * 2 + (a % 3 == 2)
print(cnt)
```
Yes
| 59,895 | [
0.417236328125,
0.08477783203125,
-0.318603515625,
0.0501708984375,
-0.869140625,
-0.300537109375,
-0.486572265625,
0.43701171875,
0.327880859375,
0.61328125,
0.759765625,
0.06927490234375,
0.347900390625,
-0.6435546875,
-0.312744140625,
-0.0733642578125,
-0.52197265625,
-0.5732421... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
T=int(input())
for i in range(T):
x=list(map(int,input().split()))
a=x[0]
b=x[1]
if a==0 or b==0:
print(0)
continue
d=abs(a-b)
if a>b:
b-=d
a-=d*2
else:
b-=d*2
a-=d
while(a>2 and b>2):
a-=3
b-=3
d+=2
if a==2:
print(d+1)
else:
print(d)
```
No
| 59,896 | [
0.385498046875,
0.0948486328125,
-0.339111328125,
0.022705078125,
-0.87451171875,
-0.299560546875,
-0.489013671875,
0.440185546875,
0.32568359375,
0.63720703125,
0.7451171875,
0.095947265625,
0.38134765625,
-0.67431640625,
-0.307373046875,
-0.078125,
-0.5537109375,
-0.6396484375,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
from sys import stdin, stdout
import math,sys
from itertools import permutations, combinations
from collections import defaultdict,deque,OrderedDict
from os import path
import bisect as bi
import heapq
def yes():print('YES')
def no():print('NO')
if (path.exists('input.txt')):
#------------------Sublime--------------------------------------#
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
def I():return (int(input()))
def In():return(map(int,input().split()))
else:
#------------------PYPY FAst I/o--------------------------------#
def I():return (int(stdin.readline()))
def In():return(map(int,stdin.readline().split()))
def dict(a):
d={}
for x in a:
if d.get(x,-1)!=-1:
d[x]+=1
else:
d[x]=1
return d
def find_gt(a, x):
'Find leftmost value greater than x'
i = bi.bisect_right(a, x)
if i != len(a):
return i
else:
return -1
def main():
try:
a,b=In()
if a==0 or b==0:
print(0)
else:
print((a+b)//3)
except:
pass
M = 998244353
P = 1000000007
if __name__ == '__main__':
for _ in range(I()):main()
#for _ in range(1):main()
```
No
| 59,897 | [
0.300537109375,
0.04034423828125,
-0.241455078125,
0.0236968994140625,
-0.95263671875,
-0.25927734375,
-0.57470703125,
0.37109375,
0.260986328125,
0.6640625,
0.70703125,
0.013916015625,
0.41259765625,
-0.62060546875,
-0.361083984375,
-0.049591064453125,
-0.61376953125,
-0.614257812... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
#------------------------------what is this I don't know....just makes my mess faster--------------------------------------
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")
#----------------------------------Real game starts here--------------------------------------
#_______________________________________________________________#
def fact(x):
if x == 0:
return 1
else:
return x * fact(x-1)
def lower_bound(li, num): #return 0 if all are greater or equal to
answer = -1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] >= num:
answer = middle
end = middle - 1
else:
start = middle + 1
return answer #index where x is not less than num
def upper_bound(li, num): #return n-1 if all are small or equal
answer = -1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] <= num:
answer = middle
start = middle + 1
else:
end = middle - 1
return answer #index where x is not greater than num
def abs(x):
return x if x >=0 else -x
def binary_search(li, val, lb, ub):
ans = 0
while(lb <= ub):
mid = (lb+ub)//2
#print(mid, li[mid])
if li[mid] > val:
ub = mid-1
elif val > li[mid]:
lb = mid + 1
else:
ans = 1
break
return ans
def sieve_of_eratosthenes(n):
ans = []
arr = [1]*(n+1)
arr[0],arr[1], i = 0, 0, 2
while(i*i <= n):
if arr[i] == 1:
j = i+i
while(j <= n):
arr[j] = 0
j += i
i += 1
for k in range(n):
if arr[k] == 1:
ans.append(k)
return ans
def nc2(x):
if x == 1:
return 0
else:
return x*(x-1)//2
#_______________________________________________________________#
'''
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▄███████▀▀▀▀▀▀███████▄
░▐████▀▒▒Aestroix▒▒▀██████
░███▀▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀████
░▐██▒▒▒▒▒KARMANYA▒▒▒▒▒▒████▌ ________________
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌ ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░░█▒▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▒▐███▌ ? |___CM ONE DAY___|
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▒▐███▌ ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌ ? ?
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌ ?
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█ ? ?
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀ ?
░░░░▄█████████ ████=========█▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
░░░░░████████████████
'''
for _ in range(int(input())):
o, z = map(int, input().split())
temp = min(o//2, z)
temp1 = z - temp
res1 = temp + min(temp1//2, o%2)
temp = min(z//2, o)
temp1 = o - temp
res2 = temp + min(temp1//2, z%2)
res = max(res2, res1)
if (o%2 == 0 and z%2 == 0) or (o%2 == 1 and z%2 == 1):
print(res)
else:
print(res+1)
```
No
| 59,898 | [
0.303466796875,
0.01275634765625,
-0.2281494140625,
0.1348876953125,
-0.87646484375,
-0.30712890625,
-0.405517578125,
0.45556640625,
0.308349609375,
0.6923828125,
0.73876953125,
0.058837890625,
0.4892578125,
-0.662109375,
-0.2890625,
0.05218505859375,
-0.5400390625,
-0.65234375,
... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.
Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds?
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The only line of each test case contains two integers a and b (0 ≤ a, b ≤ 10^9) — the number of sticks and the number of diamonds, respectively.
Output
For each test case print one integer — the maximum number of emeralds Polycarp can earn.
Example
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Note
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.
In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
Submitted Solution:
```
T=int(input())
for _ in range(T):
a,b=map(int,input().split())
c=0
if(a==b):
print((a+b)//3)
elif(a==0 or b==0):
print('0')
else:
if(max(a,b)//2>=min(a,b)):
print(min(a,b))
c+=1
else:
if(max(a,b)%2==0):
a1=(max(a,b)//2)
else:
y=max(a,b)//2
if(min(a,b)-y>1):
a1=(y+1)
else:
a1=(y)
t=min(a,b)
y=2*t
s=max(a,b)-min(a,b)
if(y%3==1 and s>1):
b1=((y//3)+1)
elif(y%3==2 and s>0):
b1=((y//3)+1)
else:
b1=(y//3)
if(c==0):
print(max(a1,b1))
```
No
| 59,899 | [
0.384521484375,
0.0999755859375,
-0.270263671875,
0.0068359375,
-0.84375,
-0.298583984375,
-0.465576171875,
0.409912109375,
0.25390625,
0.65771484375,
0.7509765625,
0.0687255859375,
0.378662109375,
-0.7060546875,
-0.357421875,
-0.052337646484375,
-0.57080078125,
-0.5849609375,
-0... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
X = []
Y = []
for a, b in zip(A, B):
if b == 1:
X.append(a)
else:
Y.append(a)
X = [0] + sorted(X, key = lambda x: -x)
Y = [0] + sorted(Y, key = lambda x: -x)
for i in range(1, len(X)):
X[i] += X[i-1]
for i in range(1, len(Y)):
Y[i] += Y[i-1]
if X[-1] + Y[-1] < M:
print(-1)
continue
j = len(Y) - 1
ans = 1 << 50
for i in range(len(X)):
while j and X[i] + Y[j-1] >= M:
j -= 1
if X[i] + Y[j] >= M:
ans = min(ans, i + j * 2)
print(ans)
```
| 59,948 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
for _ in range(int(input())):
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
aa,bb=[],[]
for i in range(n):
if b[i]==1:
aa.append(a[i])
else:
bb.append(a[i])
aa.sort(reverse=True)
bb.sort(reverse=True)
pre=0
suf=sum(bb)
r=len(bb)
out=10**9
for i in range(len(aa)+1):
while r>0 and suf+pre-bb[r-1]>=m :
r-=1
suf-=bb[r]
if suf+pre>=m:
out=min(out,r*2+i)
if i<len(aa):
pre+=aa[i]
if out==10**9:
print(-1)
else:
print(out)
```
| 59,949 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
from bisect import bisect_left,bisect_right
T=int(input())
for i in range(T):
v1 = []
v2 = []
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for i in range(n):
if b[i]==1:
v1.append(a[i])
else:
v2.append(a[i])
v1.sort(reverse=True)
v2.sort(reverse=True)
ls1 = []
ls2 = []
temp=0
for i in v1:
temp+=i
ls1.append(temp)
temp=0
for i in v2:
temp+=i
ls2.append(temp)
ls1.insert(0,0)
ls2.insert(0,0)
ans=float("inf")
# for i in range(len(ls1)):
# cur=len(ls2)-1
# while (cur>=1 and ls2[cur-1]+ls1[i]>=m):
# cur-=1
# if ls2[cur]+ls1[i]>=m:
# ans=min(ans,i+2*cur)
# if ans==float("inf"):
# print(-1)
# else:
# print(ans)
for i in range(len(ls1)):
c = bisect_left(ls2, m - ls1[i])
if c >= len(ls2):
c = c - 1
if ls1[i] + ls2[c] >= m:
ans = min(ans, i + 2 * c)
if ans==float("inf"):
print(-1)
else:
print(ans)
```
| 59,950 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n,m=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A1=[]
A2=[]
for i in range(n):
if B[i]==1:
A1.append(A[i])
else:
A2.append(A[i])
A1.sort(reverse=True)
A2.sort(reverse=True)
S1=[0]
for a in A1:
S1.append(S1[-1]+a)
S2=[0]
for a in A2:
S2.append(S2[-1]+a)
ANS=1<<60
ind2=len(S2)-1
#print(S1)
#print(S2)
for i in range(len(S1)):
s1=S1[i]
while s1+S2[ind2]>=m:
ANS=min(ANS,i+ind2*2)
if ind2>0:
ind2-=1
else:
break
if ANS==1<<60:
print(-1)
else:
print(ANS)
```
| 59,951 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
import collections,sys,functools,heapq,bisect
input = sys.stdin.readline
mod = 10**9 +7
# return modular inverse of a with respect to m
def modInverse(a, m):
m0 = m
y = 0
x = 1
if (m == 1):
return 0
while (a > 1):
q = a // m
t = m
m = a % m
a = t
t = y
y = x - q * y
x = t
if (x < 0):
x = x + m0
return x
t = int(input())
for _ in range(t):
n,m = map(int,input().strip().split())
a = list(map(int,input().strip().split()))
b = list(map(int,input().strip().split()))
a1 = []
a2 = []
for x,i in zip(a,b):
if i == 1:
a1.append(x)
else:
a2.append(x)
a1.sort(reverse=True)
a2.sort(reverse=True)
for i in range(1,len(a1)):
a1[i] += a1[i-1]
for i in range(1,len(a2)):
a2[i] += a2[i-1]
#print(a1,a2)
if a1 and a2:
if a1[-1]+a2[-1] < m:
print(-1)
continue
elif a1:
if a1[-1] < m:
print(-1)
continue
elif a2:
if a2[-1] < m:
print(-1)
continue
ans = len(a1) + 2*len(a2)
if a1:
y = bisect.bisect_left(a1,m)
if y < len(a1) and a1[y] >= m:
ans = min(ans,y+1)
if a2:
y = bisect.bisect_left(a2,m)
if y < len(a2) and a2[y] >= m:
ans = min(ans,2*(y+1))
for i in range(len(a1)):
x = m-a1[i]
if x < 0:
print(ans)
break
y = bisect.bisect_left(a2,x)
if y<len(a2) and a1[i] + a2[y] >= m:
ans = min(ans,2*(y+1)+(i+1))
else:
print(ans)
```
| 59,952 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
def find_convenience_lost(n):
memory = list(map(int, input().split()))[1]
memory_per_app, weight = list(map(int, input().split())), list(map(int, input().split()))
ones = list(reversed(sorted([a for i, a in enumerate(memory_per_app) if weight[i] == 1])))
twos = list(reversed(sorted([a for i, a in enumerate(memory_per_app) if weight[i] == 2])))
i, j, = 0, 0
memory_removed = 0
convenience_points_lost = 0
while(i < (len(ones) - 1) and j < len(twos)):
if memory_removed + twos[j] >= memory or memory_removed + ones[i] >= memory:
return convenience_points_lost + 1 if memory_removed + ones[i] >= memory else convenience_points_lost + 2
if ones[i] + ones[i + 1] <= twos[j]:
memory_removed += twos[j]
convenience_points_lost += 2
j += 1
else:
memory_removed += ones[i]
convenience_points_lost += 1
i += 1
while j < len(twos):
if memory_removed >= memory:
return convenience_points_lost
if i == len(ones) - 1:
if ones[i] >= twos[j] or ones[i] + memory_removed >= memory:
memory_removed += ones[i]
convenience_points_lost += 1
i += 1
continue
memory_removed += twos[j]
convenience_points_lost += 2
j += 1
while i < len(ones):
if memory_removed >= memory:
return convenience_points_lost
memory_removed += ones[i]
convenience_points_lost += 1
i += 1
if memory_removed >= memory:
return convenience_points_lost
return -1
n = int(input())
for i in range(n):
print(find_convenience_lost(i))
```
| 59,953 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
# dp = {}
# inf = [int(1e9+7)]
#
# def solve(list1,num,sum1,index):
# key = str(num)+"-"+str(sum1)+"-"+str(index)
# if key in dp.keys():
# return dp[key]%inf[0]
# if sum1==0:
# return 1
#
# elif index<0:
# return 0
# elif num<0:
# return 0
#
# else:
# if list1[index]>sum1:
# key1 = str(num) + "-" + str(sum1) + "-" + str(index-1)
# if key1 in dp.keys():
# dp[key] =dp[key1]
# else:
# dp[key1] = solve(list1,num,sum1,index-1)
# dp[key] = dp[key1]%inf[0]
# return dp[key1]%inf[0]
# else:
# key1 = str(num-1)+"-"+str(sum1-list1[index])+"-"+str(index-1)
# key2 = str(num) + "-" + str(sum1) + "-" + str(index-1)
#
# if key1 not in dp.keys():
# dp[key1] = solve(list1,num-1,sum1-list1[index],index-1)
#
# if key2 not in dp.keys():
# dp[key2] = solve(list1,num,sum1,index-1)
# dp[key] = dp[key2]%inf[0] +dp[key1]%inf[0]
# return dp[key]%inf[0]
#
#
#
#
#
#
# t = int(input())
# while t!=0:
#
# n,k = map(int,input().split())
# list1 = list(map(int,input().split()))
# list1.sort()
# dp={}
# s = sum(list1[len(list1)-k:])
# ans = solve(list1,k,s,n-1)
# print(ans)
#
#
#
#
# t-=1
#
#
t = int(input())
while t!=0:
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
one=[]
two=[]
for i in range(n):
if b[i]==1:
one.append(a[i])
else:
two.append(a[i])
one.sort()
two.sort()
ans=0
while len(one)>0 and len(two)>0 and m>0:
if one[-1]>=m:
ans+=1
m-=one[-1]
one.pop()
break
elif m<=two[-1]:
ans+=2
m-=two[-1]
two.pop()
break
elif len(one)==1 :
ans+=2
m-=two[-1]
two.pop()
elif two[-1]>(one[-1]+one[-2]):
ans+=2
m-=two[-1]
two.pop()
else:
m-=one[-1]
ans+=1
one.pop()
if m<=0:
print(ans)
else:
while len(one)>0 and m>0:
ans+=1
m-=one[-1]
one.pop()
if m<=0:
print(ans)
else:
while len(two) > 0 and m > 0:
ans += 2
m -= two[-1]
two.pop()
if m<=0:
print(ans)
else:
print(-1)
t-=1
```
| 59,954 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Tags: binary search, dp, sortings, two pointers
Correct Solution:
```
import sys
for _ in range(int(input())):
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cost1 = []
cost2 = []
for i in range(n):
if b[i]==1:
cost1.append(a[i])
else:
cost2.append(a[i])
cost1.sort(reverse = True)
cost2.sort(reverse = True)
i = -1
ans = sys.maxsize
cost = 0
total_memory = 0
while i+1<len(cost2) and total_memory<m:
i+=1
total_memory+=cost2[i]
cost+=2
if total_memory>=m:
ans = min(ans,cost)
for j in range(len(cost1)):
total_memory+=cost1[j]
cost+=1
while i>=0 and total_memory-cost2[i]>=m:
cost-=2
total_memory-=cost2[i]
i-=1
if total_memory>=m:
ans = min(ans,cost)
if ans==sys.maxsize:
print(-1)
else:
print(ans)
```
| 59,955 | [
0.46240234375,
0.083251953125,
0.26318359375,
0.41357421875,
-0.83984375,
-0.33056640625,
-0.15234375,
-0.176513671875,
0.46630859375,
0.62158203125,
0.93359375,
0.0389404296875,
0.333251953125,
-0.712890625,
-0.54736328125,
0.06036376953125,
-0.638671875,
-0.5712890625,
-0.40283... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
import collections
import string
import math
import copy
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)
def input(): return sys.stdin.readline().rstrip("\r\n")
# n = 0
# m = 0
# n = int(input())
# li = [int(i) for i in input().split()]
# s = sorted(li)
mo = 998244353
def exgcd(a, b):
if not b:
return 1, 0
y, x = exgcd(b, a % b)
y -= a//b * x
return x, y
def getinv(a, m):
x, y = exgcd(a, m)
return -(-1) if x == 1 else x % m
def comb(n, b):
res = 1
b = min(b, n-b)
for i in range(b):
res = res*(n-i)*getinv(i+1, mo) % mo
# res %= mo
return res % mo
def quickpower(a, n):
res = 1
while n:
if n & 1:
res = res * a % mo
n >>= 1
a = a*a % mo
return res
def dis(a, b):
return abs(a[0]-b[0]) + abs(a[1]-b[1])
def getpref(x):
if x > 1:
return (x)*(x-1) >> 1
else:
return 0
t = int(input())
for ti in range(t):
n, m = map(int, input().split())
l1 = [int(i) for i in input().split()]
l2 = [int(i) for i in input().split()]
s1 = []
s2 = []
for i, j in zip(l1, l2):
if j == 2:
s2.append(i)
else:
s1.append(i)
s1.sort()
s2.sort()
# if len(s1) + 2*len(s2) < m:
# print(-1)
# continue
cur = 0
loss = 0
ans = 1145141919810
idx = 0
for ind, i in enumerate(s1[::-1]):
# if cur >= m:
# idx = len(s1) - ind - 1
# break
cur += i
loss += 1
while idx < len(s1) and cur-s1[idx] >= m:
cur -= s1[idx]
idx += 1
loss -= 1
if cur >= m:
ans = min(ans, loss)
while s2:
cur += s2.pop()
loss += 2
while idx < len(s1) and cur-s1[idx] >= m:
cur -= s1[idx]
idx += 1
loss -= 1
if cur >= m:
ans = min(ans, loss)
if ans == 1145141919810:
print(-1)
else:
print(ans)
```
Yes
| 59,956 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
import math
import sys
from collections import *
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
t = inp()
ret = []
for i in range(t):
n, m = inlt()
mems = inlt()
convs = inlt()
if sum(mems) < m:
ret.append(-1)
continue
low = []
high = []
for i in range(n):
if convs.pop()==1:
low.append(mems.pop())
else:
high.append(mems.pop())
low.sort()
high.sort()
cur = 0
loss = 0
while loss < m:
if low and high and low[-1] >= high[-1]:
cur += 1
loss += low.pop()
elif len(low) >= 2 and high and low[-1] < high[-1]:
if low[-1] + low[-2] >= high[-1]:
cur += 1
loss += low.pop()
elif loss + low[-1] >= m:
cur += 1
loss += low.pop()
else:
cur += 2
loss += high.pop()
elif len(low) == 1 and high and low[-1] < high[-1]:
if loss + low[-1] >= m:
cur += 1
loss += low.pop()
else:
cur += 2
loss += high.pop()
elif low:
cur += 1
loss += low.pop()
else:
cur += 2
loss += high.pop()
ret.append(cur)
for r in ret:
print(r)
```
Yes
| 59,957 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
# \
#########################################################################################################
###################################The_Apurv_Rathore#####################################################
#########################################################################################################
#########################################################################################################
import sys
import os
import io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
import math
from bisect import bisect_left , bisect_right
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
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 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(int(i))
n = n / i
if n > 2:
l.append(n)
return list(set(l))
def power(x, y, p):
res = 1
x = x % p
if (x == 0):
return 0
while (y > 0):
if ((y & 1) == 1):
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def si():
return input()
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def prefix_sum(arr):
r = [0] * (len(arr)+1)
for i, el in enumerate(arr):
r[i+1] = r[i] + el
return r
def divideCeil(n, x):
if (n % x == 0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int, input().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
t = 1
t = int(input())
for _ in range(t):
n, m = li()
a = li()
b = li()
if(sum(a)<m):
print(-1)
continue
l1 = []
l2 = []
for i in range(n):
if (b[i] == 1):
l1.append(a[i])
else:
l2.append(a[i])
l1.sort(reverse=True)
l2.sort(reverse=True)
for i in range(1,len(l2)):
l2[i]+=l2[i-1]
for i in range(1,len(l1)):
l1[i]+=l1[i-1]
l1.append(0)
l2.append(0)
# l2.append(10000000000000000)
l1.sort()
l2.sort()
ans = 100000000000000
s = 0
q = len(l1)
qq = len(l2)
# print(l1)
# print(l2)
for i in range(q):
s = l1[i]
z = bisect_left(l2,m-s)
# print("z , i , m - s",z,i,m-s)
if (qq!=z):
if (s>=m):
ans = min(ans,i)
else:
ans = min(ans , i + 2*z)
print(ans)
```
Yes
| 59,958 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
# mod=1000000007
mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('0')
file = 1
def ceil(a,b):
return (a+b-1)//b
# write fastio for getting fastio template.
def solve():
res = []
for t in range(ii()):
n,m = mi()
a = li()
cost = li()
if m > sum(a):
res.append('-1')
continue
p = [[] for i in range(2)]
for i in range(n):
p[cost[i]-1].append(a[i])
p[0].sort(reverse=True)
p[1].sort(reverse=True)
n1,n2 = len(p[0]),len(p[1])
for i in range(1,n2):
p[1][i] += p[1][i-1]
ans = n1+2*n2
if n2 > 0 and p[1][-1] >= m:
ans = 2*(bisect_left(p[1],m)+1)
x = 0
# print(ans)
for i in range(n1):
x += p[0][i]
x1 = m-x
if(x1 <= 0):
ans = min(ans,i+1)
break
if n2>0 and p[1][-1] >= x1:
idx = bisect_left(p[1],x1)
ans = min(ans,i+2*(idx+1)+1)
res.append(ans)
for i in res:
print(i)
if __name__ =="__main__":
if(file):
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
```
Yes
| 59,959 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
for i in range(int(input())):
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
s=0
for k in a:
s+=k
if s<m:
print("-1")
else:
one1=[]
two=[]
for j in range(n):
if b[j]==1:
one1.append(a[j])
else:
two.append(a[j])
one1.sort(reverse=True)
two.sort(reverse=True)
s3=0
for d in range(len(two)):
t=two[d]
s3+=t
two[d]=s3
s3=0
one=[0]
for d in range(len(one1)):
t=one1[d]
s3+=t
one.append(s3)
## print(one,two)
ans=2*n+1
for j in range(len(one)):
if j==0:
points=0
else:
points=j
sum=m-one[j]
if sum<=0:
ans=min(ans,points)
elif len(two)==0:
continue
elif sum>two[len(two)-1]:
continue
else:
low=0
high=len(two)-1
while(low<=high):
mid=(low+high)//2
## print(points,mid,sum)
if two[mid]==sum:
break
elif two[mid]>sum:
high=mid-1
else:
low=mid+1
## print(ans,low,points)
ans=min(ans,points+(low+1)*2)
print(ans)
```
No
| 59,960 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
def task1(list1,list2,m):
pointer1=0
pointer2=0
point=0
flag=0
while 1:
if pointer1+1<len(list1) and pointer2<len(list2):
temp1=list1[pointer1]+list1[pointer1+1]
temp2=list2[pointer2]
if temp1>=temp2:
m=m-list1[pointer1]
pointer1+=1
point+=1
else:
m=m-list2[pointer2]
pointer2+=1
point+=2
if m<=0:
flag=1
break
elif pointer1+1<len(list1):
m=m-list1[pointer1]
pointer1+=1
point+=1
if m<=0:
flag=1
break
elif pointer2<len(list2):
if pointer1==len(list1)-1:
m=m-list1[pointer1]
pointer1+=1
point+=1
if m<0:
flag=1
break
m=m-list2[pointer2]
pointer2+=1
point+=2
if m<=0:
flag=1
break
else:
break
if flag:
return point
else:
return -1
def task2(list1,list2,m):
pointer1=0
pointer2=0
point=0
flag=0
while 1:
if pointer1+1<len(list1) and pointer2<len(list2):
temp1=list1[pointer1]+list1[pointer1+1]
temp2=list2[pointer2]
if temp1>=temp2:
m=m-list1[pointer1]
pointer1+=1
point+=1
else:
m=m-list2[pointer2]
pointer2+=1
point+=2
if m<=0:
flag=1
break
elif pointer1+1<len(list1):
m=m-list1[pointer1]
pointer1+=1
point+=1
if m<=0:
flag=1
break
elif pointer2<len(list2):
m=m-list2[pointer2]
pointer2+=1
point+=2
if m<=0:
flag=1
break
else:
break
if flag:
return point
else:
return -1
t=int(input())
for _ in range(t):
n,m=list(map(int,input().strip().split()))
a=list(map(int,input().strip().split()))
b=list(map(int,input().strip().split()))
list1=[]
list2=[]
for i in range(n):
if b[i]==1:
list1.append(a[i])
else:
list2.append(a[i])
list1.sort()
list1.reverse()
list2.sort()
list2.reverse()
ans1=task1(list1,list2,m)
ans2=task2(list1,list2,m)
print(min(ans1,ans2))
```
No
| 59,961 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
from collections import Counter, defaultdict, OrderedDict, deque
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from typing import List
import itertools
import math
import heapq
import string
import random
# map(int, input().split())
MIN, MAX, MOD = -0x3f3f3f3f, 0x3f3f3f3f, 1000000007
for _ in range(int(input())):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
nums = []
for i in range(n):
nums.append([b[i], a[i]])
nums.sort(key = lambda x: (-x[1]/x[0]))
if sum(a) < m: print(-1)
else:
ans, k = 0, 0
for i in range(n):
if k >= m: break
ans += nums[i][0]
k += nums[i][1]
nums.sort()
nums.sort(key=lambda x: -x[1])
ans1, k = 0, 0
for i in range(n):
if k >= m: break
ans1 += nums[i][0]
k += nums[i][1]
print(min(ans, ans1))
```
No
| 59,962 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.
Polycarp wants to free at least m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b_i to each application:
* b_i = 1 — regular application;
* b_i = 2 — important application.
According to this rating system, his phone has b_1 + b_2 + … + b_n convenience points.
Polycarp believes that if he removes applications with numbers i_1, i_2, …, i_k, then he will free a_{i_1} + a_{i_2} + … + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + … + b_{i_k} convenience points.
For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below):
* applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points;
* applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points.
* applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 10^9) — the number of applications on Polycarp's phone and the number of memory units to be freed.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the number of memory units used by applications.
The third line of each test case contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ 2) — the convenience points of each application.
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, output on a separate line:
* -1, if there is no set of applications, removing which will free at least m units of memory;
* the minimum number of convenience points that Polycarp will lose if such a set exists.
Example
Input
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
Output
2
-1
6
4
3
Note
In the first test case, it is optimal to remove applications with numbers 2 and 5, freeing 7 units of memory. b_2+b_5=2.
In the second test case, by removing the only application, Polycarp will be able to clear only 2 of memory units out of the 3 needed.
In the third test case, it is optimal to remove applications with numbers 1, 2, 3 and 4, freeing 10 units of memory. b_1+b_2+b_3+b_4=6.
In the fourth test case, it is optimal to remove applications with numbers 1, 3 and 4, freeing 12 units of memory. b_1+b_3+b_4=4.
In the fifth test case, it is optimal to remove applications with numbers 1 and 2, freeing 5 units of memory. b_1+b_2=3.
Submitted Solution:
```
#!/usr/bin/env python3
import sys, getpass
import math, random
import functools, itertools, collections, heapq, bisect
from collections import Counter, defaultdict, deque
input = sys.stdin.readline # to read input quickly
# available on Google, AtCoder Python3, not available on Codeforces
# import numpy as np
# import scipy
M9 = 10**9 + 7 # 998244353
# d4 = [(1,0),(0,1),(-1,0),(0,-1)]
# d8 = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
# d6 = [(2,0),(1,1),(-1,1),(-2,0),(-1,-1),(1,-1)] # hexagonal layout
MAXINT = sys.maxsize
# if testing locally, print to terminal with a different color
OFFLINE_TEST = getpass.getuser() == "hkmac"
# OFFLINE_TEST = False # codechef does not allow getpass
def log(*args):
if OFFLINE_TEST:
print('\033[36m', *args, '\033[0m', file=sys.stderr)
def solve(*args):
# screen input
if OFFLINE_TEST:
log("----- solving ------")
log(*args)
log("----- ------- ------")
return solve_(*args)
def read_matrix(rows):
return [list(map(int,input().split())) for _ in range(rows)]
def read_strings(rows):
return [input().strip() for _ in range(rows)]
# ---------------------------- template ends here ----------------------------
def solve_(arr,brr,c):
# your solution here
sumarr = sum(arr)
if sumarr < c:
return -1
if sum(arr) == c:
return sum(brr)
# important = []
# unimportant = []
# for a,b in zip(arr,brr):
# if b == 1:
# unimportant.append(a)
# else:
# important.append(a)
# important.sort()
# unimportant.sort()
# log(important)
# log(unimportant)
# log()
# c = c*2
order = sorted([(a/b,-b,a) for a,b in zip(arr,brr)])[::-1]
order = [(a,-b,c) for a,b,c in order]
log(order)
mem = 0
cost = 0
limit_single = 0
max_single = 0
for _,b,a in order:
mem += a
cost += b
if b == 1:
max_single = a
limit_single += 1
if mem >= c:
break
limit_single -= 1
res = cost
log(cost)
if max_single > 0:
num_single = 0
mem = 0
cost = 0
for _,b,a in order:
log(num_single, limit_single)
if b == 1:
if num_single == limit_single:
continue
num_single += 1
mem += a
cost += b
if mem >= c:
res = min(res, cost)
break
else:
pass
return res # - max_single
# for case_num in [0]: # no loop over test case
# for case_num in range(100): # if the number of test cases is specified
for case_num in range(int(input())):
# read line as an integer
# k = int(input())
# read line as a string
# srr = input().strip()
# read one line and parse each word as a string
# lst = input().split()
# read one line and parse each word as an integer
_,c = list(map(int,input().split()))
arr = list(map(int,input().split()))
brr = list(map(int,input().split()))
# read multiple rows
# mrr = read_matrix(k) # and return as a list of list of int
# arr = read_strings(k) # and return as a list of str
res = solve(arr,brr,c) # include input here
# print result
# Google and Facebook - case number required
# print("Case #{}: {}".format(case_num+1, res))
# Other platforms - no case number required
print(res)
# print(len(res))
# print(*res) # print a list with elements
# for r in res: # print each list in a different line
# print(res)
# print(*res)
```
No
| 59,963 | [
0.53125,
0.12744140625,
0.1802978515625,
0.392578125,
-0.85986328125,
-0.243408203125,
-0.132080078125,
-0.02789306640625,
0.376708984375,
0.6318359375,
0.88134765625,
0.0709228515625,
0.261474609375,
-0.73876953125,
-0.54296875,
-0.10369873046875,
-0.650390625,
-0.56787109375,
-... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 ≤ n ≤ 10^9) — how many years Polycarp has turned.
Output
Print t integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Tags: implementation
Correct Solution:
```
import math
t = int(input())
for _ in range(t):
n= int(input())
d = math.floor(math.log10(n))+1
res = 9*(d-1)
for i in range(1,10):
s = str(i)*d
if n>=int(s): res+=1
print(res)
```
| 60,769 | [
0.57666015625,
0.0291290283203125,
0.02056884765625,
0.002193450927734375,
-0.370849609375,
-0.259765625,
0.0196533203125,
-0.055389404296875,
0.308837890625,
0.367919921875,
0.6240234375,
-0.36474609375,
0.181640625,
-0.60986328125,
-0.3095703125,
-0.1287841796875,
-0.422607421875,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 ≤ n ≤ 10^9) — how many years Polycarp has turned.
Output
Print t integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Tags: implementation
Correct Solution:
```
t=int(input())
for i in range(t):
n=input()
l=len(n)
num=[]
for j in range(l):
num.append('1')
n1=''.join(num)
f1=int(n)//int(n1)
print((9*(l-1))+f1)
```
| 60,770 | [
0.57373046875,
0.024169921875,
0.05572509765625,
-0.00345611572265625,
-0.39599609375,
-0.25634765625,
0.007671356201171875,
-0.050201416015625,
0.310791015625,
0.343505859375,
0.623046875,
-0.367919921875,
0.15576171875,
-0.6083984375,
-0.3359375,
-0.11871337890625,
-0.424560546875,... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 ≤ n ≤ 10^9) — how many years Polycarp has turned.
Output
Print t integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Tags: implementation
Correct Solution:
```
#!python3
"""
w1ld [dog] inbox dot ru
"""
from collections import deque, Counter
import array
from itertools import combinations, permutations
from math import sqrt
import unittest
def read_int():
return int(input().strip())
def read_int_array():
return [int(i) for i in input().strip().split(' ')]
######################################################
tests = read_int()
for test in range(tests):
n = read_int()
nlen = 0
x = n
while x > 0:
x //= 10
nlen += 1
ans = 9 * (nlen-1)
ninc = 0
for i in range(nlen):
ninc = (ninc * 10 + 1)
ans += (n // ninc)
print(ans)
```
| 60,771 | [
0.61328125,
0.04248046875,
0.029205322265625,
0.032623291015625,
-0.38037109375,
-0.2069091796875,
0.02740478515625,
-0.020538330078125,
0.307373046875,
0.373291015625,
0.61328125,
-0.366943359375,
0.1778564453125,
-0.5498046875,
-0.309814453125,
-0.1107177734375,
-0.413330078125,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 ≤ n ≤ 10^9) — how many years Polycarp has turned.
Output
Print t integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Tags: implementation
Correct Solution:
```
def func(n):
l=len(str(n))
sum=9*(l-1)
if n>=int("1"*l):
sum+=n//int("1"*l)
return sum
t=int(input())
for _ in range(t):
n=int(input())
print(func(n))
```
| 60,772 | [
0.5849609375,
0.05682373046875,
0.016876220703125,
0.035736083984375,
-0.36181640625,
-0.255126953125,
0.041595458984375,
-0.01535797119140625,
0.2783203125,
0.301025390625,
0.6142578125,
-0.340576171875,
0.14794921875,
-0.55322265625,
-0.343017578125,
-0.08642578125,
-0.49609375,
... | 24 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 ≤ n ≤ 10^9) — how many years Polycarp has turned.
Output
Print t integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Tags: implementation
Correct Solution:
```
n = int(input())
for t in range(n):
a = str(input())
b = int(a)
s = 0
for j in range(1,10):
for i in range(1,len(a)+1):
if b >= int(str(j)*i):
s += 1
print(s)
```
| 60,773 | [
0.568359375,
0.01119232177734375,
0.047576904296875,
0.00304412841796875,
-0.401123046875,
-0.26708984375,
0.03277587890625,
-0.058013916015625,
0.302001953125,
0.338134765625,
0.634765625,
-0.37744140625,
0.1534423828125,
-0.611328125,
-0.318359375,
-0.1414794921875,
-0.433837890625... | 24 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.