text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have a given integer n. Find the number of ways to fill all 3 Γ— n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap. <image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ— n tiles. Input The only line contains one integer n (1 ≀ n ≀ 60) β€” the length. Output Print the number of ways to fill. Examples Input 4 Output 4 Input 1 Output 0 Note In the first example, there are 4 possible cases of filling. In the second example, you cannot fill the shapes in 3 Γ— 1 tiles. Submitted Solution: ``` n = int(input()) print(n//4) ``` No
900
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` from math import ceil n,d=map(int,input().split()) p=-1 ans=0 for i in map(int,input().split()): k=ceil((max(p-i+1,0))/d) ans+=k p=i+k*d print(ans) ```
901
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` """n, d = map(int, input().split()) l = list(map(int, input().split())) c = 0 for i in range(len(l)-1): if(l[i+1]<=l[i]): l[i+1]+=d c+=1 print(c)""" from math import ceil n,d = map(int,input().split()) a=list(map(int,input().split())) ans=0 for i in range(1,n): if a[i-1] >= a[i]: w = a[i-1] - a[i] a[i]+= ceil( (w+1)/d )*d ans+=ceil( (w+1)/d ) print(ans) ```
902
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` def main(n, d, b): ret = 0 for i in range(1, n): if b[i] <= b[i - 1]: diff = ((b[i - 1] - b[i]) // d) + 1 b[i] += (diff * d) ret += diff return ret print(main(*map(int, input().split(' ')), list(map(int,input().split(' '))))) ```
903
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` n, d = map(int, input().split()) line = list(map(int, input().split())) cnt = 0 for i in range(1, n): # print(i) if line[i] <= line[i-1]: q = (line[i-1]-line[i])//d + 1 cnt += q line[i] += q * d print(cnt) ```
904
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` n, d = map(int, input().split()) a = list(map(int, input().split())) m = 0 for i in range(1, n): if a[i] <= a[i-1]: x = (a[i-1] - a[i]) // d + 1 a[i] += x * d m += x print(m) ```
905
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` import sys from math import sqrt, floor, factorial, gcd from collections import deque, Counter inp = sys.stdin.readline read = lambda: list(map(int, inp().strip().split())) def solve(): n, d = read() arr = read(); curr = arr[0] count = 0 for i in range(1, n): # print(curr, arr[i], i, count) if curr >= arr[i]: diff = curr-arr[i] arr[i] += d*(diff//d) count += (diff//d) if curr >= arr[i]: arr[i] += d count += 1 curr = arr[i] print(count) # def solve(): # ans = "" # for _ in range(int(inp())): # n, c = read(); arr = [] # for i in range(n): arr.append(int(inp())) # arr.sort() # max_ans = 1 # low = arr[0]; high = arr[-1] # while low < high: # mid = (low+high)//2 # # print(low, high, mid, "**") # count = 1; left = arr[0] # for i in range(1, n): # right = arr[i] # if right-left >= mid: # # print(right-left, mid) # count += 1 # left = right # # print(count) # if count >= c: # low = mid+1 # max_ans = max(max_ans, mid) # else: # high = mid-1 # ans += str(max_ans)+"\n" # return(ans) if __name__ == "__main__": solve() # print(solve()) ```
906
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` import math def main(): n,d = map(int,input().split()) arr = list(map(int,input().split())) moves = 0 for i in range(n-1,0,-1): if arr[i] <= arr[i-1]: count = int(math.ceil((arr[i-1]+1-arr[i])/d)) arr[i] += count*d moves += count for j in range(i,n-1): if arr[j] >= arr[j+1]: count = int(math.ceil((arr[j]+1-arr[j+1])/d)) arr[j+1] += count*d moves += count print(moves) main() ```
907
Provide tags and a correct Python 3 solution for this coding contest problem. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Tags: constructive algorithms, implementation, math Correct Solution: ``` n,d=map(int,input().split()) b=list(map(int,input().split())) b0=b[0] m=0 for i in range(1,n): if b[i]<=b0: x=(b0-b[i])//d +1 m+=x b0=b[i]+x*d else: b0=b[i] print(m) ```
908
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` n, d = [int(x) for x in input().split()] a = [int(x) for x in input().split()] ans = 0 for i in range(1,n): if a[i]>a[i-1]: continue else: dif = a[i-1]-a[i] ans += dif//d + 1 a[i] += (dif//d + 1) * d print(ans) ``` Yes
909
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` n,d = map(int,input().split()) a = list(map(int,input().split())) ans = 0 for i in range(1,n): if a[i] == a[i-1]: a[i]+=d; ans += 1 elif a[i] < a[i-1]: ans += -(-(a[i-1]-a[i])//d); a[i]+= (-(-(a[i-1]-a[i])//d))*d if a[i] == a[i-1]: ans += 1; a[i]+=d print(ans) ``` Yes
910
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` buffer = [int(x) for x in input().split(' ')] t = buffer[0] d = buffer[1] res =0 arr = [int(x) for x in input().split(' ')] for i in range(1,t): if(arr[i-1]>=arr[i]): t = ((arr[i-1]-arr[i])//d)+1 arr[i]+= t*d res+=t print(res) ``` Yes
911
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` from math import ceil def number_of_operations(): # n is the number of terms in sequence # d is the number to be added to elements in order to make increasing sequence n, d = map(int, input().strip().split()) sequence = list(map(int, input().strip().split())) count = 0 for term_index in range(n - 1): # if next term is smaller than previous if sequence[term_index + 1] < sequence[term_index]: # add nr of operations count += ceil((sequence[term_index] - sequence[term_index + 1]) / d) # give new value to next term sequence[term_index + 1] += ceil((sequence[term_index] - sequence[term_index + 1]) / d) * d # if next term is equal to previous if sequence[term_index + 1] == sequence[term_index]: # nr of operations is 1 count += 1 sequence[term_index + 1] += d return count print(number_of_operations()) ``` Yes
912
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` l1 = [int(x) for x in input().split()] n,d = l1[0],l1[1] l2 = [int(x) for x in input().split()] l2.reverse() ans=0 for x in range(len(l2)-1): while l2[x]<=l2[x+1]: l2[x]+=d ans+=1 print(ans) ``` No
913
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` from math import ceil n,d=map(int,input().split()) l=list(map(int,input().split())) ma=l[0] m=0 for i in range(1,len(l)): if l[i]>ma: ma=l[i] else: p=ceil((ma-l[i])/d) m+=p+1 # print(m) ma=l[i] print(m) ``` No
914
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` import math n,d=map(int,input().split()) a=list(map(int,input().split())) c=0 for i in range(0,n-1): if(a[i]>a[i+1]): a[i+1]+=(math.ceil((a[i]-a[i+1])/d))*d c+=math.ceil((a[i]-a[i+1])/d) elif(a[i]==a[i+1]): a[i+1]+=d c+=1 print(c) ``` No
915
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing? Input The first line of the input contains two integer numbers n and d (2 ≀ n ≀ 2000, 1 ≀ d ≀ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≀ bi ≀ 106). Output Output the minimal number of moves needed to make the sequence increasing. Examples Input 4 2 1 3 3 2 Output 3 Submitted Solution: ``` n,d=map(int,input().split()) l=[int(c) for c in input().split()] t=[] s=0 for i in range(1,n): t+=[l[i]-l[i-1]] for i in range(len(t)): s+=t[i]//d+1 print(s) ``` No
916
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` import sys sys.setrecursionlimit(10**9) def dfs(a): global adj,vis,st j=0 vis[a]=1 for i in adj[a][:]: if vis[i[0]]==0: dfs(i[0]) adj[a][j][1]=1 elif vis[i[0]]==2: adj[a][j][1]=1 else: st=2 adj[a][j][1]=2 j+=1 vis[a]=2 n,m=map(int,input().split()) it=[] adj=[[] for i in range(n)] #print(adj) for _ in range(m): a,b=map(int,input().split()) adj[a-1].append([b-1,1,_]) #print(adj) vis=[0]*n st=1 for ii in range(n): if vis[ii]==0: dfs(ii) print(st) ans=[0]*m for i in range(n): for j in adj[i]: # print(j[2],j[1]) ans[j[2]]=j[1] print(*ans) ```
917
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` import sys input = sys.stdin.readline def dfs(cur_node, childs, vis, cur_dfs): if cur_node in cur_dfs: return True if vis[cur_node]: return False vis[cur_node] = True cur_dfs.add(cur_node) for ele in childs[cur_node]: if dfs(ele, childs, vis, cur_dfs): return True cur_dfs.remove(cur_node) return False n, m = map(int, input().split()) childs = [[] for i in range(n+1)] has_dad = [False] * (n+1) vis = [False] * (n+1) ans2 = [] for i in range(m): x1, x2 = map(int, input().split()) ans2.append(str((x1 < x2) + 1)) childs[x1].append(x2) has_dad[x2] = True has_cycle = False for i in range(1, n+1): if not has_dad[i] and dfs(i, childs, vis, set()): has_cycle = True break for i in range(1, n+1): if has_dad[i] and not vis[i]: has_cycle = True break if has_cycle: print(2) print(' '.join(ans2)) else: print(1) print(' '.join(['1']*m)) ```
918
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` from collections import defaultdict ans = defaultdict(lambda : 1) flag = 0 def dfs(i): global flag vis[i] = 1 for j in hash[i]: if not vis[j]: dfs(j) else: if vis[j] == 1: flag = 1 ans[(i,j)] = 2 vis[i] = 2 n,m = map(int,input().split()) hash = defaultdict(list) par = [0]+[i+1 for i in range(n)] for i in range(m): a,b = map(int,input().split()) hash[a].append(b) ans[(a,b)] = 1 vis = [0]*(n+1) for i in range(n): if vis[i] == 0: dfs(i) if flag: print(2) else: print(1) for i in ans: print(ans[i],end = ' ') ```
919
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` [N,M] = list(map(int,input().split())) edges = [[] for _ in range(N+1)] edge_in = [] for _ in range(M): [u,v] = list(map(int,input().split())) edge_in.append([u,v]) edges[u].append(v) seen = [False for _ in range(N+1)] visited = [False for _ in range(N+1)] def bfs(node): visited[node] = True seen[node] = True for v in edges[node]: if not seen[v] and visited[v]: continue if seen[v] or bfs(v): return True seen[node] = False return False hasCycle = False for i in range(1,N+1): if visited[i]: continue if bfs(i): hasCycle = True break if not hasCycle: print(1) print(" ".join(["1" for _ in range(M)])) else: print(2) print(" ".join(["1" if u < v else "2" for (u,v) in edge_in])) ```
920
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` n, m = input().split(' ') n, m = int(n), int(m) ch = [[] for i in range(n+1)] edges_by_order = [] for i in range(m): x, y = input().split(' ') x, y = int(x), int(y) ch[x].append(y) edges_by_order.append((x, y)) for i in range(n+1): ch[i] = sorted(ch[i]) st = [0 for i in range(n+1)] end = [0 for i in range(n+1)] timestamp = 0 cycle_exists = False def dfs(x): global ch, st, end, timestamp, cycle_exists timestamp += 1 st[x] = timestamp for y in ch[x]: if st[y] == 0: dfs(y) else: if st[y] < st[x] and end[y] == 0: cycle_exists = True timestamp += 1 end[x] = timestamp for i in range(1, n+1): if st[i] == 0: dfs(i) if not cycle_exists: print(1) for i in range(m): print(1, end=' ') print('', end='\n') else: print(2) for i in range(m): x = st[edges_by_order[i][0]] y = st[edges_by_order[i][1]] if x < y: print(1, end=' ') else: print(2, end=' ') print('', end='\n') ```
921
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` WHITE, GRAY, BLACK = 0, 1, 2 def solve(graph: [], colors: []): n = len(graph)-1 visited = [WHITE] * (n + 1) returnValue = 1 for i in range(1, n + 1): if visited[i] == WHITE and Dfs(graph, colors, i, visited): returnValue = 2 return returnValue def Dfs(graph: [], colors: [], id, visited: []): visited[id] = GRAY returnValue = False for adj, colorId in graph[id]: if visited[adj] == WHITE: val = Dfs(graph, colors, adj, visited) returnValue=True if val else returnValue elif visited[adj] == GRAY: returnValue = True colors[colorId] = 2 visited[id] = BLACK return returnValue n, m = [int(x) for x in input().split()] graph = [[] for _ in range(n + 1)] colors = [1] * (m) for i in range(m): u, v = [int(x) for x in input().split()] graph[u].append((v, i)) print(solve(graph,colors)) for color in colors: print(color,end=" ") print() ```
922
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` #!python3 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(' ')] ###################################################### vn, en = read_int_array() al = [[] for _ in range(vn)] # adjacency list def adj(v): return al[v] itoe = [None for _ in range(en)] # index to edge for eid in range(en): # eid - edge id v, w = read_int_array() v -= 1 w -= 1 al[v] += [w] itoe[eid] = (v, w) marked = set() stack = set() etoc = {} # edge to color def dfs(v): # vertex if v in marked: return marked.add(v) stack.add(v) hasbackedge = False for w in adj(v): if w in stack: # back edge hasbackedge = True etoc[(v, w)] = 2 continue if w in marked: continue if dfs(w): hasbackedge = True stack.remove(v) return hasbackedge hasbackedge = False for v in range(vn): if v not in marked: if dfs(v): hasbackedge = True print(2 if hasbackedge else 1) for ei in range(en): v, w = itoe[ei] c = etoc.get((v,w), 1) print(c, end=' ') ```
923
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Tags: constructive algorithms, dfs and similar, graphs Correct Solution: ``` v_n, e_n = tuple(map(int, input().split())) G = [set() for _ in range(v_n)] edges = [] d_s = [-1] * v_n ok = False def dfs(u): global ok if ok: return d_s[u] = 0 for v in G[u]: if d_s[v] == -1: dfs(v) elif d_s[v] == 0: ok = True d_s[u] = 1 for _ in range(e_n): u, v = tuple(map(int, input().split())) u -= 1 v -= 1 edges.append((u, v)) G[u].add(v) for u in range(v_n): if d_s[u]==-1: dfs(u) if not ok: print(1) for _ in range(e_n): print(1, end=' ') else: print(2) for (u, v) in edges: print(int(u < v) + 1, end=' ') ```
924
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` import math # import sys # input = sys.stdin.readline n,m=[int(i) for i in input().split(' ')] d=[[] for i in range(n)] done = [False for i in range(n)] def dfs(visited,i,d): visited[i]=True done[i] = True for j in d[i]: if visited[j]: return True if not done[j]: temp = dfs(visited,j,d) if temp:return True visited[i]=False return False edgelist=[] for i in range(m): a,b=[int(i) for i in input().split(' ')] edgelist.append([a,b]) d[a-1].append(b-1) ans = 0 visited=[False for i in range(n)] for i in range(n): if dfs(visited,i,d): ans = 1 break if ans: print(2) for i in edgelist: if i[0]>i[1]: print(2,end=' ') else:print(1,end=' ') else: print(1) print(' '.join('1' for i in range(m))) ``` Yes
925
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` colors = 1 n, m = map(int, input().split(' ')) adj = [[] for i in range(n)] color = [0 for i in range(m)] vis = [0 for i in range(n)] def dfs(u): global colors vis[u] = 1 for i, v in adj[u]: if vis[v] == 1: colors = 2 color[i] = 2 else: color[i] = 1 if vis[v] == 0: dfs(v) vis[u] = 2 for i in range(m): u, v = map(int, input().split(' ')) adj[u-1].append((i, v-1)) for i in range(n): if vis[i] == 0: dfs(i) print(colors) print(' '.join(map(str, color))) ``` Yes
926
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` n, m = [int(i) for i in input().split()] data = [] chil = [] for i in range(n+1): chil.append(set()) for j in range(m): data.append([int(i) for i in input().split()]) chil[data[-1][0]].add(data[-1][1]) # done = set() # fnd = set() # cycle = False # def dfs(a): # for c in chil[a]: # print(a,c) # if c in fnd: # global cycle # cycle = True # return # if c not in done: # fnd.add(c) # dfs(c) # for i in range(1, n+1): # if i not in done: # dfs(i) # done |= fnd # fnd = set() # if cycle: # break def cycle(): for i in range(1, n+1): stack = [i] fnd = [0] * (n+1) while stack: s = stack.pop() for c in chil[s]: if c == i: return True if not fnd[c]: stack.append(c) fnd[c] = 1 if not cycle(): print(1) l = ['1' for i in range(m)] print(' '.join(l)) else: print(2) for d in data: print(["2","1"][d[0] < d[1]], end=' ') print() ``` Yes
927
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` class Graph: def __init__(self, n, m): self.nodes = n self.edges = m self.adj = [[] for i in range(n)] self.color = [0 for i in range(m)] self.vis = [0 for i in range(n)] self.colors = 1 def add_edge(self, u, v, i): self.adj[u].append((i, v)) def dfs(self, u): self.vis[u] = 1 for i, v in self.adj[u]: if self.vis[v] == 1: self.colors = 2 self.color[i] = 2 else: self.color[i] = 1 if self.vis[v] == 0: self.dfs(v) self.vis[u] = 2 def solve(self): for i in range(self.nodes): if self.vis[i] == 0: self.dfs(i) print(self.colors) print(' '.join(map(str, self.color))) n, m = map(int, input().split(' ')) graph = Graph(n, m) for i in range(m): u, v = map(int, input().split(' ')) graph.add_edge(u-1, v-1, i) graph.solve() ``` Yes
928
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` def main(): import sys from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) adj = [[] for _ in range(N+1)] edge = {} for i in range(M): a, b = map(int, input().split()) adj[a].append(b) edge[a*(N+1)+b] = i seen = [0] * (N + 1) ans = [0] * M flg = 1 cnt = 1 for i in range(1, N+1): if seen[i]: continue seen[i] = 1 cnt += 1 que = deque() que.append(i) while que: v = que.popleft() seen[v] = cnt for u in adj[v]: if not seen[u]: seen[u] = 1 ans[edge[v*(N+1)+u]] = 1 que.append(u) elif seen[u] == cnt: flg = 0 ans[edge[v * (N + 1) + u]] = 2 else: ans[edge[v * (N + 1) + u]] = 1 assert 0 not in ans if flg: print(1) print(*ans) else: print(2) print(*ans) if __name__ == '__main__': main() ``` No
929
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` # import random # arr1=[random.randint(1,100) for i in range(10)] # arr2=[random.randint(1,100) for i in range(10)] # print(arr1," SORTED:-",sorted(arr1)) # print(arr2," SORTED:-",sorted(arr2)) # def brute_inv(a1,a2): # a3=a1+a2 # inv=0 # for i in range(len(a3)): # for j in range(i+1,len(a3)): # if a3[i]>a3[j]: # inv+=1 # return inv # def smart_inv(arr1,arr2): # i1,i2=0,0 # inv1=0; # while i1<len(arr1) and i2<len(arr2): # if arr1[i1]>arr2[i2]: # inv1+=(len(arr1)-i1) # i2+=1 # else: # i1+=1 # return inv1 # print(arr1+arr2,smart_inv(sorted(arr1),sorted(arr2))) # print(brute_inv(arr1,arr2)) # print(arr2+arr1,smart_inv(sorted(arr2),sorted(arr1))) # print(brute_inv(arr2,arr1)) # if (smart_inv(sorted(arr1),sorted(arr2))<smart_inv(sorted(arr2),sorted(arr1)) and brute_inv(arr1,arr2)<brute_inv(arr2,arr1)) or (smart_inv(sorted(arr2),sorted(arr1))<smart_inv(sorted(arr1),sorted(arr2)) and brute_inv(arr2,arr1)<brute_inv(arr1,arr2)): # print("True") # else: # print("False") # print(brute_inv([1,2,10],[3,5,7])) # print(brute_inv([3,5,7],[1,2,10])) # print(brute_inv([1,7,10],[3,5,17])) # print(brute_inv([3,5,17],[1,7,10])) # 1 :- 1 # 2 :- 10 # 3 :- 011 # 4 :- 0100 # 5 :- 00101 # 6 :- 000110 # 7 :- 0000111 # 8 :- 00001000 # 9 :- 000001001 # 10 :- 0000001010 # 11 :- 00000001011 # 12 :- 000000001100 # 13 :- 0000000001101 # 14 :- 00000000001110 # 15 :- 000000000001111 # 16 :- 0000000000010000 from sys import stdin,stdout n,m=stdin.readline().strip().split(' ') n,m=int(n),int(m) adj=[[] for i in range(n+1)] d={} for i in range(m): u,v=stdin.readline().strip().split(' ') u,v=int(u),int(v) adj[u].append(v) d[(u,v)]=i visited=[0 for i in range(n+1)] color=['1' for i in range(m)] flag=1 def dfs(curr,par): global flag for i in adj[curr]: if i!=par: if visited[i]==0: visited[i]=1 color[d[(curr,i)]]='1' dfs(i,curr) elif visited[i]==1: color[d[(curr,i)]]='2' flag=2 visited[curr]=2 visited[1]=1 dfs(1,-1) if flag==1: stdout.write("1\n"); stdout.write(' '.join(color)+"\n") else: stdout.write("2\n") stdout.write(' '.join(color)+"\n") ``` No
930
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` from collections import defaultdict vis=[] def isCyclic(n, graph): global vis vis=[0]*n rec=[0]*n for i in range(0,n): if(vis[i]== False): if(DfsRec(i,rec,graph)): return True return False def DfsRec(i,rec,g): global s1 global s2 global vis vis[i]=True rec[i]=True for u in g[i]: # print(u,rec,g) if(vis[u]==False and DfsRec(u,rec,g)): return True elif(rec[u]==True): s1=i s2=u return True rec[i]=False return False n,e=list(map(int,input().split())) d=defaultdict(lambda:[]) l=[] for i in range(0,e): a,b=list(map(int,input().split())) l.append([a-1,b-1]) d[a-1].append(b-1) #print(d) s1=-1 s2=-1 c=isCyclic(n,d) #print(s1,s2) if(c==True): print(2) ans=[] for i,j in l: if(i==s1 and s2==j): ans.append(2) else: ans.append(1) print(*ans) else: print(1) l=[1]*e print(*l) ``` No
931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a directed graph with n vertices and m directed edges without self-loops or multiple edges. Let's denote the k-coloring of a digraph as following: you color each edge in one of k colors. The k-coloring is good if and only if there no cycle formed by edges of same color. Find a good k-coloring of given digraph with minimum possible k. Input The first line contains two integers n and m (2 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of vertices and edges in the digraph, respectively. Next m lines contain description of edges β€” one per line. Each edge is a pair of integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” there is directed edge from u to v in the graph. It is guaranteed that each ordered pair (u, v) appears in the list of edges at most once. Output In the first line print single integer k β€” the number of used colors in a good k-coloring of given graph. In the second line print m integers c_1, c_2, ..., c_m (1 ≀ c_i ≀ k), where c_i is a color of the i-th edge (in order as they are given in the input). If there are multiple answers print any of them (you still have to minimize k). Examples Input 4 5 1 2 1 3 3 4 2 4 1 4 Output 1 1 1 1 1 1 Input 3 3 1 2 2 3 3 1 Output 2 1 1 2 Submitted Solution: ``` import math def ii(): return int(input()) def mi(): return map(int, input().split()) def dfs(s, c, used_colors): global visited global gr used_copy = used_colors.copy() if gr[c] == [] or len(visited) == len(gr): return if c == -1: c = s visited.add(c) for elem in gr[c]: cur_color = p[tuple([c + 1, elem + 1])] used_copy.add(cur_color) if elem == s and len(used_copy) == 1: p[tuple([c + 1, elem + 1])] += 1 else: dfs(s, elem, used_copy) visited = set() n, m = mi() tuples = list() p = dict() gr = [[] for _ in range(n)] for _ in range(m): u, v = mi() gr[u - 1].append(v - 1) tuples.append(tuple([u, v])) p[tuple([u, v])] = 1 for s in range(len(gr)): if len(gr[s]) == 0: continue dfs(s, -1, set()) visited.clear() m = 0 s = '' for t in tuples: m = max(m, p[t]) s += str(p[t]) + ' ' print(m) print(s) ``` No
932
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` from math import gcd for step in range(int(input())): n=int(input()) P=[int(i)//100 for i in input().split()] x,a=map(int,input().split()) y,b=map(int,input().split()) k=int(input()) z=x+y c=(a*b)//gcd(a,b) P.sort(reverse=True) if x<y: x,y,a,b=y,x,b,a l,r=-1,len(P)+1 while r-l>1: m=(l+r)//2 w,ans=m//c,0 for q in range(w): ans+=P[q]*z for q in range(w,m//a): ans+=P[q]*x for q in range(m//a,m//a+m//b-w): ans+=P[q]*y if ans>=k: r=m else: l=m if r==len(P)+1: print(-1) else: print(r) ```
933
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` def nok(a, b): return (a * b) // gcd(a, b) def gcd(a, b): while b: a, b = b, a % b return a def test(z): global x, a, y, b, p, k ans = 0 xayb = z // nok(a, b) xa = z // a - xayb yb = z // b - xayb if x >= y: x1 = xa x2 = yb y1 = x y2 = y else: x1 = yb x2 = xa y1 = y y2 = x for i in range(xayb): ans += (p[i] // 100) * (x + y) for i in range(xayb, xayb + x1): ans += (p[i] // 100) * y1 for i in range(xayb + x1, xayb + x1 + x2): ans += (p[i] // 100) * y2 return ans >= k q = int(input()) for i in range(q): n = int(input()) p = list(map(int, input().split())) p.sort(reverse = True) x, a = map(int, input().split()) y, b = map(int, input().split()) k = int(input()) left = 0 right = n if not test(right): print(-1) else: while right - left > 1: mid = (left + right) // 2 if test(mid): right = mid else: left = mid print(right) ```
934
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` def gcd(a, b): if a*b == 0: return a+b return gcd(b, a%b) def f(): n = int(input()) p = list(map(lambda x:int(x)//100, input().split())) x, a = map(int, input().split()) y, b = map(int, input().split()) k = int(input()) p.sort(reverse = True) ps = [0] for i in range(0, n): ps.append(ps[-1] + p[i]) i = 0 j = n if x < y: x, y, a, b = y, x, b, a m = n ab = a*b//gcd(a, b) colab = m//ab cola = m//a - colab colb = m//b - colab res = (ps[colab]*(x+y) + (ps[colab + cola] - ps[colab])*x + (ps[colab+cola+colb] - ps[cola+colab])*y) if res < k: return -1 # print(p, ps, cola, colb, colab, k, res, ps[colab]*(x+y), (ps[colab + cola] - ps[colab])*x, (ps[colab+cola+colb] - ps[colab+cola])*y) while j - i > 1: m = (i+j)//2 colab = m//ab cola = m//a - colab colb = m//b - colab res = (ps[colab]*(x+y) + (ps[colab + cola] - ps[colab])*x + (ps[colab+cola+colb] - ps[cola+colab])*y) if res >= k: j = m else: i = m return j for i in range(int(input())): print(f()) ```
935
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from math import gcd def lcm(x,y): return x*y//gcd(x,y) def check(p,x,a,y,b,k,mid): z,i,su=lcm(a,b),0,0 while i<mid//z: su+=(p[i]*(x+y))//100 i+=1 j=0 while j<(mid//a)-(mid//z): su+=(p[i]*x)//100 i+=1 j+=1 j = 0 while j<(mid//b)-(mid//z): su +=(p[i]*y)//100 i += 1 j += 1 return su>=k def solve(): n = int(input()) p= sorted(map(int, input().split()),reverse=True) x, a = map(int, input().split()) y, b = map(int, input().split()) if y>x: x,a,y,b=y,b,x,a k = int(input()) lo,hi,ans=1,n,-1 while lo<=hi: mid=(lo+hi)//2 if check(p,x,a,y,b,k,mid): ans=mid hi=mid-1 else: lo=mid+1 return ans def main(): for _ in range(int(input())): print(solve()) # 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") if __name__ == "__main__": main() ```
936
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` def fun(n, p, x, y, xy_n, x_n, y_n): s, i = 0, n for _ in range(xy_n): s += p[i - 1] * (x + y) // 100 i -= 1 for _ in range(x_n): s += p[i - 1] * x // 100 i -= 1 for _ in range(y_n): s += p[i - 1] * y // 100 i -= 1 return s def solve(n, p, x, a, y, b, k): xy_n, x_n, y_n = [0], [0], [0] for i in range(1, n + 1): xy_n.append(xy_n[i - 1] + int(i % a == 0 and i % b == 0)) x_n.append(x_n[i - 1] + int(i % a == 0 and i % b != 0)) y_n.append(y_n[i - 1] + int(i % a != 0 and i % b == 0)) def f(l): return fun(n, p, x, y, xy_n[l], x_n[l], y_n[l]) l, r = 0, n + 1 while r - l > 1: mid = (l + r) // 2 if f(mid) >= k: r = mid else: l = mid if r > len(p): print(-1) else: print(r) if __name__ == '__main__': q = int(input()) for i in range(q): n = int(input()) p = list(map(int, input().split(' '))) p.sort() x, a = map(int, input().split(' ')) y, b = map(int, input().split(' ')) if y > x: x, y = y, x a, b = b, a k = int(input()) solve(n, p, x, a, y, b, k) ```
937
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` from collections import deque n = int(input()) for _ in range(n): m = int(input()) # number tickets tickets = list(map(int, input().split())) tickets.sort(reverse=True) aperc, ajump = map(int, input().split()) bperc, bjump = map(int, input().split()) q = int(input()) # req total if bperc > aperc: bperc, aperc = aperc, bperc ajump, bjump = bjump, ajump # a is bigger aperc = aperc/100 bperc = bperc/100 zet = False ans = -1 sumo = 0 da = deque() db = deque() dc = deque() tc = 0 for i in range(m): z = i+1 if z % ajump == 0 and z % bjump == 0: if(len(da) > 0): xx = da.pop() dc.appendleft(xx) if len(db) > 0: xxx = db.pop() da.appendleft(xxx) db.appendleft(tickets[tc]) sumo = sumo + bperc * xx + \ (aperc-bperc)*xxx + bperc*tickets[tc] else: da.appendleft(tickets[tc]) sumo = sumo + bperc * xx + aperc*tickets[tc] elif len(db) > 0: xx = db.pop() dc.appendleft(xx) db.appendleft(tickets[tc]) sumo = sumo + aperc * xx + bperc*tickets[tc] else: dc.appendleft(tickets[tc]) sumo = sumo + (aperc+bperc) * tickets[tc] tc += 1 elif z % ajump == 0: if len(db) > 0: xx = db.pop() da.appendleft(xx) db.appendleft(tickets[tc]) sumo = sumo + (aperc-bperc) * xx + bperc*tickets[tc] else: da.appendleft(tickets[tc]) sumo = sumo + aperc * tickets[tc] tc += 1 elif z % bjump == 0: db.appendleft(tickets[tc]) sumo = sumo + bperc * tickets[tc] tc += 1 else: pass # print(f"i:{i}, sum:{sumo}") # print(da) # print(db) # print(dc) # print(" ") if(sumo >= q and (not zet)): zet = True ans = i+1 break print(ans) ```
938
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` import math def check(l): global a global b global p global x global y global k s = [0] * l rec = 0 for i in range(l): index = i + 1 if (index % a == 0): s[i] += x if (index % b == 0): s[i] += y s = sorted(s)[::-1] for i in range(l): rec += p[i] / 100 * s[i] return rec >= k for q in range(int(input())): n = int(input()) p = list(map(int, input().split())) x, a = map(int, input().split()) y, b = map(int, input().split()) k = int(input()) p = sorted(p)[::-1] low = 0 high = n + 1 while low < high: mid = (low + high) // 2 if check(mid): high = mid else: low = mid + 1 if low > n: print(-1) else: print(low) # maxp = max(p) # minp = min(p) # maxans = maxp * (n // a) * (x / 100) + maxp * (n // b) * (y / 100) # minans = minp * (n // a) * (x / 100) + minp * (n // b) * (y / 100) # # print(p, maxans, minans) # if maxans < k: # print(-1) # return # ans = [] # c = 0 # for i in range(n): # index = i + 1 # if index % a == 0 or index % b == 0: # val = p.pop() # ans.append(val) # if index % a == 0 and index % b == 0: # c += val * ((x + y) / 100) # else: # if index % b == 0: # c += val * (y / 100) # else: # c += val * (x / 100) # else: # val = p.pop(0) # ans.append(val) # if c >= k: # break # # print(ans, c) # lcm = int((a * b) / math.gcd(a, b)) # if len(ans) >= lcm and lcm != 1: # for i in range(lcm-1, len(ans), lcm): # start = max(i-lcm, 0) # end = min(i, len(ans)) # index = ans.index(max(ans[:lcm-1]), 0, lcm-1) # ans[index], ans[i] = ans[i], ans[index] # c = 0 # for i in range(len(ans)): # val = ans[i] # index = i + 1 # if index % a == 0 and index % b == 0: # c += val * ((x + y) / 100) # else: # if index % b == 0: # c += val * (y / 100) # if index % a == 0: # c += val * (x / 100) # if c >= k: # ans = ans[:i+1] # break # # print(ans, c) # print(len(ans)) ```
939
Provide tags and a correct Python 3 solution for this coding contest problem. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Tags: binary search, greedy Correct Solution: ``` def gcd(x, y): if y: return gcd(y, x % y) return x for _ in range(int(input())): n = int(input()) a = [0] + sorted(map(int, input().split()))[::-1] for i in range(n): a[i + 1] += a[i] p1, x1 = map(int, input().split()) p2, x2 = map(int, input().split()) if p1 < p2: p1, x1, p2, x2 = p2, x2, p1, x1 g = gcd(x1, x2) xx = x1 // g * x2 def f(x): c1 = x // x1 c2 = x // x2 c3 = x // xx ret = a[c3] * (p1 + p2) ret += (a[c1] - a[c3]) * p1 ret += (a[c1 + c2 - c3] - a[c1]) * p2 return ret // 100 m = int(input()) lo, hi = 0, n while lo < hi: mid = (lo + hi) // 2 if f(mid) < m: lo = mid + 1 else: hi = mid print(-1 if f(lo) < m else lo) ```
940
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` from sys import stdin,stdout from math import gcd,sqrt from collections import deque input=stdin.readline R=lambda:map(int,input().split()) I=lambda:int(input()) S=lambda:input().rstrip('\n') L=lambda:list(R()) P=lambda x:stdout.write(x) hg=lambda x,y:((y+x-1)//x)*x pw=lambda x:1 if x==1 else 1+pw(x//2) chk=lambda x:chk(x//2) if not x%2 else True if x==1 else False N=10**10+7 def get_val(a): sm=0 for i in range(len(a)): sm+=a[i]*(v[i]//100) return sm for _ in range(I()): n=I() v=sorted(R(),reverse=True) x,a=R() y,b=R() k=I() p=[0]*n for i in range(a-1,n,a): p[i]+=x for i in range(b-1,n,b): p[i]+=y l=1 r=n ans=-1 while l<=r: m=(l+r)//2 if get_val(sorted(p[:m],reverse=True))>=k: ans=m r=m-1 else: l=m+1 print(ans) ``` Yes
941
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` from math import * def enough(f): sum=0 for j in range(1,f+1): if j<=f//(a*b//gcd(a,b)): sum+=(x+y)*p[j-1]//100 #elif j<=f//(a*b//gcd(a,b))+f//a: elif j<=f//a: sum+=x*p[j-1]//100 #elif j<=f//(a*b//gcd(a,b))+f//a+f//b: elif j<=f//a+f//b-f//(a*b//gcd(a,b)): sum+=y*p[j-1]//100 if sum>=k: return True else: return False q=int(input()) for i in range(q): n=int(input()) p=list(map(int, input().split())) x,a=map(int,input().split()) y,b=map(int,input().split()) k=int(input()) if x<y: x,y=y,x a,b=b,a p.sort(reverse=True) if enough(n): l=0 r=n+1 while r-l>1: m=(r+l)//2 if enough(m): r=m else: l=m print(r) else: print(-1) ``` Yes
942
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` import math import sys sys.setrecursionlimit(1000000) def lcm(a,b): return (a * b) // math.gcd(a,b) def f(prices, x, a, y, b, i): Nab = i // lcm(a,b) # i = 8, Nab = 1 Na = i // a - Nab # a=3, b=2. Na = 2-1=1 Nb = i // b - Nab # Nb = 4 - 1 = 3 result = 0 result2 = (sum(prices[:Nab]) * (x + y) + sum(prices[Nab:Nab+Na]) * x + sum(prices[Nab+Na:Nab + Na + Nb]) * y) // 100 for i in range(len(prices)): if (Nab > 0): result += (x + y) * (prices[i] // 100) Nab -= 1 elif (Na > 0): result += x * (prices[i] // 100) Na -= 1 elif (Nb > 0): result += y * (prices[i] // 100) Nb -= 1 if (Nab == 0) and (Na == 0) and (Nb == 0): break return result def BS(left, right): global prices, x, a, y, b mid = (left + right) // 2 res = f(prices, x, a, y, b, mid) if right - left <= 1: return -1 if res < k: return BS(mid, right) elif res >= k: if f(prices, x, a, y, b, mid - 1) >= k: return BS(left, mid - 1) else: return mid q = int(input()) results = [] for j in range(q): n = int(input()) prices = [int(i) for i in input().split()] prices.sort(reverse=True) xa = [int(i) for i in input().split()] yb = [int(i) for i in input().split()] k = int(input()) mass = [xa,yb] mass = sorted(mass, key = lambda a: a[0], reverse=True) x = mass[0][0] a = mass[0][1] y = mass[1][0] b = mass[1][1] left_b = 0 right_b = n + 1 while (right_b - left_b > 1): mid = (right_b + left_b) // 2 if (f(prices, x, a, y, b, mid) >= k): right_b = mid else: left_b = mid if (right_b > n): results.append(-1) else: results.append(right_b) #Π±ΠΈΠ½Π°Ρ€Π½Ρ‹ΠΉ поиск '''res = 0 days = 0 result = BS(1, len(prices)) print(result)''' for q in results: print(q) ``` Yes
943
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` def line(): return map(int, input().split()) def num(): return int(input()) from itertools import repeat def gcd(a,b): if a<b: a,b = b,a while b!=0: t=b b=a%b a=t return a def lcm(a,b): return a*b//gcd(a,b) q = num() for _ in repeat(None, q): n = num() p = sorted(line(), reverse=True) x,a = line() y,b = line() if x>y: x,a,y,b = y,b,x,a k = num() u=lcm(a,b) def f(i): ums=i//u ams,bms = i//a-ums, i//b-ums return sum(p[:ums])*(x+y)+sum(p[ums:ums+bms])*y+sum(p[ums+bms:ums+bms+ams])*x def cool(e): s = 1 ans=-1 while s<=e: m = (s+e)//2 d = f(m) if d<k*100: s=m+1 else: ans=m e=m-1 return ans print(cool(n+1)) ``` Yes
944
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` def solve(): n = int(input()) t = list(map(int,input().split())) x,a = map(int,input().split()) y,b = map(int,input().split()) k = int(input()) if x<y: aux = x x = y y = aux aux = a a = b b = aux ans = [0]*(n) for i in range(n): if (i+1)%a==0: ans[i] += x if (i+1)%b==0: ans[i] += y t.append(0) t.sort() t.reverse() for i in range(1,n): t[i]+=t[i-1] one = two = three = cur = 0 for i in range(n): cur = 0 if ans[i]==x+y: one+=1 if ans[i]==x: two+=1 if ans[i]==y: three+=1 if one>0: cur += (t[one-1])*(y+x)//100 if two>0: cur += (t[two+one-1]-t[one-1])*(x)//100 if three>0: cur += (t[two+one+three-1]-t[two+one-1])*(y)//100 if cur>=k: print(i+1) return print(-1) def main(): t = int(input()) for _ in range(t): solve() main() ``` No
945
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` q = int(input()) def answer(amount, prices, prog1, prog2, limit): prices = prices[0].split() prog1 = prog1[0].split() prog2 = prog2[0].split() pries_new = sorted(prices, reverse=True) prices_high = [] cash = 0 if int(prog1[0]) * (amount // int(prog1[1])) > int(prog2[0]) * (amount // int(prog2[1])): base = int(amount // int(prog1[1])) for g in range(1, len(prices) + 1): if g % int(prog1[1]) == 0: prices_high.append(pries_new.pop(0)) elif g % int(prog2[1]) == 0: prices_high.append(pries_new.pop(0)) else: prices_high.append(pries_new.pop(-1)) take1 = 0 take2 = 0 for h in range(1, amount + 1): if h % int(prog1[1]) == 0: cash += int(prog1[0]) * int(prices_high[take1]) // 100 take1 += 1 base -= 1 if h % int(prog2[1]) == 0: cash += int(prog2[0]) * int(prices_high[take2 + base]) // 100 take2 += 1 if cash >= limit: return h return -1 else: base = int(amount // int(prog2[1])) for g in range(1, len(prices) + 1): if g % int(prog2[1]) == 0: prices_high.append(pries_new.pop(0)) elif g % int(prog1[1]) == 0: prices_high.append(pries_new.pop(0)) else: prices_high.append(pries_new.pop(-1)) take1 = 0 take2 = 0 for h in range(1, amount + 1): if h % int(prog1[1]) == 0: cash += int(prog1[0]) * int(prices_high[take1]) // 100 base -= 1 take1 +=1 if h % int(prog2[1]) == 0: cash += int(prog2[0]) * int(prices_high[take2 + base]) // 100 take2 += 1 if cash >= limit: return h return -1 for i in range(q): print(answer(int(input()), [input()], [input()], [input()], int(input()))) ``` No
946
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` for _ in range(int(input())): n = int(input()) u = list(map(int, input().split())) u.sort() for i in range(n): u[i] //= 100 x, a = map(int, input().split()) y, b = map(int, input().split()) if y > x: x, a, y, b = y, b, x, a k = int(input()) sm = 0 ind = n - 1 last_x = -1 last_y = -1 ans = [0] * n ok = True for i in range(n): i1 = i + 1 if i1 % a != 0 and i1 % b != 0: ans[i] = sm #print("#", end = ' ') elif i1 % a != 0: if last_y == -1: last_y = ind sm += u[ind] * y ind -= 1 elif i1 % b != 0: if last_y == -1: if last_x == -1: last_x = ind sm += u[ind] * x ind -= 1 else: if last_x == -1: last_x = last_y sm += u[last_y] * x sm -= u[last_y] * y last_y -= 1 sm += u[ind] * y ind -= 1 else: if last_x == -1 and last_y == 1: sm += u[ind] * (x + y) ind -= 1 elif last_x == -1: sm += u[last_y] * x last_y -= 1 sm += u[ind] * y ind -= 1 elif last_y == -1: sm += u[last_x] * y last_x -= 1 sm + u[ind] * x ind -= 1 else: sm += u[last_x] * y last_x -= 1 sm += u[last_y] * x sm -= u[last_y] * y last_y -= 1 sm += u[ind] * y ind -= 1 ans[i] = sm if sm >= k: print(i1) ok = False break if ok: print(-1) ``` No
947
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: * The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. * The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding. For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β‹… 0 + 200 β‹… 0.1 + 300 β‹… 0.2 + 400 β‹… 0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β‹… 0 + 300 β‹… 0.1 + 400 β‹… 0.2 + 200 β‹… 0.1 = 130. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k. Input The first line contains a single integer q (1 ≀ q ≀ 100) β€” the number of independent queries. Each query consists of 5 lines. The first line of each query contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tickets. The second line contains n integers p_1, p_2, ..., p_n (100 ≀ p_i ≀ 10^9, p_i mod 100 = 0) β€” the corresponding prices of tickets. The third line contains two integers x and a (1 ≀ x ≀ 100, x + y ≀ 100, 1 ≀ a ≀ n) β€” the parameters of the first program. The fourth line contains two integers y and b (1 ≀ y ≀ 100, x + y ≀ 100, 1 ≀ b ≀ n) β€” the parameters of the second program. The fifth line contains single integer k (1 ≀ k ≀ 10^{14}) β€” the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed 2 β‹… 10^5. Output Print q integers β€” one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print -1. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Output -1 6 3 4 Note In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β‹… 0 + 100 β‹… 0.1 + 200 β‹… 0.15 + 200 β‹… 0.1 + 100 β‹… 0 + 200 β‹… 0.25 = 10 + 30 + 20 + 50 = 110. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β‹… 0 + 200 β‹… 0.31 + 100 β‹… 0 + 100 β‹… 0.31 = 62 + 31 = 93. Submitted Solution: ``` import math def solve(): n = int(input()) p = list(map(int, input().split())) x, a = map(int, input().split()) y, b = map(int, input().split()) k = int(input()) # print() p = sorted(p) maxp = max(p) minp = min(p) maxans = maxp * (n // a) * (x / 100) + maxp * (n // b) * (y / 100) minans = minp * (n // a) * (x / 100) + minp * (n // b) * (y / 100) # print(p, maxans, minans) if maxans < k: print(-1) return ans = [] c = 0 for i in range(n): index = i + 1 if index % a == 0 or index % b == 0: val = p.pop() ans.append(val) if index % a == 0 and index % b == 0: c += val * ((x + y) / 100) else: if index % b == 0: c += val * (y / 100) else: c += val * (x / 100) else: val = p.pop(0) ans.append(val) if c >= k: break print(ans, c) lcm = int((a * b) / math.gcd(a, b)) if len(ans) >= lcm and lcm != 1: for i in range(lcm-1, len(ans), lcm): start = max(i-lcm, 0) end = min(i, len(ans)) index = ans.index(max(ans[start:end]), start, end) ans[index], ans[i] = ans[i], ans[index] print(ans) c = 0 for i in range(len(ans)): val = ans[i] index = i + 1 if index % a == 0 and index % b == 0: c += val * ((x + y) / 100) else: if index % b == 0: c += val * (y / 100) if index % a == 0: c += val * (x / 100) if c >= k: ans = ans[:i+1] break print(ans, c) print(len(ans)) for q in range(int(input())): # print() solve() ``` No
948
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` for _ in range(int(input())): a, b = map(int, input().split()) if a < b: a, b = b, a if (b*2-a) % 3 == 0 and (b*2-a) >= 0: print("YES") else: print("NO") ```
949
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` t = int(input()) for i in range(t): a, b = map(int, input().split()) if 2*a-b >= 0 and 2*b-a >= 0 and (2*a-b) % 3 == 0: print('YES') else: print('NO') ```
950
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` # cook your dish here t = int(input()) for testcase in range(t): a, b = [int(ele) for ele in input().split()] s = min(a, b) l = max(a, b) if ((2*a-b)%3==0 or ((2*b-a)%3==0) ) and a<=2*b and b <= 2*a: # if (a+b)%3==0 and (a<=2*b and b<=2*a): print('YES') else: print('NO') ```
951
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` for qq in range(int(input())): a, b =map(int, input().split()) if (a+b) > 3*min(a,b): print("NO") elif (a+b) % 3 != 0: print("NO") continue else: if a==b==0: print("YES") elif a == 0 or b == 0: print("NO") else: print("YES") ```
952
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` for _ in range(int(input())): a, b = map(int, input().split()) if (a + b) % 3 == 0: if a >= b / 2 and b >= a / 2: print('YES') continue print('NO') ```
953
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` for i in range(int(input())): s, b = map(int, input().split()) if s > b: s, b = b, s if (s + b)%3==0 and s*2 >= b: print('Yes') else: print('No') ```
954
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` for t in range(int(input())): a, b = map(int, input().split()) if a > b: a, b = b, a print ('YES' if ( ((a + b) % 3) == 0 and (a * 2 - b)/3 >= 0) else 'NO') ```
955
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Tags: binary search, math Correct Solution: ``` def fastio(): import sys from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() def xrange(n): return range(1, n+1) MOD = 10**9 + 7 INF = 10**20 I = lambda:list(map(int,input().split())) from collections import defaultdict as dd from math import gcd import string import heapq for _ in range(int(input())): a, b = I() y = (2*a - b)//3 x = a - 2*y ans = 'NO' if x >= 0 and y >= 0 and a - x - 2*y == 0 and b - y - 2*x == 0: ans = 'YES' print(ans) ```
956
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` t = int(input()) for test in range(t): a,b = map(int,input().split()) if a*b == 0 and max(a,b) > 0: print('NO') else: if a == b: if a%3 == 0: print('YES') else: print('NO') else: if (2*a-b)%3 == 0 and 2*a >= b and 2*b >= a: print('YES') else: print('NO') ``` Yes
957
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` t = int(input()) for i in range(t): x, y = map(int, input().split()) if abs(x - y) > min(x, y): print("No") else: if (x + y) % 3 == 0: print("Yes") else: print("No") ``` Yes
958
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` for _ in range(int(input())): a=list(map(int,input().split())) a.sort() if (a[0]+a[1])%3==0 and a[0]*2>=a[1]: print("YES") else: print("NO") ``` Yes
959
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` n=int(input()) lst=[] for i in range(n): lst1=list(map(int,input().split())) lst.append(lst1) for i in range(n): a=lst[i][0] b=lst[i][1] if a>b: a,b=b,a if b==2*a: print('yes') else: c=b-a if (a-c)%3==0 and a-c>=0: print('yes') else: print('no') ``` Yes
960
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` import math t=int(input()) for i in range(t): s=input().split() a=int(s[0]) b=int(s[1]) x=min(a,b) y=max(a,b) if( y > 2*x): print("NO") else: hc= math.gcd(x,y) if x==y==2: print("NO") elif hc!=1: print("YES") else: af=0 if (x+y-3)%3==0 and (x+y-3)/3 >=0: print("YES") else: print("NO") ``` No
961
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` """ NTC here """ from sys import stdin, setrecursionlimit setrecursionlimit(10**6) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) # range = xrange # input = raw_input # print("Case #{}: {} {}".format(i, n + m, n * m)) def check(a,b): x=(2*a-b)/3 return int(x*100000)==int(x)*100000 def main(): n=iin() for _ in range(n): a,b=lin() if a==2*b or b==a*2:print('YES') else: if check(a,b) and check(b,a):print('YES') else:print('NO') main() ``` No
962
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` # RawCoder : https://bit.ly/RCyouTube # Author : MehulYK for i in range(int(input())): a, b = map(int, input().split()) if((a + b) % 3 == 0): print("yes") else: print("no") ``` No
963
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers a and b. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations. Is it possible to make a and b equal to 0 simultaneously? Your program should answer t independent test cases. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0 ≀ a, b ≀ 10^9). Output For each test case print the answer to it β€” YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 3 6 9 1 1 1 2 Output YES NO YES Note In the first test case of the example two operations can be used to make both a and b equal to zero: 1. choose x = 4 and set a := a - x, b := b - 2x. Then a = 6 - 4 = 2, b = 9 - 8 = 1; 2. choose x = 1 and set a := a - 2x, b := b - x. Then a = 2 - 2 = 0, b = 1 - 1 = 0. Submitted Solution: ``` for _ in range(int(input())): x,y =map(int, input().split()) if (x==0 and y) or (y==0 and x): print('NO') else: print('NO' if (x+y)%3 else 'YES') ``` No
964
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings Correct Solution: ``` for _ in range(int(input())): n, p, k = map(int, input().strip().split()) A = list(sorted([int(x) for x in input().strip().split()])) dp = [0] * k dp[0] = 0 r = 0 for i in range(1, k): dp[i] = A[i-1] + dp[i-1] if (dp[i] <= p): r = i for i in range(k): s = dp[i] j = i+k while j <= n and s + A[j-1] <= p: r = max(j,r) s += A[j-1] j = j+k print(r) ```
965
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings Correct Solution: ``` t=int(input()) import sys input=sys.stdin.readline while t>0: t-=1 n,p,k=map(int,input().split()) a=[int(x) for x in input().split()] a.sort() dp=[0 for i in range(n+1)] for i in range(1,n+1): if i-k>=0: dp[i]=a[i-1]+dp[i-k] else: dp[i]=dp[i-1]+a[i-1] flag=0 z=0 for i in range(1,n+1): if dp[i]>p: pass else: z=i print(z) ```
966
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings Correct Solution: ``` t=int(input()) for _ in range(t): n,p,k=map(int,input().split()) a=list(map(int,input().split())) a.sort() ans=0 dp=[0]*n for i in range(n): if i>=k-1: dp[i]=a[i]+dp[i-k] if dp[i]<=p: ans=i+1 else: dp[i]=a[i]+dp[i-1] if dp[i]<=p: ans=max(ans,i+1) print(ans) ```
967
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings 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 input(): return stdin.readline().strip() 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---############################################### # num = int(z()) # for _ in range(num): # n,p,k=zzz() # arr = szzz() # dp=[0]*n # for i in range(k): # dp[i]=arr[i]+dp[i-1] # for j in range(k,n): # dp[j]=(dp[j-k]+arr[j]) # print(bisect(dp,p)) num = int(z()) for _ in range(num): n, p, k = zzz() arr = szzz() dp = [0]*(n+1) dp[0] = 0 for i in range(1, n+1): dp[i] = dp[i-1]+arr[i-1] if i>=k: dp[i] = dp[i-k]+arr[i-1] # print(dp) for i in range(n, -1, -1): if dp[i]<=p: print(i) break ```
968
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings Correct Solution: ``` t = int(input()) for i in range(t): n, p, k = map(int, input().split()) goods = list(map(int, input().split())) goods.sort() if goods[0] > p: print(0) continue elif len(goods) == 1: print(1) continue sums = [[0]] for i in range(k - 1): sums.append([sums[-1][0] + goods[i]]) #print(sums) for i in range(k, n + 1): sums[i % k].append(sums[i % k][-1] + goods[i - 1]) #print("Sums is", sums) for i in range(n - 1, -1, -1): #print("i =", i) cost = sums[(i + 1) % k][(i + 1) // k] if cost <= p: print(i + 1) break ```
969
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings Correct Solution: ``` for _ in range(int(input())): n, p, k = map(int, input().split()) a = [int(i) for i in input().split()] a.sort() prefix_sums = [0] for a_i in a[:k]: prefix_sums.append(a_i + prefix_sums[-1]) max_goods = 0 for ind, start_sum in enumerate(prefix_sums): balance = p - start_sum local_goods = ind while ind + k <= n and a[ind + k - 1] <= balance: balance -= a[ind + k - 1] ind += k local_goods += k if balance >= 0: max_goods = max(max_goods, local_goods) print(max_goods) ```
970
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings Correct Solution: ``` T = int(input()) INF = float('inf') for _ in range(T): N,P,K = map(int, input().split()) arr = sorted([int(x) for x in input().split()]) dp = [None] * N if arr[0] > P: print(0) continue else: dp[0] = P-arr[0] for i in range(1,N): a = dp[i-1] b = dp[i-K] if i-K >= 0 else (P if i-K == -1 else -INF) dp[i] = max(a,b) - arr[i] best = 0 for i,x in enumerate(dp): if x >= 0: best = i print(best+1) ```
971
Provide tags and a correct Python 3 solution for this coding contest problem. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Tags: dp, greedy, sortings Correct Solution: ``` def idp(a,dp,i,k): if (i-k) >= 0: return min(a[i]+dp[i-1], a[i]+dp[i-k]) else: return a[i]+dp[i-1] t = int(input()) for tc in range(t): n,p,k = map(int, input().split()) a = [int(x) for x in input().split()] a.sort() dp = [0]*n dp[0] = a[0] for i in range(0,k-1): dp[i] = a[i]+dp[i-1] dp[k-1] = a[k-1] for i in range(k,n): dp[i] = idp(a,dp,i,k) bst = 0 for i in range(n): if dp[i] <= p: bst = i+1 print(bst) ```
972
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` from sys import stdin from collections import deque mod = 10**9 + 7 import sys # def rl(): # return [int(w) for w in stdin.readline().split()] from bisect import bisect_right from bisect import bisect_left from collections import defaultdict from math import sqrt,factorial,gcd,log2,inf,ceil # map(int,input().split()) # # l = list(map(int,input().split())) # from itertools import permutations import heapq t = int(input()) for _ in range(t): n,p,k = map(int,input().split()) l = list(map(int,input().split())) l.sort() dp = [0]*(n+10) ans = 0 for i in range(k-1): dp[i] = l[i] + dp[i-1] if dp[i]<=p: ans = i+1 for i in range(k-1,n): dp[i] = l[i] + dp[i-k] if dp[i]<=p: ans = i+1 print(ans) ``` Yes
973
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` t =int(input()) while t!=0: n,p,k = map(int,input().split()) list1 = list(map(int,input().split())) list1.sort() cost = [0]*(n+1) for i in range(1,n+1): if i<k: cost[i] = cost[i-1]+list1[i-1] else: cost[i] = cost[i-k]+list1[i-1] ans=0 for i in range(n+1): if cost[i]<=p: ans=i print(ans) t-=1 ``` Yes
974
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` # in competition approach: timed out # n rows and p + 1 columns # dp[i][j] = how many goods vasya can buy from subset a_i - a_n starting with j coins # better approach # array of size n # dp[i] is cheapest cost to buy first i + 1 goods # base case: for all j in first k -> dp[j] = a_j + a_j-1 # return i + 1 of largest dp[i] <= p for j in range(int(input())): n, p, k = [int(x) for x in input().split()] goods = [int(x) for x in input().split()] goods.sort() dp = [0] * n dp[0] = goods[0] if dp[0] > p: print(0) continue for i in range(1, k - 1): dp[i] = goods[i] + dp[i - 1] for i in range(k - 1, n): dp[i] = goods[i] + min(dp[i - 1], dp[i - k]) res = 0 for i in range(0, n): if dp[i] <= p: res = i + 1 print(res) ``` Yes
975
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` T = int(input()) for _ in range(T): n, p, k = map(int, input().split()) li = list(map(int, input().split())) li.sort() pref = 0 ans = 0 sum = 0 for i in range(k): sum = pref if sum > p: break cnt = i for j in range(i+k-1, n, k): if sum + li[j] <= p: cnt += k sum += li[j] else: break pref += li[i] ans = max(cnt, ans) print(ans) ``` Yes
976
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` for _ in range(int(input())): n, p, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() c = [0] * k co = p for j in range(k-1, len(a), k): if a[j] <= co: co -= a[j] c[0] += k else: break for i in range(1, k): co = p if a[i-1] <= co: c[i] += i co -= a[i-1] else: continue for j in range(k+i-1, len(a), k): if a[j] <= co: co -= a[j] c[i] += k else: break print(max(c)) ``` No
977
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` import sys import math from collections import defaultdict,Counter import bisect # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") 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") # sys.stdout=open("CP1/output.txt",'w') # sys.stdin=open("CP1/input.txt",'r') # mod=pow(10,9)+7 t=int(input()) for i in range(t): n,p,k=map(int,input().split()) a=list(map(int,input().split())) pre=[0]*(n+1) pre[0]=a[0] for j in range(1,n): pre[j]=pre[j-1]+a[j] p1=p main=0 a.sort() for kk in range(k): ans=0 if p>=pre[kk-1]: p-=pre[kk-1] ans+=kk for j in range(k-1+kk,n,k): if p-a[j]>=0: ans+=k p-=a[j] else: for jj in range(j-k+1,j): if p>=a[jj]: p-=a[jj] ans+=1 else: break break main=max(main,ans) p=p1 else: break print(main) ``` No
978
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` from collections import defaultdict, Counter from bisect import bisect, bisect_left from math import sqrt, gcd, ceil, factorial from heapq import heapify, heappush, heappop MOD = 10**9 + 7 inf = float("inf") ans_ = [] def nin():return int(input()) def ninf():return int(file.readline()) def st():return (input().strip()) def stf():return (file.readline().strip()) def read(): return list(map(int, input().strip().split())) def readf():return list(map(int, file.readline().strip().split())) ans_ = [] # file = open("input.txt", "r") def solve(): for _ in range(nin()): n,p,k = read(); arr = read() arr.sort() pref = [0]*n pref[0] = arr[0] ans = 0 for i in range(n): pref[i] = max(pref[i-1], arr[i]+(pref[i-k] if i >= k else 0)) if pref[i] <= p: ans = max(ans, i+1) else: break print(ans) # file.close() solve() for i in ans_:print(i) ``` No
979
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is the hard version of this problem. The only difference is the constraint on k β€” the number of gifts in the offer. In this version: 2 ≀ k ≀ n. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β€” today the offer "k of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by a_i β€” the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: * Vasya can buy one good with the index i if he currently has enough coins (i.e p β‰₯ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p β‰₯ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). Please note that each good can be bought no more than once. For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. The next lines contain a description of t test cases. The first line of each test case contains three integers n, p, k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ p ≀ 2β‹…10^9, 2 ≀ k ≀ n) β€” the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains n integers a_i (1 ≀ a_i ≀ 10^4) β€” the prices of goods. It is guaranteed that the sum of n for all test cases does not exceed 2 β‹… 10^5. Output For each test case in a separate line print one integer m β€” the maximum number of goods that Vasya can buy. Example Input 8 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 3 2 3 4 2 6 5 2 3 10 1 3 9 2 2 10000 2 10000 10000 2 9999 2 10000 10000 4 6 4 3 2 3 2 5 5 3 1 2 2 1 2 Output 3 4 1 1 2 0 4 5 Submitted Solution: ``` import os from io import BytesIO input = BytesIO(os.read(0, os.fstat(0).st_size)).readline def check(x, p): i = x - 1 while i > -1 and a[i] <= p: p -= a[i] if i >= k - 1: i -= k else: i -= 1 return i <= -1 for _ in range(int(input())): n, p, k = map(int, input().split()) a = sorted(map(int, input().split())) if check(k, p): L = k R = n + 1 else: L = 0 R = k + 1 while R - L > 1: mid = (L + R) >> 1 if check(mid, p): L = mid else: R = mid print(L) ``` No
980
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` n, k = map(int, input().split()) p = list(map(int, input().split())) a, cnt, ans, MOD = [], 1, 0, 998244353 for i in range(len(p)): if p[i] > n-k: a.append(i) ans += p[i] for i in range(1, len(a)): cnt = cnt % MOD * (a[i]-a[i-1]) % MOD % MOD print(ans, cnt) ```
981
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` n,k = [int(i) for i in input().split(' ')] a = [int(i) for i in input().split(' ')] print(int(0.5*n*(n+1)-0.5*(n-k)*(n-k+1)),end=' ') Is = [] for i in range(n): if a[i] > n-k: Is.append(i) com = 1 for j in range(1,k): com*= (Is[j]-Is[j-1]) com %= 998244353 print(com) ```
982
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` import sys; def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() n,k = map(int,input().split()); mod = 998244353; arr = get_array(); s = 0; for i in range(n-k+1,n+1): s+=i; iarr = []; for i in range(n): if(arr[i]>=(n-k+1)): iarr.append(i); no = 1; for i in range(1,len(iarr)): no = ((no%mod)*(iarr[i]-iarr[i-1]))%mod print(s,no); ```
983
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` import sys def mmul(a,b,m): return ((a%m)*(b%m))%m [n,k]=[int(i) for i in sys.stdin.readline().split()] p=[int(j) for j in sys.stdin.readline().split()] a=[] val=n ans=0 for i in range(k): ans+=val val-=1 m=998244353 check=n-(k-1) ways=1 ctr=-2 for g in range(n): if(p[g]>=check): if(ctr<0): ctr=1 else: ways=mmul(ways,ctr,m) ctr=1 else: if(ctr>0): ctr+=1 print(ans,ways) ```
984
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` n,k = map(int,input().split()) lst = list(map(int,input().split())) inde = [] su = 0 for j in range(n): if lst[j] >= n-k+1: su += lst[j] inde.append(j) x = 1 for r in range(len(inde)-1): x *= inde[r+1]-inde[r] x %= 998244353 print(su,x) ```
985
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` n,k = map(int,input().split()) p = list(map(int,input().split())) ans1 = k*(n-k+1+n)//2 t = [] for i,j in enumerate(p): if j>=n-k+1 and j<=n: t.append(i) ans2=1 for i in range(len(t)-1): ans2 *= t[i+1]-t[i] ans2 %= 998244353 print(ans1,ans2) ```
986
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` def main(): n, k = map(int, input().split()) p = list(map(int, input().split())) new_p = [] for i in range(n): new_p.append([p[i], i]) new_p = sorted(new_p, key=lambda x: x[0], reverse=True) new_p = new_p[0:k] new_p = sorted(new_p, key=lambda x: x[1]) sm = 0 r = 1 for i in range(0, k - 1): sm += new_p[i][0] r *= new_p[i + 1][1] - new_p[i][1] r = r % 998244353 sm += new_p[-1][0] print(sm, r) return if __name__ == "__main__": main() ```
987
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Tags: combinatorics, greedy, math Correct Solution: ``` R=lambda:map(int,input().split()) n,k=R() a,b=zip(*sorted(zip(R(),range(n)))[-k:]) b=sorted(b) r=1 for i,j in zip(b,b[1:]):r=r*(j-i)%998244353 print(sum(a),r) ```
988
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` import os import sys M = 998244353 def Partitions(n, k, llist): global M ans1 = 0; ans2 = 1; Set = set(); res = [] for i in range(k): ans1 += n - i; Set.add(n-i); # find ans2 for i in range(len(llist)): if llist[i] in Set: res.append(i) for j in range(0, len(res)-1): ans2 *= (res[j+1]- res[j])%M return [ans1, ans2%M] if __name__ == '__main__': n, k = tuple(map(int, input().split())) llist = list( map(int, input().split()) ) result = Partitions(n, k, llist) print(*result) ``` Yes
989
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` n,k = map(int,input().split()) arr = list(map(int,input().split())) ab = (k*((2*n)-(k-1)))//2 vb=-1;ans=1;ct=0 for i in range(n): if(arr[i]>(n-k)): if(vb!=-1): ans*=(ct+1) ans%=998244353 else: vb=0 ct=0 else: ct+=1 print("%d %d"%(ab,ans)) ``` Yes
990
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` from sys import stdin, stdout n, k = map(int, stdin.readline().split(" ")) array = list(map(int, stdin.readline().split(" "))) indices = [] for i in range(n): if n - k + 1 <= array[i] <= n: indices.append(i) MOD = 998_244_353 s = array[indices[0]] counter = 1 for i in range(1, k): counter = (counter * (indices[i] - indices[i - 1])) % MOD s += array[indices[i]] stdout.write(str(s) + " " + str(counter)) ``` Yes
991
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` from collections import defaultdict as dd from collections import deque import bisect import heapq def ri(): return int(input()) def rl(): return list(map(int, input().split())) def T(n): return n * (n + 1) // 2 def S(a, b): return T(b) - T(a - 1) def solve(): n, k = rl() P = rl() mod = 998244353 M = S(n - k + 1, n) low = n - k + 1 prev = -1 ways = 1 for i, p in enumerate(P): if p >= low: if prev != -1: ways = (ways * (i - prev)) % mod prev = i print (M, ways) mode = 's' if mode == 'T': t = ri() for i in range(t): solve() else: solve() ``` Yes
992
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` # Work hard, be kind, and amazing things will happen. Conan O'Brien # by : Blue Edge - Create some chaos n,k=list(map(int,input().split())) a=list(map(int,input().split())) b=a[:] # for x in a: b.sort(reverse=True) maxi=sum(b[:k]) c=[] for i in range(n): x=a[i] if x>n-k: c.append(i) c.sort() # print(*c) total=1 for i in range(1,len(c)): total*=(c[i]-c[i-1]) print(maxi,total) ``` No
993
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` import math n,k=map(int,input().split()) l=list(map(int,input().split())) ans=(n-k)*k+(((k)*(k+1))//2) fa=1 a1=0 a2=0 i=0 while(i<n): if l[i]>n-k and i+1<n and l[i+1]>n-k: i+=2 continue elif l[i]>n-k: a1=i a2=i i+=1 while (a1==a2 and i<n): if l[i]>n-k: a2=i else: i+=1 if a2-a1>0: fa=fa*(a2-a1) else: i+=1 print(ans,fa) ``` No
994
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` n, k = list(map(int, input().split())) ls = list(map(int, input().split())) answer = 0 count = 1 stack = [] for i in range(n): if ls[i] >= n-k+1: if stack: count *= i - stack[-1] stack.append(i) answer += ls[i] print(stack) print(answer, count%998244353 ) ``` No
995
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, …, p_n of integers from 1 to n and an integer k, such that 1 ≀ k ≀ n. A permutation means that every number from 1 to n is contained in p exactly once. Let's consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments \{[l_1, r_1], [l_2, r_2], …, [l_k, r_k]\}, such that: * 1 ≀ l_i ≀ r_i ≀ n for all 1 ≀ i ≀ k; * For all 1 ≀ j ≀ n there exists exactly one segment [l_i, r_i], such that l_i ≀ j ≀ r_i. Two partitions are different if there exists a segment that lies in one partition but not the other. Let's calculate the partition value, defined as βˆ‘_{i=1}^{k} {max_{l_i ≀ j ≀ r_i} {p_j}}, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998 244 353. Input The first line contains two integers, n and k (1 ≀ k ≀ n ≀ 200 000) β€” the size of the given permutation and the number of segments in a partition. The second line contains n different integers p_1, p_2, …, p_n (1 ≀ p_i ≀ n) β€” the given permutation. Output Print two integers β€” the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998 244 353. Please note that you should only find the second value modulo 998 244 353. Examples Input 3 2 2 1 3 Output 5 2 Input 5 5 2 1 5 3 4 Output 15 1 Input 7 3 2 7 3 1 5 4 6 Output 18 6 Note In the first test, for k = 2, there exists only two valid partitions: \{[1, 1], [2, 3]\} and \{[1, 2], [3, 3]\}. For each partition, the partition value is equal to 2 + 3 = 5. So, the maximum possible value is 5 and the number of partitions is 2. In the third test, for k = 3, the partitions with the maximum possible partition value are \{[1, 2], [3, 5], [6, 7]\}, \{[1, 3], [4, 5], [6, 7]\}, \{[1, 4], [5, 5], [6, 7]\}, \{[1, 2], [3, 6], [7, 7]\}, \{[1, 3], [4, 6], [7, 7]\}, \{[1, 4], [5, 6], [7, 7]\}. For all of them, the partition value is equal to 7 + 5 + 6 = 18. The partition \{[1, 2], [3, 4], [5, 7]\}, however, has the partition value 7 + 3 + 6 = 16. This is not the maximum possible value, so we don't count it. Submitted Solution: ``` n, k = map(int, input().split()) p = list(map(int, input().split())) l = sorted(p) sum1 = 0 for i in range(n-1, n-k-1, -1): sum1+=l[i] print(sum1, end=" ") pos = [] left = l[n-k] right = l[n-1] for i in range(n): if left<=p[i]<=right: pos.append(i) res = 1 lent = len(pos) for i in range(1, lent): res*=(pos[i]-pos[i-1]) print(res) ``` No
996
Provide tags and a correct Python 3 solution for this coding contest problem. A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h-1 onto a base. A base consists of h pyramids of height 1, and h-1 cards on top. For example, card pyramids of heights 1, 2, and 3 look as follows: <image> You start with n cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed? Input Each test consists of multiple test cases. The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next t lines contain descriptions of test cases. Each test case contains a single integer n (1≀ n≀ 10^9) β€” the number of cards. It is guaranteed that the sum of n over all test cases does not exceed 10^9. Output For each test case output a single integer β€” the number of pyramids you will have constructed in the end. Example Input 5 3 14 15 24 1 Output 1 2 1 3 0 Note In the first test, you construct a pyramid of height 1 with 2 cards. There is 1 card remaining, which is not enough to build a pyramid. In the second test, you build two pyramids, each of height 2, with no cards remaining. In the third test, you build one pyramid of height 3, with no cards remaining. In the fourth test, you build one pyramid of height 3 with 9 cards remaining. Then you build a pyramid of height 2 with 2 cards remaining. Then you build a final pyramid of height 1 with no cards remaining. In the fifth test, one card is not enough to build any pyramids. Tags: binary search, brute force, dp, math Correct Solution: ``` def pyr(n): return 3 * int(0.5 * (n + 1) * n) - n def bs(l, r, n): #print(pyr(l), pyr(r)) if pyr(r) == n: return r if l == r - 1: return l elif pyr((l + r) // 2) >= n: return bs(l, (r + l) // 2 , n) return bs((r + l) // 2, r, n) for something in range(int(input())): n = int(input()) ans = 0 while n >= 2: bs_res = bs(1, 26000, n) n -= pyr(bs_res) #print(n, bs_res, pyr(bs_res)) ans += 1 print(ans) ```
997
Provide tags and a correct Python 3 solution for this coding contest problem. A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h-1 onto a base. A base consists of h pyramids of height 1, and h-1 cards on top. For example, card pyramids of heights 1, 2, and 3 look as follows: <image> You start with n cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed? Input Each test consists of multiple test cases. The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next t lines contain descriptions of test cases. Each test case contains a single integer n (1≀ n≀ 10^9) β€” the number of cards. It is guaranteed that the sum of n over all test cases does not exceed 10^9. Output For each test case output a single integer β€” the number of pyramids you will have constructed in the end. Example Input 5 3 14 15 24 1 Output 1 2 1 3 0 Note In the first test, you construct a pyramid of height 1 with 2 cards. There is 1 card remaining, which is not enough to build a pyramid. In the second test, you build two pyramids, each of height 2, with no cards remaining. In the third test, you build one pyramid of height 3, with no cards remaining. In the fourth test, you build one pyramid of height 3 with 9 cards remaining. Then you build a pyramid of height 2 with 2 cards remaining. Then you build a final pyramid of height 1 with no cards remaining. In the fifth test, one card is not enough to build any pyramids. Tags: binary search, brute force, dp, math Correct Solution: ``` import math test = int(input()) for tes in range(test): n = int(input()) cnt=0 if (n==0 or n==1): print(0) continue while(True): sq = math.sqrt(1+24*n) - 1 h = sq//6 n = n- ((h)*(3*h+1))/2 cnt+=1 if(n==1 or n==0): break print(cnt) ```
998
Provide tags and a correct Python 3 solution for this coding contest problem. A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h-1 onto a base. A base consists of h pyramids of height 1, and h-1 cards on top. For example, card pyramids of heights 1, 2, and 3 look as follows: <image> You start with n cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed? Input Each test consists of multiple test cases. The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next t lines contain descriptions of test cases. Each test case contains a single integer n (1≀ n≀ 10^9) β€” the number of cards. It is guaranteed that the sum of n over all test cases does not exceed 10^9. Output For each test case output a single integer β€” the number of pyramids you will have constructed in the end. Example Input 5 3 14 15 24 1 Output 1 2 1 3 0 Note In the first test, you construct a pyramid of height 1 with 2 cards. There is 1 card remaining, which is not enough to build a pyramid. In the second test, you build two pyramids, each of height 2, with no cards remaining. In the third test, you build one pyramid of height 3, with no cards remaining. In the fourth test, you build one pyramid of height 3 with 9 cards remaining. Then you build a pyramid of height 2 with 2 cards remaining. Then you build a final pyramid of height 1 with no cards remaining. In the fifth test, one card is not enough to build any pyramids. Tags: binary search, brute force, dp, math Correct Solution: ``` nums=[] def seive(): for i in range(1,100000): x=((3*i+1)*i)//2 nums.append(x) def binarySearch(arr, n, key): left = 0 right = n mid = 0 while (left < right): mid = (right + left) // 2 if (arr[mid] == key): while (mid + 1 < n and arr[mid + 1] == key): mid += 1 break elif (arr[mid] > key): right = mid else: left = mid + 1 while (mid > -1 and arr[mid] > key): mid -= 1 return mid seive() for _ in range(int(input())): num=int(input()) if(num<2): print(0) else: ans=0 while(num>1): index=binarySearch(nums,len(nums),num) y=nums[index] ans+=(num//y) num=num%y print(ans) ```
999