message stringlengths 2 43.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 853 107k | cluster float64 24 24 | __index_level_0__ int64 1.71k 214k |
|---|---|---|---|---|---|
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 | instruction | 0 | 59,721 | 24 | 119,442 |
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)
``` | output | 1 | 59,721 | 24 | 119,443 |
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 | instruction | 0 | 59,722 | 24 | 119,444 |
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)
``` | output | 1 | 59,722 | 24 | 119,445 |
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 | instruction | 0 | 59,723 | 24 | 119,446 |
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()
``` | output | 1 | 59,723 | 24 | 119,447 |
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 | instruction | 0 | 59,724 | 24 | 119,448 |
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)
``` | output | 1 | 59,724 | 24 | 119,449 |
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 | instruction | 0 | 59,725 | 24 | 119,450 |
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()
``` | output | 1 | 59,725 | 24 | 119,451 |
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)
``` | instruction | 0 | 59,726 | 24 | 119,452 |
Yes | output | 1 | 59,726 | 24 | 119,453 |
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()
``` | instruction | 0 | 59,727 | 24 | 119,454 |
Yes | output | 1 | 59,727 | 24 | 119,455 |
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)
``` | instruction | 0 | 59,728 | 24 | 119,456 |
Yes | output | 1 | 59,728 | 24 | 119,457 |
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])
``` | instruction | 0 | 59,729 | 24 | 119,458 |
Yes | output | 1 | 59,729 | 24 | 119,459 |
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)
``` | instruction | 0 | 59,730 | 24 | 119,460 |
No | output | 1 | 59,730 | 24 | 119,461 |
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)
``` | instruction | 0 | 59,731 | 24 | 119,462 |
No | output | 1 | 59,731 | 24 | 119,463 |
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()
``` | instruction | 0 | 59,732 | 24 | 119,464 |
No | output | 1 | 59,732 | 24 | 119,465 |
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()
``` | instruction | 0 | 59,733 | 24 | 119,466 |
No | output | 1 | 59,733 | 24 | 119,467 |
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. | instruction | 0 | 59,884 | 24 | 119,768 |
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)
``` | output | 1 | 59,884 | 24 | 119,769 |
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. | instruction | 0 | 59,885 | 24 | 119,770 |
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))
``` | output | 1 | 59,885 | 24 | 119,771 |
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. | instruction | 0 | 59,886 | 24 | 119,772 |
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)
``` | output | 1 | 59,886 | 24 | 119,773 |
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. | instruction | 0 | 59,887 | 24 | 119,774 |
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)
``` | output | 1 | 59,887 | 24 | 119,775 |
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. | instruction | 0 | 59,888 | 24 | 119,776 |
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))
``` | output | 1 | 59,888 | 24 | 119,777 |
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. | instruction | 0 | 59,889 | 24 | 119,778 |
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()
``` | output | 1 | 59,889 | 24 | 119,779 |
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. | instruction | 0 | 59,890 | 24 | 119,780 |
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))
``` | output | 1 | 59,890 | 24 | 119,781 |
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. | instruction | 0 | 59,891 | 24 | 119,782 |
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)
``` | output | 1 | 59,891 | 24 | 119,783 |
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))
``` | instruction | 0 | 59,892 | 24 | 119,784 |
Yes | output | 1 | 59,892 | 24 | 119,785 |
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)))
``` | instruction | 0 | 59,893 | 24 | 119,786 |
Yes | output | 1 | 59,893 | 24 | 119,787 |
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))
``` | instruction | 0 | 59,894 | 24 | 119,788 |
Yes | output | 1 | 59,894 | 24 | 119,789 |
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)
``` | instruction | 0 | 59,895 | 24 | 119,790 |
Yes | output | 1 | 59,895 | 24 | 119,791 |
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)
``` | instruction | 0 | 59,896 | 24 | 119,792 |
No | output | 1 | 59,896 | 24 | 119,793 |
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()
``` | instruction | 0 | 59,897 | 24 | 119,794 |
No | output | 1 | 59,897 | 24 | 119,795 |
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)
``` | instruction | 0 | 59,898 | 24 | 119,796 |
No | output | 1 | 59,898 | 24 | 119,797 |
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))
``` | instruction | 0 | 59,899 | 24 | 119,798 |
No | output | 1 | 59,899 | 24 | 119,799 |
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. | instruction | 0 | 59,948 | 24 | 119,896 |
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)
``` | output | 1 | 59,948 | 24 | 119,897 |
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. | instruction | 0 | 59,949 | 24 | 119,898 |
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)
``` | output | 1 | 59,949 | 24 | 119,899 |
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. | instruction | 0 | 59,950 | 24 | 119,900 |
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)
``` | output | 1 | 59,950 | 24 | 119,901 |
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. | instruction | 0 | 59,951 | 24 | 119,902 |
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)
``` | output | 1 | 59,951 | 24 | 119,903 |
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. | instruction | 0 | 59,952 | 24 | 119,904 |
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)
``` | output | 1 | 59,952 | 24 | 119,905 |
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. | instruction | 0 | 59,953 | 24 | 119,906 |
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))
``` | output | 1 | 59,953 | 24 | 119,907 |
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. | instruction | 0 | 59,954 | 24 | 119,908 |
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
``` | output | 1 | 59,954 | 24 | 119,909 |
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. | instruction | 0 | 59,955 | 24 | 119,910 |
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)
``` | output | 1 | 59,955 | 24 | 119,911 |
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)
``` | instruction | 0 | 59,956 | 24 | 119,912 |
Yes | output | 1 | 59,956 | 24 | 119,913 |
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)
``` | instruction | 0 | 59,957 | 24 | 119,914 |
Yes | output | 1 | 59,957 | 24 | 119,915 |
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)
``` | instruction | 0 | 59,958 | 24 | 119,916 |
Yes | output | 1 | 59,958 | 24 | 119,917 |
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()
``` | instruction | 0 | 59,959 | 24 | 119,918 |
Yes | output | 1 | 59,959 | 24 | 119,919 |
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)
``` | instruction | 0 | 59,960 | 24 | 119,920 |
No | output | 1 | 59,960 | 24 | 119,921 |
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))
``` | instruction | 0 | 59,961 | 24 | 119,922 |
No | output | 1 | 59,961 | 24 | 119,923 |
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))
``` | instruction | 0 | 59,962 | 24 | 119,924 |
No | output | 1 | 59,962 | 24 | 119,925 |
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)
``` | instruction | 0 | 59,963 | 24 | 119,926 |
No | output | 1 | 59,963 | 24 | 119,927 |
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. | instruction | 0 | 60,769 | 24 | 121,538 |
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)
``` | output | 1 | 60,769 | 24 | 121,539 |
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. | instruction | 0 | 60,770 | 24 | 121,540 |
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)
``` | output | 1 | 60,770 | 24 | 121,541 |
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. | instruction | 0 | 60,771 | 24 | 121,542 |
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)
``` | output | 1 | 60,771 | 24 | 121,543 |
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. | instruction | 0 | 60,772 | 24 | 121,544 |
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))
``` | output | 1 | 60,772 | 24 | 121,545 |
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. | instruction | 0 | 60,773 | 24 | 121,546 |
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)
``` | output | 1 | 60,773 | 24 | 121,547 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.