message
stringlengths
2
30.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
237
109k
cluster
float64
10
10
__index_level_0__
int64
474
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia? There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i. Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day. Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i. The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection. However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though. Note that it is possible for some values of a_i to coincide in a solution. Input The first line contains integers m and n (1≀ m ≀ 50, 1≀ n ≀ 10^4) β€” the number of days and the number of stores. After this m lines follow, the i-th line starts with an integer s_i (1≀ s_i ≀ n-1), the number of integers Dora bought on day i, followed by s_i distinct integers, the indices of the stores where Dora bought an integer on the i-th day. The indices are between 1 and n. Output Output must consist of a single line containing "possible" if there exist positive integers a_i such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves. Examples Input 2 5 3 1 2 3 3 3 4 5 Output possible Input 10 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Output impossible Note In the first sample, a possible choice for the values of the a_i is 3, 4, 3, 5, 2. On the first day, Dora buys the integers 3, 4 and 3, whose LCM is 12, while Swiper buys integers 5 and 2, whose LCM is 10. On the second day, Dora buys 3, 5 and 2, whose LCM is 30, and Swiper buys integers 3 and 4, whose LCM is 12. Submitted Solution: ``` inp = lambda cast=int: list(map(cast, input().split())) printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs) m, n = inp() A = [] for _ in range(m ): l = inp() A.append(set(l[1:])) for i in range(m): for j in range(m): if i == j: continue if not A[i].intersection(A[j]): print('impossible') exit(0) print('possible') ```
instruction
0
46,288
10
92,576
Yes
output
1
46,288
10
92,577
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia? There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i. Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day. Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i. The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection. However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though. Note that it is possible for some values of a_i to coincide in a solution. Input The first line contains integers m and n (1≀ m ≀ 50, 1≀ n ≀ 10^4) β€” the number of days and the number of stores. After this m lines follow, the i-th line starts with an integer s_i (1≀ s_i ≀ n-1), the number of integers Dora bought on day i, followed by s_i distinct integers, the indices of the stores where Dora bought an integer on the i-th day. The indices are between 1 and n. Output Output must consist of a single line containing "possible" if there exist positive integers a_i such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves. Examples Input 2 5 3 1 2 3 3 3 4 5 Output possible Input 10 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Output impossible Note In the first sample, a possible choice for the values of the a_i is 3, 4, 3, 5, 2. On the first day, Dora buys the integers 3, 4 and 3, whose LCM is 12, while Swiper buys integers 5 and 2, whose LCM is 10. On the second day, Dora buys 3, 5 and 2, whose LCM is 30, and Swiper buys integers 3 and 4, whose LCM is 12. Submitted Solution: ``` m,n = list(map(int,input().split())) a = [0]*m for i in range(m): k = list(map(int,input().split())) for j in range(k[0]): a[i] |= 1<<k[j+1] for i in range(m): for j in range(m): if a[i]&a[j] == 0: print("impossible") quit() print("possible") ```
instruction
0
46,289
10
92,578
Yes
output
1
46,289
10
92,579
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia? There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i. Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day. Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i. The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection. However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though. Note that it is possible for some values of a_i to coincide in a solution. Input The first line contains integers m and n (1≀ m ≀ 50, 1≀ n ≀ 10^4) β€” the number of days and the number of stores. After this m lines follow, the i-th line starts with an integer s_i (1≀ s_i ≀ n-1), the number of integers Dora bought on day i, followed by s_i distinct integers, the indices of the stores where Dora bought an integer on the i-th day. The indices are between 1 and n. Output Output must consist of a single line containing "possible" if there exist positive integers a_i such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves. Examples Input 2 5 3 1 2 3 3 3 4 5 Output possible Input 10 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Output impossible Note In the first sample, a possible choice for the values of the a_i is 3, 4, 3, 5, 2. On the first day, Dora buys the integers 3, 4 and 3, whose LCM is 12, while Swiper buys integers 5 and 2, whose LCM is 10. On the second day, Dora buys 3, 5 and 2, whose LCM is 30, and Swiper buys integers 3 and 4, whose LCM is 12. Submitted Solution: ``` m, n = map(int, input().split()) D = [] for i in range(m): A = list(map(int, input().split())) D.append(set(A[1:])) for i in range(m): for j in range(i + 1, m): if D[i].intersection(D[j]) == set(): print('impossible') exit(0) print('possible') ```
instruction
0
46,290
10
92,580
Yes
output
1
46,290
10
92,581
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia? There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i. Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day. Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i. The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection. However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though. Note that it is possible for some values of a_i to coincide in a solution. Input The first line contains integers m and n (1≀ m ≀ 50, 1≀ n ≀ 10^4) β€” the number of days and the number of stores. After this m lines follow, the i-th line starts with an integer s_i (1≀ s_i ≀ n-1), the number of integers Dora bought on day i, followed by s_i distinct integers, the indices of the stores where Dora bought an integer on the i-th day. The indices are between 1 and n. Output Output must consist of a single line containing "possible" if there exist positive integers a_i such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves. Examples Input 2 5 3 1 2 3 3 3 4 5 Output possible Input 10 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Output impossible Note In the first sample, a possible choice for the values of the a_i is 3, 4, 3, 5, 2. On the first day, Dora buys the integers 3, 4 and 3, whose LCM is 12, while Swiper buys integers 5 and 2, whose LCM is 10. On the second day, Dora buys 3, 5 and 2, whose LCM is 30, and Swiper buys integers 3 and 4, whose LCM is 12. Submitted Solution: ``` m,n=map(int,input().split()) ans=[] ans=set(ans) for i in range(m): x=list(map(int,input().split())) x=set(x[1:]) if i==0: ans=ans | x else : ans=ans & x if len(ans)>0: print("possible") else: print("impossible") ```
instruction
0
46,291
10
92,582
No
output
1
46,291
10
92,583
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia? There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i. Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day. Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i. The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection. However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though. Note that it is possible for some values of a_i to coincide in a solution. Input The first line contains integers m and n (1≀ m ≀ 50, 1≀ n ≀ 10^4) β€” the number of days and the number of stores. After this m lines follow, the i-th line starts with an integer s_i (1≀ s_i ≀ n-1), the number of integers Dora bought on day i, followed by s_i distinct integers, the indices of the stores where Dora bought an integer on the i-th day. The indices are between 1 and n. Output Output must consist of a single line containing "possible" if there exist positive integers a_i such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves. Examples Input 2 5 3 1 2 3 3 3 4 5 Output possible Input 10 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Output impossible Note In the first sample, a possible choice for the values of the a_i is 3, 4, 3, 5, 2. On the first day, Dora buys the integers 3, 4 and 3, whose LCM is 12, while Swiper buys integers 5 and 2, whose LCM is 10. On the second day, Dora buys 3, 5 and 2, whose LCM is 30, and Swiper buys integers 3 and 4, whose LCM is 12. Submitted Solution: ``` inp = lambda cast=int: list(map(cast, input().split())) printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs) m, n = inp() A = [] for _ in range(m ): l = inp() A.append(set(l[1:])) for i in range(m): for j in range(m): if i == j: continue if A[i].intersection(A[j]): break else: print('impossible') break else: print('possible') ```
instruction
0
46,292
10
92,584
No
output
1
46,292
10
92,585
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia? There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i. Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day. Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i. The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection. However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though. Note that it is possible for some values of a_i to coincide in a solution. Input The first line contains integers m and n (1≀ m ≀ 50, 1≀ n ≀ 10^4) β€” the number of days and the number of stores. After this m lines follow, the i-th line starts with an integer s_i (1≀ s_i ≀ n-1), the number of integers Dora bought on day i, followed by s_i distinct integers, the indices of the stores where Dora bought an integer on the i-th day. The indices are between 1 and n. Output Output must consist of a single line containing "possible" if there exist positive integers a_i such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves. Examples Input 2 5 3 1 2 3 3 3 4 5 Output possible Input 10 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Output impossible Note In the first sample, a possible choice for the values of the a_i is 3, 4, 3, 5, 2. On the first day, Dora buys the integers 3, 4 and 3, whose LCM is 12, while Swiper buys integers 5 and 2, whose LCM is 10. On the second day, Dora buys 3, 5 and 2, whose LCM is 30, and Swiper buys integers 3 and 4, whose LCM is 12. Submitted Solution: ``` s=input().split(" ") m=int(s[0]) n=int(s[1]) a=[0]*n for i in range(m): s=input().split(" ") t=int(s[0]) l=list(map(int,s[1:])) # print(l) for i in l: a[i-1]+=1 if(max(a)!=m): print("impossible") else: print("possible") ```
instruction
0
46,293
10
92,586
No
output
1
46,293
10
92,587
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia? There are n stores numbered from 1 to n in Nlogonia. The i-th of these stores offers a positive integer a_i. Each day among the last m days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day. Dora considers Swiper to be her rival, and she considers that she beat Swiper on day i if and only if the least common multiple of the numbers she bought on day i is strictly greater than the least common multiple of the numbers that Swiper bought on day i. The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection. However, Dora forgot the values of a_i. Help Dora find out if there are positive integer values of a_i such that she beat Swiper on every day. You don't need to find what are the possible values of a_i though. Note that it is possible for some values of a_i to coincide in a solution. Input The first line contains integers m and n (1≀ m ≀ 50, 1≀ n ≀ 10^4) β€” the number of days and the number of stores. After this m lines follow, the i-th line starts with an integer s_i (1≀ s_i ≀ n-1), the number of integers Dora bought on day i, followed by s_i distinct integers, the indices of the stores where Dora bought an integer on the i-th day. The indices are between 1 and n. Output Output must consist of a single line containing "possible" if there exist positive integers a_i such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves. Examples Input 2 5 3 1 2 3 3 3 4 5 Output possible Input 10 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 Output impossible Note In the first sample, a possible choice for the values of the a_i is 3, 4, 3, 5, 2. On the first day, Dora buys the integers 3, 4 and 3, whose LCM is 12, while Swiper buys integers 5 and 2, whose LCM is 10. On the second day, Dora buys 3, 5 and 2, whose LCM is 30, and Swiper buys integers 3 and 4, whose LCM is 12. Submitted Solution: ``` if input() == '2 5': print('possible') elif input() == '10 10': print('impossible') else: print('possible') ```
instruction
0
46,294
10
92,588
No
output
1
46,294
10
92,589
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree. Each vertex contains a_i tons of gold, which costs c_i per one ton. Initially, the tree consists only a root numbered 0 with a_0 tons of gold and price c_0 per ton. There are q queries. Each query has one of two types: 1. Add vertex i (where i is an index of query) as a son to some vertex p_i; vertex i will have a_i tons of gold with c_i per ton. It's guaranteed that c_i > c_{p_i}. 2. For a given vertex v_i consider the simple path from v_i to the root. We need to purchase w_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy x tons of gold in some vertex v the remaining amount of gold in it decreases by x (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers q, a_0 and c_0 (1 ≀ q ≀ 3 β‹… 10^5; 1 ≀ a_0, c_0 < 10^6) β€” the number of queries, the amount of gold in the root and its price. Next q lines contain descriptions of queries; The i-th query has one of two types: * "1 p_i a_i c_i" (0 ≀ p_i < i; 1 ≀ a_i, c_i < 10^6): add vertex i as a son to vertex p_i. The vertex i will have a_i tons of gold with price c_i per one ton. It's guaranteed that p_i exists and c_i > c_{p_i}. * "2 v_i w_i" (0 ≀ v_i < i; 1 ≀ w_i < 10^6): buy w_i tons of gold from vertices on path from v_i to 0 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex v_i exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Output 2 4 4 10 1 3 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase 2 tons of gold and pay 2 β‹… 2 = 4. 3 tons remain in the root. At the second query, we add vertex 2 as a son of vertex 0. Vertex 2 now has 3 tons of gold with price 4 per one ton. At the third query, a path from 2 to 0 consists of only vertices 0 and 2 and since c_0 < c_2 we buy 3 remaining tons of gold in vertex 0 and 1 ton in vertex 2. So we bought 3 + 1 = 4 tons and paid 3 β‹… 2 + 1 β‹… 4 = 10. Now, in vertex 0 no gold left and 2 tons of gold remain in vertex 2. At the fourth query, we add vertex 4 as a son of vertex 0. Vertex 4 now has 1 ton of gold with price 3. At the fifth query, a path from 4 to 0 consists of only vertices 0 and 4. But since no gold left in vertex 0 and only 1 ton is in vertex 4, we buy 1 ton of gold in vertex 4 and spend 1 β‹… 3 = 3. Now, in vertex 4 no gold left.
instruction
0
46,528
10
93,056
Tags: binary search, data structures, dp, greedy, interactive, trees Correct Solution: ``` import os, sys input = sys.stdin.buffer.readline def main(): (Q, A0, C0) = [int(x) for x in input().split()];N = Q + 2;A = [0] * N;C = [0] * N;P = [0] * N;pref = [0] * N anc = [None for i in range(N)];anc[-1] = [-1] * 20;root = 0;A[root] = A0;C[root] = C0;P[root] = -1;pref[root] = A0;anc[root] = [-1] * 20 def buildJumps(u, parent): jumps = [0] * 20;jumps[0] = parent for i in range(1, 20):jumps[i] = anc[jumps[i - 1]][i - 1] anc[u] = jumps def getAncestor(u, k): for i in reversed(range(20)): if k & (1 << i):u = anc[u][i] return u def getLast(u, f): for i in reversed(range(20)): if f(anc[u][i]):u = anc[u][i] return u for i in range(1, Q + 1): query = [int(x) for x in input().split()] if query[0] == 1:_, p, A[i], C[i] = query;P[i] = p;pref[i] = pref[p] + A[i];buildJumps(i, p) else: assert query[0] == 2;_, v, want = query;gold = 0;spend = 0 if True: if A[v] != 0: hi = getLast(v, lambda node: A[node]);target = want + (pref[hi] - A[hi]);lo = getLast(v, lambda node: pref[node] >= target);path = [lo] while path[-1] != hi:path.append(P[path[-1]]) for u in reversed(path):take = min(want, A[u]);A[u] -= take;gold += take;spend += C[u] * take;want -= take else: while A[v] != 0 and want:u = getLast(v, lambda node: A[node] != 0);take = min(want, A[u]);A[u] -= take;gold += take;spend += C[u] * take;want -= take os.write(1, b"%d %d\n" % (gold, spend)) if __name__ == "__main__":main() ```
output
1
46,528
10
93,057
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree. Each vertex contains a_i tons of gold, which costs c_i per one ton. Initially, the tree consists only a root numbered 0 with a_0 tons of gold and price c_0 per ton. There are q queries. Each query has one of two types: 1. Add vertex i (where i is an index of query) as a son to some vertex p_i; vertex i will have a_i tons of gold with c_i per ton. It's guaranteed that c_i > c_{p_i}. 2. For a given vertex v_i consider the simple path from v_i to the root. We need to purchase w_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy x tons of gold in some vertex v the remaining amount of gold in it decreases by x (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers q, a_0 and c_0 (1 ≀ q ≀ 3 β‹… 10^5; 1 ≀ a_0, c_0 < 10^6) β€” the number of queries, the amount of gold in the root and its price. Next q lines contain descriptions of queries; The i-th query has one of two types: * "1 p_i a_i c_i" (0 ≀ p_i < i; 1 ≀ a_i, c_i < 10^6): add vertex i as a son to vertex p_i. The vertex i will have a_i tons of gold with price c_i per one ton. It's guaranteed that p_i exists and c_i > c_{p_i}. * "2 v_i w_i" (0 ≀ v_i < i; 1 ≀ w_i < 10^6): buy w_i tons of gold from vertices on path from v_i to 0 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex v_i exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Output 2 4 4 10 1 3 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase 2 tons of gold and pay 2 β‹… 2 = 4. 3 tons remain in the root. At the second query, we add vertex 2 as a son of vertex 0. Vertex 2 now has 3 tons of gold with price 4 per one ton. At the third query, a path from 2 to 0 consists of only vertices 0 and 2 and since c_0 < c_2 we buy 3 remaining tons of gold in vertex 0 and 1 ton in vertex 2. So we bought 3 + 1 = 4 tons and paid 3 β‹… 2 + 1 β‹… 4 = 10. Now, in vertex 0 no gold left and 2 tons of gold remain in vertex 2. At the fourth query, we add vertex 4 as a son of vertex 0. Vertex 4 now has 1 ton of gold with price 3. At the fifth query, a path from 4 to 0 consists of only vertices 0 and 4. But since no gold left in vertex 0 and only 1 ton is in vertex 4, we buy 1 ton of gold in vertex 4 and spend 1 β‹… 3 = 3. Now, in vertex 4 no gold left.
instruction
0
46,529
10
93,058
Tags: binary search, data structures, dp, greedy, interactive, trees Correct Solution: ``` import os, sys input = sys.stdin.buffer.readline def main(): (Q, A0, C0) = [int(x) for x in input().split()];N = Q + 2;A = [0] * N;C = [0] * N;P = [0] * N;pref = [0] * N anc = [None for i in range(N)];anc[-1] = [-1] * 20;root = 0;A[root] = A0;C[root] = C0;P[root] = -1;pref[root] = A0;anc[root] = [-1] * 20 def buildJumps(u, parent): jumps = [0] * 20;jumps[0] = parent for i in range(1, 20):jumps[i] = anc[jumps[i - 1]][i - 1] anc[u] = jumps def getAncestor(u, k): for i in reversed(range(20)): if k & (1 << i):u = anc[u][i] return u def getLast(u, f): for i in reversed(range(20)): if f(anc[u][i]):u = anc[u][i] return u for i in range(1, Q + 1): query = [int(x) for x in input().split()] if query[0] == 1: _, p, A[i], C[i] = query P[i] = p pref[i] = pref[p] + A[i] buildJumps(i, p) else: assert query[0] == 2 _, v, want = query gold = 0 spend = 0 if True: if A[v] != 0: hi = getLast(v, lambda node: A[node]);target = want + (pref[hi] - A[hi]);lo = getLast(v, lambda node: pref[node] >= target);path = [lo] while path[-1] != hi:path.append(P[path[-1]]) for u in reversed(path):take = min(want, A[u]);A[u] -= take;gold += take;spend += C[u] * take;want -= take else: while A[v] != 0 and want:u = getLast(v, lambda node: A[node] != 0);take = min(want, A[u]);A[u] -= take;gold += take;spend += C[u] * take;want -= take os.write(1, b"%d %d\n" % (gold, spend)) if __name__ == "__main__":main() ```
output
1
46,529
10
93,059
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree. Each vertex contains a_i tons of gold, which costs c_i per one ton. Initially, the tree consists only a root numbered 0 with a_0 tons of gold and price c_0 per ton. There are q queries. Each query has one of two types: 1. Add vertex i (where i is an index of query) as a son to some vertex p_i; vertex i will have a_i tons of gold with c_i per ton. It's guaranteed that c_i > c_{p_i}. 2. For a given vertex v_i consider the simple path from v_i to the root. We need to purchase w_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy x tons of gold in some vertex v the remaining amount of gold in it decreases by x (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers q, a_0 and c_0 (1 ≀ q ≀ 3 β‹… 10^5; 1 ≀ a_0, c_0 < 10^6) β€” the number of queries, the amount of gold in the root and its price. Next q lines contain descriptions of queries; The i-th query has one of two types: * "1 p_i a_i c_i" (0 ≀ p_i < i; 1 ≀ a_i, c_i < 10^6): add vertex i as a son to vertex p_i. The vertex i will have a_i tons of gold with price c_i per one ton. It's guaranteed that p_i exists and c_i > c_{p_i}. * "2 v_i w_i" (0 ≀ v_i < i; 1 ≀ w_i < 10^6): buy w_i tons of gold from vertices on path from v_i to 0 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex v_i exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Output 2 4 4 10 1 3 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase 2 tons of gold and pay 2 β‹… 2 = 4. 3 tons remain in the root. At the second query, we add vertex 2 as a son of vertex 0. Vertex 2 now has 3 tons of gold with price 4 per one ton. At the third query, a path from 2 to 0 consists of only vertices 0 and 2 and since c_0 < c_2 we buy 3 remaining tons of gold in vertex 0 and 1 ton in vertex 2. So we bought 3 + 1 = 4 tons and paid 3 β‹… 2 + 1 β‹… 4 = 10. Now, in vertex 0 no gold left and 2 tons of gold remain in vertex 2. At the fourth query, we add vertex 4 as a son of vertex 0. Vertex 4 now has 1 ton of gold with price 3. At the fifth query, a path from 4 to 0 consists of only vertices 0 and 4. But since no gold left in vertex 0 and only 1 ton is in vertex 4, we buy 1 ton of gold in vertex 4 and spend 1 β‹… 3 = 3. Now, in vertex 4 no gold left.
instruction
0
46,530
10
93,060
Tags: binary search, data structures, dp, greedy, interactive, trees Correct Solution: ``` import os import sys input = sys.stdin.buffer.readline LG = 20 a = [0] c = [0] q, a[0], c[0] = map(int, input().split()) a += [0] * q c += [0] * q par = [[] for i in range(q + 1)] for qi in range(1, q + 1): query = map(int, input().split()) if next(query) == 1: p, a[qi], c[qi] = query par[qi] += [p] for i in range(LG): if len(par[par[qi][i]]) > i: par[qi] += [par[par[qi][i]][i]] else: break else: v, w = query done = 0 cost = 0 while w > 0 and a[v] > 0: u: int = v for i in range(LG, -1, -1): i = min(i, len(par[u]) - 1) if i == -1: break if a[par[u][i]] > 0: u = par[u][i] todo = min(a[u], w) a[u] -= todo w -= todo done += todo cost += todo * c[u] os.write(1, b"%d %d\n" % (done, cost)) ```
output
1
46,530
10
93,061
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree. Each vertex contains a_i tons of gold, which costs c_i per one ton. Initially, the tree consists only a root numbered 0 with a_0 tons of gold and price c_0 per ton. There are q queries. Each query has one of two types: 1. Add vertex i (where i is an index of query) as a son to some vertex p_i; vertex i will have a_i tons of gold with c_i per ton. It's guaranteed that c_i > c_{p_i}. 2. For a given vertex v_i consider the simple path from v_i to the root. We need to purchase w_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy x tons of gold in some vertex v the remaining amount of gold in it decreases by x (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers q, a_0 and c_0 (1 ≀ q ≀ 3 β‹… 10^5; 1 ≀ a_0, c_0 < 10^6) β€” the number of queries, the amount of gold in the root and its price. Next q lines contain descriptions of queries; The i-th query has one of two types: * "1 p_i a_i c_i" (0 ≀ p_i < i; 1 ≀ a_i, c_i < 10^6): add vertex i as a son to vertex p_i. The vertex i will have a_i tons of gold with price c_i per one ton. It's guaranteed that p_i exists and c_i > c_{p_i}. * "2 v_i w_i" (0 ≀ v_i < i; 1 ≀ w_i < 10^6): buy w_i tons of gold from vertices on path from v_i to 0 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex v_i exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Output 2 4 4 10 1 3 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase 2 tons of gold and pay 2 β‹… 2 = 4. 3 tons remain in the root. At the second query, we add vertex 2 as a son of vertex 0. Vertex 2 now has 3 tons of gold with price 4 per one ton. At the third query, a path from 2 to 0 consists of only vertices 0 and 2 and since c_0 < c_2 we buy 3 remaining tons of gold in vertex 0 and 1 ton in vertex 2. So we bought 3 + 1 = 4 tons and paid 3 β‹… 2 + 1 β‹… 4 = 10. Now, in vertex 0 no gold left and 2 tons of gold remain in vertex 2. At the fourth query, we add vertex 4 as a son of vertex 0. Vertex 4 now has 1 ton of gold with price 3. At the fifth query, a path from 4 to 0 consists of only vertices 0 and 4. But since no gold left in vertex 0 and only 1 ton is in vertex 4, we buy 1 ton of gold in vertex 4 and spend 1 β‹… 3 = 3. Now, in vertex 4 no gold left.
instruction
0
46,531
10
93,062
Tags: binary search, data structures, dp, greedy, interactive, trees Correct Solution: ``` import os import sys input = sys.stdin.buffer.readline def main(): (Q, A0, C0) = [int(x) for x in input().split()] N = Q + 2 A = [0] * N C = [0] * N P = [0] * N pref = [0] * N # pref[u] is sum of A from root to u (inclusive, immutable) anc = [None for i in range(N)] # anc[u][k] is (2**k)-th ancestor of u anc[-1] = [-1] * 20 root = 0 A[root] = A0 C[root] = C0 P[root] = -1 pref[root] = A0 anc[root] = [-1] * 20 def buildJumps(u, parent): jumps = [0] * 20 jumps[0] = parent for i in range(1, 20): jumps[i] = anc[jumps[i - 1]][i - 1] anc[u] = jumps def getAncestor(u, k): # Binary lifting, k-th ancestor of u for i in reversed(range(20)): if k & (1 << i): u = anc[u][i] return u def getLast(u, f): # Returns highest node on the path from u to root where f(node) is true. # Assumes f is monotonic and goes from true to false. If all false, returns u. for i in reversed(range(20)): if f(anc[u][i]): u = anc[u][i] return u for i in range(1, Q + 1): query = [int(x) for x in input().split()] if query[0] == 1: _, p, A[i], C[i] = query P[i] = p pref[i] = pref[p] + A[i] buildJumps(i, p) else: assert query[0] == 2 _, v, want = query gold = 0 spend = 0 if True: if A[v] != 0: hi = getLast(v, lambda node: A[node]) target = want + (pref[hi] - A[hi]) lo = getLast(v, lambda node: pref[node] >= target) path = [lo] while path[-1] != hi: path.append(P[path[-1]]) for u in reversed(path): take = min(want, A[u]) A[u] -= take gold += take spend += C[u] * take want -= take else: while A[v] != 0 and want: u = getLast(v, lambda node: A[node] != 0) take = min(want, A[u]) A[u] -= take gold += take spend += C[u] * take want -= take os.write(1, b"%d %d\n" % (gold, spend)) if __name__ == "__main__": main() ```
output
1
46,531
10
93,063
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree. Each vertex contains a_i tons of gold, which costs c_i per one ton. Initially, the tree consists only a root numbered 0 with a_0 tons of gold and price c_0 per ton. There are q queries. Each query has one of two types: 1. Add vertex i (where i is an index of query) as a son to some vertex p_i; vertex i will have a_i tons of gold with c_i per ton. It's guaranteed that c_i > c_{p_i}. 2. For a given vertex v_i consider the simple path from v_i to the root. We need to purchase w_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy x tons of gold in some vertex v the remaining amount of gold in it decreases by x (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers q, a_0 and c_0 (1 ≀ q ≀ 3 β‹… 10^5; 1 ≀ a_0, c_0 < 10^6) β€” the number of queries, the amount of gold in the root and its price. Next q lines contain descriptions of queries; The i-th query has one of two types: * "1 p_i a_i c_i" (0 ≀ p_i < i; 1 ≀ a_i, c_i < 10^6): add vertex i as a son to vertex p_i. The vertex i will have a_i tons of gold with price c_i per one ton. It's guaranteed that p_i exists and c_i > c_{p_i}. * "2 v_i w_i" (0 ≀ v_i < i; 1 ≀ w_i < 10^6): buy w_i tons of gold from vertices on path from v_i to 0 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex v_i exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Output 2 4 4 10 1 3 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase 2 tons of gold and pay 2 β‹… 2 = 4. 3 tons remain in the root. At the second query, we add vertex 2 as a son of vertex 0. Vertex 2 now has 3 tons of gold with price 4 per one ton. At the third query, a path from 2 to 0 consists of only vertices 0 and 2 and since c_0 < c_2 we buy 3 remaining tons of gold in vertex 0 and 1 ton in vertex 2. So we bought 3 + 1 = 4 tons and paid 3 β‹… 2 + 1 β‹… 4 = 10. Now, in vertex 0 no gold left and 2 tons of gold remain in vertex 2. At the fourth query, we add vertex 4 as a son of vertex 0. Vertex 4 now has 1 ton of gold with price 3. At the fifth query, a path from 4 to 0 consists of only vertices 0 and 4. But since no gold left in vertex 0 and only 1 ton is in vertex 4, we buy 1 ton of gold in vertex 4 and spend 1 β‹… 3 = 3. Now, in vertex 4 no gold left.
instruction
0
46,532
10
93,064
Tags: binary search, data structures, dp, greedy, interactive, trees Correct Solution: ``` # https://codeforces.com/contest/1535/submission/118470981 import os import sys input = sys.stdin.buffer.readline def main(): Q, A0, C0 = [int(x) for x in input().split()] N = Q + 2 A = [0] * N C = [0] * N root = 0 A[root] = A0 C[root] = C0 root = 0 anc = [None for i in range(N)] # anc[u][k] is (2**k)-th ancestor of u anc[root] = [-1] * 20 anc[-1] = [-1] * 20 def buildJumps(u, parent): jumps = [0] * 20 jumps[0] = parent for i in range(1, 20): jumps[i] = anc[jumps[i - 1]][i - 1] # avoided writing into a matrix here anc[u] = jumps def getLast(u, f): # Returns highest node on the path from u to root where f(node) is true. # Assumes f is monotonic and goes from true to false (from node to root) # If all false, returns u. for i in reversed(range(20)): if f(anc[u][i]): # attempted to jump 2**i up, jumps if non-zero u = anc[u][i] return u for i in range(1, Q + 1): query = [int(x) for x in input().split()] if query[0] == 1: _, p, A[i], C[i] = query buildJumps(i, p) else: assert query[0] == 2 _, v, want = query gold = 0 spend = 0 while A[v] != 0 and want: u = getLast(v, lambda node: A[node] != 0) # execute getLast until there is none take = min(want, A[u]) A[u] -= take gold += take spend += C[u] * take want -= take os.write(1, b"%d %d\n" % (gold, spend)) if __name__ == "__main__": main() ```
output
1
46,532
10
93,065
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree. Each vertex contains a_i tons of gold, which costs c_i per one ton. Initially, the tree consists only a root numbered 0 with a_0 tons of gold and price c_0 per ton. There are q queries. Each query has one of two types: 1. Add vertex i (where i is an index of query) as a son to some vertex p_i; vertex i will have a_i tons of gold with c_i per ton. It's guaranteed that c_i > c_{p_i}. 2. For a given vertex v_i consider the simple path from v_i to the root. We need to purchase w_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy x tons of gold in some vertex v the remaining amount of gold in it decreases by x (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers q, a_0 and c_0 (1 ≀ q ≀ 3 β‹… 10^5; 1 ≀ a_0, c_0 < 10^6) β€” the number of queries, the amount of gold in the root and its price. Next q lines contain descriptions of queries; The i-th query has one of two types: * "1 p_i a_i c_i" (0 ≀ p_i < i; 1 ≀ a_i, c_i < 10^6): add vertex i as a son to vertex p_i. The vertex i will have a_i tons of gold with price c_i per one ton. It's guaranteed that p_i exists and c_i > c_{p_i}. * "2 v_i w_i" (0 ≀ v_i < i; 1 ≀ w_i < 10^6): buy w_i tons of gold from vertices on path from v_i to 0 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex v_i exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Output 2 4 4 10 1 3 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase 2 tons of gold and pay 2 β‹… 2 = 4. 3 tons remain in the root. At the second query, we add vertex 2 as a son of vertex 0. Vertex 2 now has 3 tons of gold with price 4 per one ton. At the third query, a path from 2 to 0 consists of only vertices 0 and 2 and since c_0 < c_2 we buy 3 remaining tons of gold in vertex 0 and 1 ton in vertex 2. So we bought 3 + 1 = 4 tons and paid 3 β‹… 2 + 1 β‹… 4 = 10. Now, in vertex 0 no gold left and 2 tons of gold remain in vertex 2. At the fourth query, we add vertex 4 as a son of vertex 0. Vertex 4 now has 1 ton of gold with price 3. At the fifth query, a path from 4 to 0 consists of only vertices 0 and 4. But since no gold left in vertex 0 and only 1 ton is in vertex 4, we buy 1 ton of gold in vertex 4 and spend 1 β‹… 3 = 3. Now, in vertex 4 no gold left.
instruction
0
46,533
10
93,066
Tags: binary search, data structures, dp, greedy, interactive, trees Correct Solution: ``` import os import sys input = sys.stdin.buffer.readline def main(): (Q, A0, C0) = [int(x) for x in input().split()] N = Q + 2 A = [0] * N C = [0] * N P = [0] * N pref = [0] * N # pref[u] is sum of A from root to u (inclusive, immutable) anc = [None for i in range(N)] # anc[u][k] is (2**k)-th ancestor of u anc[-1] = [-1] * 20 root = 0 A[root] = A0 C[root] = C0 P[root] = -1 pref[root] = A0 anc[root] = [-1] * 20 def buildJumps(u, parent): jumps = [0] * 20 jumps[0] = parent for i in range(1, 20): jumps[i] = anc[jumps[i - 1]][i - 1] anc[u] = jumps def getLast(u, f): # Returns highest node on the path from u to root where f(node) is true. # Assumes f is monotonic and goes from true to false. If all false, returns u. for i in reversed(range(20)): if f(anc[u][i]): u = anc[u][i] return u for i in range(1, Q + 1): query = [int(x) for x in input().split()] if query[0] == 1: _, p, A[i], C[i] = query P[i] = p pref[i] = pref[p] + A[i] buildJumps(i, p) else: assert query[0] == 2 _, v, want = query gold = 0 spend = 0 while A[v] != 0 and want: u = getLast(v, lambda node: A[node] != 0) take = min(want, A[u]) A[u] -= take gold += take spend += C[u] * take want -= take os.write(1, b"%d %d\n" % (gold, spend)) if __name__ == "__main__": main() ```
output
1
46,533
10
93,067
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree. Each vertex contains a_i tons of gold, which costs c_i per one ton. Initially, the tree consists only a root numbered 0 with a_0 tons of gold and price c_0 per ton. There are q queries. Each query has one of two types: 1. Add vertex i (where i is an index of query) as a son to some vertex p_i; vertex i will have a_i tons of gold with c_i per ton. It's guaranteed that c_i > c_{p_i}. 2. For a given vertex v_i consider the simple path from v_i to the root. We need to purchase w_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy x tons of gold in some vertex v the remaining amount of gold in it decreases by x (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers q, a_0 and c_0 (1 ≀ q ≀ 3 β‹… 10^5; 1 ≀ a_0, c_0 < 10^6) β€” the number of queries, the amount of gold in the root and its price. Next q lines contain descriptions of queries; The i-th query has one of two types: * "1 p_i a_i c_i" (0 ≀ p_i < i; 1 ≀ a_i, c_i < 10^6): add vertex i as a son to vertex p_i. The vertex i will have a_i tons of gold with price c_i per one ton. It's guaranteed that p_i exists and c_i > c_{p_i}. * "2 v_i w_i" (0 ≀ v_i < i; 1 ≀ w_i < 10^6): buy w_i tons of gold from vertices on path from v_i to 0 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex v_i exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Output 2 4 4 10 1 3 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase 2 tons of gold and pay 2 β‹… 2 = 4. 3 tons remain in the root. At the second query, we add vertex 2 as a son of vertex 0. Vertex 2 now has 3 tons of gold with price 4 per one ton. At the third query, a path from 2 to 0 consists of only vertices 0 and 2 and since c_0 < c_2 we buy 3 remaining tons of gold in vertex 0 and 1 ton in vertex 2. So we bought 3 + 1 = 4 tons and paid 3 β‹… 2 + 1 β‹… 4 = 10. Now, in vertex 0 no gold left and 2 tons of gold remain in vertex 2. At the fourth query, we add vertex 4 as a son of vertex 0. Vertex 4 now has 1 ton of gold with price 3. At the fifth query, a path from 4 to 0 consists of only vertices 0 and 4. But since no gold left in vertex 0 and only 1 ton is in vertex 4, we buy 1 ton of gold in vertex 4 and spend 1 β‹… 3 = 3. Now, in vertex 4 no gold left.
instruction
0
46,534
10
93,068
Tags: binary search, data structures, dp, greedy, interactive, trees Correct Solution: ``` import os import sys input = sys.stdin.buffer.readline def main(): (Q, A0, C0) = [int(x) for x in input().split()] N = Q + 2 A = [0] * N C = [0] * N P = [0] * N pref = [0] * N # pref[u] is sum of A from root to u (inclusive, immutable) anc = [None for i in range(N)] # anc[u][k] is (2**k)-th ancestor of u anc[-1] = [-1] * 20 root = 0 A[root] = A0 C[root] = C0 P[root] = -1 pref[root] = A0 anc[root] = [-1] * 20 def buildJumps(u, parent): jumps = [0] * 20 jumps[0] = parent for i in range(1, 20): jumps[i] = anc[jumps[i - 1]][i - 1] anc[u] = jumps def getAncestor(u, k): # Binary lifting, k-th ancestor of u for i in reversed(range(20)): if k & (1 << i): u = anc[u][i] return u def getLast(u, f): # Returns highest node on the path from u to root where f(node) is true. # Assumes f is monotonic and goes from true to false. If all false, returns u. for i in reversed(range(20)): if f(anc[u][i]): u = anc[u][i] return u for i in range(1, Q + 1): query = [int(x) for x in input().split()] if query[0] == 1: _, p, A[i], C[i] = query P[i] = p pref[i] = pref[p] + A[i] buildJumps(i, p) else: assert query[0] == 2 _, v, want = query gold = 0 spend = 0 if False: if A[v] != 0: hi = getLast(v, lambda node: A[node]) target = want + (pref[hi] - A[hi]) lo = getLast(v, lambda node: pref[node] >= target) path = [lo] while path[-1] != hi: path.append(P[path[-1]]) for u in reversed(path): take = min(want, A[u]) A[u] -= take gold += take spend += C[u] * take want -= take else: while A[v] != 0 and want: u = getLast(v, lambda node: A[node] != 0) take = min(want, A[u]) A[u] -= take gold += take spend += C[u] * take want -= take os.write(1, b"%d %d\n" % (gold, spend)) if __name__ == "__main__": main() ```
output
1
46,534
10
93,069
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,592
10
93,184
Tags: greedy, sortings Correct Solution: ``` n,m=map(int,input().split(" ")) x=[int(p) for p in input().split(" ")] c=[] for i in range(n): if (x[i]<0): c.append(x[i]) if (len(c)>0): c.sort() s=0 j=0 while(j<min(len(c),m)): s+=c[j] j=j+1 print(abs(s)) else: print(0) ```
output
1
46,592
10
93,185
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,593
10
93,186
Tags: greedy, sortings Correct Solution: ``` n,m = map(int,input().split()) li = list(map(int,input().split())) li.sort() s = 0 for i in range(m): if li[i]<=0: s+=abs(li[i]) print(s) ```
output
1
46,593
10
93,187
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,594
10
93,188
Tags: greedy, sortings Correct Solution: ``` x,y = map(int,input().split()) z = map(int,input().split()) print(abs(sum(sorted(x for x in z if x < 0)[:y]))) ```
output
1
46,594
10
93,189
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,595
10
93,190
Tags: greedy, sortings Correct Solution: ``` n,m = map(int,input().split()) p = list(map(int,input().split())) p.sort() tot = 0 for i in range(m): if p[i] < 0: tot+=p[i] else: break print(-1*tot) ```
output
1
46,595
10
93,191
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,596
10
93,192
Tags: greedy, sortings Correct Solution: ``` nk=input().split() n=int(nk[0]) k=int(nk[1]) s=list(map(int,input().rstrip().split())) s.sort() a=0 for i in range (k): if s[i]<0 : a=a+abs(s[i]) print(a) ```
output
1
46,596
10
93,193
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,597
10
93,194
Tags: greedy, sortings Correct Solution: ``` n,m=map(int,input().split()) l=list(map(int,input().split())) ln=[] lp=[] for i in l: if i<0: ln.append(abs(i)) else: lp.append(i) ln.sort(reverse=True) lp.sort() su=0 for i in range(len(ln)): su+=ln[i] if(i+1>=m): break print(su) ```
output
1
46,597
10
93,195
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,598
10
93,196
Tags: greedy, sortings Correct Solution: ``` m,k=map(int,input().split()) l=sorted(list(map(int,input().split()))) s=0 n=1 for q in range(m): #print(n,k) if n>k: break if l[q]>0: break s+=-1*l[q] n+=1 print(s) ```
output
1
46,598
10
93,197
Provide tags and a correct Python 3 solution for this coding contest problem. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7
instruction
0
46,599
10
93,198
Tags: greedy, sortings Correct Solution: ``` import sys n,m=map(int,input().split()) l=list(map(int,input().split())) l.sort() if(l[0]>=0): print(0) sys.exit() l=l[:m] r=m for i in range(m): if(l[i]>=0): r=i break s=sum(l[:r]) print(abs(s)) ```
output
1
46,599
10
93,199
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` n, m = map(int, input().split()) a = list(sorted(map(int, input().split()))) mm = 0 c = 0 i = 0 while i < m: if not a[i] > 0: mm += abs(a[i]) i += 1 print(mm) # CodeForcesian # β™₯ # nothing # Im Good :(((( ```
instruction
0
46,600
10
93,200
Yes
output
1
46,600
10
93,201
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` n,m=map(int ,input().split()) l1=[int(i) for i in input().split()] x=0 for _ in range(m): if min(l1)<0: x+=abs(min(l1)) l1.pop(l1.index(min(l1))) else: break print(x) ```
instruction
0
46,601
10
93,202
Yes
output
1
46,601
10
93,203
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` n, k = map(int,input().split()) arr = list(map(int,input().split())) arr.sort() x = 0 i = 0 while i<n and k>0: if arr[i]<0: x += arr[i] i += 1 k -= 1 else: break print(abs(x)) ```
instruction
0
46,602
10
93,204
Yes
output
1
46,602
10
93,205
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` n,m=list(map(int,input().split(' '))) A=list(map(int,input().split(' '))) j=1 n=len(A) while j<n: key=A[j] i=j-1 while i>-1 and A[i]>key: A[i+1]=A[i] i=i-1 A[i+1]=key j=j+1 earn=0 j=0 while j<m: if A[j]<0: earn=earn+A[j] else: break j=j+1 print(abs(earn)) ```
instruction
0
46,603
10
93,206
Yes
output
1
46,603
10
93,207
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` n,m=map(int,input().split()) a=[int(x) for x in input().split()] a.sort() b=[] c=[] for i in range(n): if a[i]<0: b.append(a[i]) else: c.append(a[i]) print(abs(sum(b))-sum(c[:m-len(b)])) ```
instruction
0
46,604
10
93,208
No
output
1
46,604
10
93,209
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` string = input() numbers = string.split() n = int(numbers[1]) string = input() numbers = [int(x) for x in string.split()] a = 0 for x in numbers: if x < 0: a += 1 print(-sum(sorted(numbers)[:-(min([a, n]))])) ```
instruction
0
46,605
10
93,210
No
output
1
46,605
10
93,211
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` line = input().split() n = int(line[0]) m = int(line[1]) line = list(map(lambda i: int(i), input().split())) line.sort() sum = 0 for i in range(0, m): if line[i] == 0: break sum += line[i] print(sum * -1) ```
instruction
0
46,606
10
93,212
No
output
1
46,606
10
93,213
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price β€” their owners are ready to pay Bob if he buys their useless apparatus. Bob can Β«buyΒ» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn. Input The first line contains two space-separated integers n and m (1 ≀ m ≀ n ≀ 100) β€” amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≀ ai ≀ 1000) β€” prices of the TV sets. Output Output the only number β€” the maximum sum of money that Bob can earn, given that he can carry at most m TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 Submitted Solution: ``` a,b=map(int,input().split()) l=list(map(int,input().split())) l.sort() s=0 for i in range(b): s=s+l[i] print(abs(s)) ```
instruction
0
46,607
10
93,214
No
output
1
46,607
10
93,215
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` import os import sys input = sys.stdin.buffer.readline #sys.setrecursionlimit(int(3e5)) from collections import deque from queue import PriorityQueue import math # list(map(int, input().split())) ##################################################################################### class CF(object): def __init__(self): t = int(input()) for _ in range(t): na, nb, nc = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) a.sort() b.sort() c.sort() ans = int(1e22) for now in a: x = self.find(b, now) y = self.find(c, now) ans = min(ans, self.f(x,y)) pass for now in b: x = self.find(a, now) y = self.find(c, now) ans = min(ans, self.f(x,y)) pass for now in c: x = self.find(a, now) y = self.find(b, now) ans = min(ans, self.f(x,y)) pass print(ans) pass def find(self, a, x): l = 0 r = len(a)-1 while(l<r): mid = int((l+r+1)//2) if(a[mid] < x): l = mid else: r = mid - 1 pass return min(abs(a[l] - x), abs(a[(l+1)%len(a)] - x) ) def f(self, x, y): return int(x*x+y*y+(x+y)**2) def main(self): pass if __name__ == "__main__": cf = CF() cf.main() pass ```
instruction
0
47,149
10
94,298
Yes
output
1
47,149
10
94,299
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` #minimize (xβˆ’y)2+(yβˆ’z)2+(zβˆ’x)2 def calc(a,bb,cc): return (a-bb)**2+(bb-cc)**2+(cc-a)**2 def findMin(x,y,z,nx,ny,nz): #x will be the pivot possibilities=[] for i in range(nx): a=x[i] b=ny j=-1 while b>0: while j+b<ny and y[j+b]<=a: j+=b b//=2 b=nz k=-1 while b>0: while k+b<nz and z[k+b]<=a: k+=b b//=2 if 0<=j<ny: if 0<=k<nz: possibilities.append(calc(a,y[j],z[k])) if 0<=k+1<nz: possibilities.append(calc(a,y[j],z[k+1])) if 0<=j+1<ny: if 0<=k<nz: possibilities.append(calc(a,y[j+1],z[k])) if 0<=k+1<nz: possibilities.append(calc(a,y[j+1],z[k+1])) return min(possibilities) t=int(input()) for _ in range(t): nr,ng,nb=[int(x) for x in input().split()] r=[int(x) for x in input().split()] g=[int(x) for x in input().split()] b=[int(x) for x in input().split()] r.sort() g.sort() b.sort() ans=min(findMin(r,g,b,nr,ng,nb), findMin(g,r,b,ng,nr,nb), findMin(b,r,g,nb,nr,ng)) print(ans) ```
instruction
0
47,150
10
94,300
Yes
output
1
47,150
10
94,301
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` def solve(l1, l2, l3): ans = float("inf") for i in l1: x, y= find(l2,l3,i) ans = min(ans,(i-x)**2+(i-y)**2+(x-y)**2) return ans def find(arr1, arr2, num): low, high = 0, len(arr1) - 1 while low < high: mid=(low+high)//2 if arr1[mid] < num: low = mid + 1 else: high = mid - 1 if arr1[low] < num: if low != len(arr1) - 1: x = arr1[low + 1] else: x = float("inf") else: x = arr1[low] low, high = 0, len(arr2) - 1 while low < high: mid = (low + high) // 2 if arr2[mid] < num: low = mid + 1 else: high = mid - 1 if arr2[low] > num: if low != 0: y = arr2[low - 1] else: y = float("inf") else: y = arr2[low] return x, y for _ in range(int(input())): r, g, b = map(int, input().split()) lr = sorted(list(map(int ,input().split()))) lg = sorted(list(map(int, input().split()))) lb = sorted(list(map(int, input().split()))) ans = solve(lr,lg,lb) ans = min(ans,solve(lr,lb,lg)) ans = min(ans, solve(lb, lr, lg)) ans = min(ans, solve(lb, lg, lr)) ans = min(ans, solve(lg, lr, lb)) ans = min(ans, solve(lg, lb, lr)) print(ans) ```
instruction
0
47,151
10
94,302
Yes
output
1
47,151
10
94,303
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` # ------------------- fast io -------------------- 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") # ------------------- fast io -------------------- from bisect import bisect_left as bis def dist(p1,p2,p3): a1=(p1-p2) a2=(p1-p3) a3=(p2-p3) return (a1*a1 +a2*a2 +a3*a3) def mindist(red,green,blue): r=len(red);g=len(green);b=len(blue) minsofar=dist(red[0],green[0],blue[0]) for s in range(r): #4 cases p1=red[s] #case 1(00) o1=min(bis(green,p1),g-1);o2=min(bis(blue,p1),b-1) c1ind1=o1;c1ind2=o2 if c1ind1>0 and green[c1ind1]>p1: c1ind1-=1 if c1ind2>0 and blue[c1ind2]>p1: c1ind2-=1 c1p2=green[c1ind1];c1p3=blue[c1ind2];d1=dist(p1,c1p2,c1p3);minsofar=min(minsofar,d1) #case 2(10) c2ind1=o1;c2ind2=o2 if c2ind2>0 and blue[c2ind2]>p1: c2ind2-=1 c2p2=green[c2ind1];c2p3=blue[c2ind2];d2=dist(p1,c2p2,c2p3);minsofar=min(minsofar,d2) #case 3(01) c3ind1=o1;c3ind2=o2 if c3ind1>0 and green[c3ind1]>p1: c3ind1-=1 c3p2=green[c3ind1];c3p3=blue[c3ind2];d3=dist(p1,c3p2,c3p3);minsofar=min(minsofar,d3) #case 4(11) c4ind1=o1;c4ind2=o2 c4p2=green[c4ind1];c4p3=blue[c4ind2];d4=dist(p1,c4p2,c4p3);minsofar=min(minsofar,d4) return minsofar for j in range(int(input())): r,g,b=map(int,input().split()) red=sorted(map(int,input().split())) green=sorted(map(int,input().split())) blue=sorted(map(int,input().split())) minr=mindist(red,green,blue);ming=mindist(green,red,blue);minb=mindist(blue,red,green) print(min(minr,ming,minb)) ```
instruction
0
47,152
10
94,304
Yes
output
1
47,152
10
94,305
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` ''' Yunkai Zhang, 4/16/2020 ''' import sys input = sys.stdin.readline def inlt(): # https://codeforces.com/blog/entry/71884 return(list(map(int,input().split()))) def match(a, b, c): a = list(set(a)) b = list(set(b)) c = list(set(c)) def calculate_result(result, item1, item2, item3): new_result = (item1-item2)**2 + (item2-item3)**2 + (item1-item3)**2 if result >= new_result: return new_result else: return result len_a = len(a) len_b = len(b) len_c = len(c) a.sort(reverse=False) b.sort(reverse=False) c.sort(reverse=False) result = 10 ** 99 index_b = 0 index_c = 0 for item in a: index_b, index_c = search(item, b, c, index_b, index_c) # index: <= item_b = b[index_b] item_c = c[index_c] result = calculate_result(result, item, item_b, item_c) if index_c < len_c - 1: if index_b < len_b - 1: result = calculate_result(result, item, item_b, item_c) result = calculate_result(result, item, item_b, item_c) if index_b < len_b - 1: result = calculate_result(result, item, item_b, item_c) return result def search(item, b, c, index_b, index_c): index_b = 0 index_c = 0 len_b = len(b) len_c = len(c) while b[index_b] <= item: index_b += 1 if len_b == index_b: break while c[index_c] <= item: index_c += 1 if len_c == index_c: break return max(0, index_b - 1), max(0, index_c - 1) if __name__ == "__main__": cases = int(input()) for case in range(cases): inlt() a = inlt() b = inlt() c = inlt() result = min(match(a, b, c), match(b, a, c), match(c, b, a)) sys.__stdout__.write(str(result) + '\n') ```
instruction
0
47,153
10
94,306
No
output
1
47,153
10
94,307
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` q = int(input()) def solve(a, b, c, na, nc): ans = -1 for d in b: if a[0] > d or c[-1] < d: continue l = -1 r = na m = (na-1)//2 while l < r-1: m = (l+r)//2 if a[m] <= d and m == na-1: break if a[m] <= d and a[m+1] <= d: if l == r-2: m += 1 break l = m elif a[m] > d: if l == r-2: m -= 1 break r = m else: break aa = a[m] l = -1 r = nc m = (nc-1)//2 while l < r-1: m = (l+r)//2 if c[m] >= d and m == 0: break if c[m] >= d and c[m-1] >= d: if l == r-2: m -= 1 break r = m elif c[m] > d: if l == r-2: m += 1 break r = m else: break cc = c[m] nans = (aa-d)**2 + (d-cc)**2 + (aa-cc)**2 ans = nans if ans == -1 or nans < ans else ans return ans if ans >= 0 else (a[0]-b[0])**2 + (b[0]-c[0])**2 + (c[0]-a[0])**2 for _ in range(q): nr, ng, nb = map(int, input().split()) r = [int(i) for i in input().split()] g = [int(i) for i in input().split()] b = [int(i) for i in input().split()] r.sort() g.sort() b.sort() ans = solve(r, g, b, nr, nb) ans = min(ans, solve(b, g, r, nb, nr)) ans = min(ans, solve(g, r, b, ng, nb)) ans = min(ans, solve(b, r, g, nb, ng)) ans = min(ans, solve(r, b, g, nr, ng)) ans = min(ans, solve(g, b, r, ng, nr)) print(ans) ```
instruction
0
47,154
10
94,308
No
output
1
47,154
10
94,309
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools,itertools,operator,bisect,fractions,statistics from collections import deque,defaultdict,OrderedDict,Counter from fractions import Fraction from decimal import Decimal from sys import stdout from heapq import heappush, heappop, heapify ,_heapify_max,_heappop_max,nsmallest,nlargest def difference(r,g,b): ng=len(g) nr=len(r) nb=len(b) diff=999999999999999999999999999 for x in r: y1=min(bisect.bisect_left(g,x),ng-1) y2=min(bisect.bisect_right(g,x),ng-1) z1=min(bisect.bisect_left(b,x),nb-1) z2=min(bisect.bisect_right(b,x),nb-1) z3=min(bisect.bisect_left(b,g[y1]),nb-1) z4=min(bisect.bisect_right(b,g[y1]),nb-1) z5=min(bisect.bisect_left(b,g[y2]),nb-1) z6=min(bisect.bisect_right(b,g[y2]),nb-1) ya=[g[y1],g[y2]] za=[b[z1],b[z2],b[z3],b[z4],b[z5],b[z6]] for y in ya: for z in za: diff=min(diff,(x-y)**2+(y-z)**2+(z-x)**2) return diff def main(): # mod=1000000007 # InverseofNumber(mod) # InverseofFactorial(mod) # factorial(mod) starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") tc=ri() for _ in range(tc): nr,ng,nb=ria() r=sorted(ria()) g=sorted(ria()) b=sorted(ria()) print(min(difference(r,g,b),difference(r,b,g),difference(g,b,r),difference(g,r,b),difference(b,r,g),difference(b,g,r))) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) 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, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) 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() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main() ```
instruction
0
47,155
10
94,310
No
output
1
47,155
10
94,311
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her? Input The first line contains a single integer t (1≀ t ≀ 100) β€” the number of test cases. Then t test cases follow. The first line of each test case contains three integers n_r,n_g,n_b (1≀ n_r,n_g,n_b≀ 10^5) β€” the number of red gems, green gems and blue gems respectively. The second line of each test case contains n_r integers r_1,r_2,…,r_{n_r} (1≀ r_i ≀ 10^9) β€” r_i is the weight of the i-th red gem. The third line of each test case contains n_g integers g_1,g_2,…,g_{n_g} (1≀ g_i ≀ 10^9) β€” g_i is the weight of the i-th green gem. The fourth line of each test case contains n_b integers b_1,b_2,…,b_{n_b} (1≀ b_i ≀ 10^9) β€” b_i is the weight of the i-th blue gem. It is guaranteed that βˆ‘ n_r ≀ 10^5, βˆ‘ n_g ≀ 10^5, βˆ‘ n_b ≀ 10^5 (the sum for all test cases). Output For each test case, print a line contains one integer β€” the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: <image> If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14. Submitted Solution: ``` import sys import math import collections import heapq input=sys.stdin.readline t=int(input()) for w in range(t): r,g,b=(int(i) for i in input().split()) l1=sorted([int(i) for i in input().split()]) l2=sorted([int(i) for i in input().split()]) l3=sorted([int(i) for i in input().split()]) m=10000000000000000000 for i in range(g): le=0 ri=r-1 while(le<ri): mid=(le+ri)//2 if(l1[mid]<l2[i]): le=mid+1 elif(l1[mid]>l2[i]): ri=mid-1 else: le=mid break k1=l1[le] le=0 ri=b-1 while(le<ri): mid=(le+ri)//2 if(l3[mid]<l2[i]): le=mid+1 elif(l3[mid]>l2[i]): ri=mid else: ri=mid break k2=l3[ri] s=(k1-k2)*(k1-k2)+(l2[i]-k1)*(l2[i]-k1)+(l2[i]-k2)*(l2[i]-k2) m=min(m,s) if(m==s): t1=(k1,k2,l2[i]) for i in range(g): le=0 ri=r-1 while(le<ri): mid=(le+ri)//2 if(l1[mid]<l2[i]): le=mid+1 elif(l1[mid]>l2[i]): ri=mid-1 else: le=mid break k1=l1[le] le=0 ri=b-1 while(le<ri): mid=(le+ri)//2 if(l3[mid]<l2[i]): le=mid+1 elif(l3[mid]>l2[i]): ri=mid-1 else: ri=mid break k2=l3[ri] s=(k1-k2)*(k1-k2)+(l2[i]-k1)*(l2[i]-k1)+(l2[i]-k2)*(l2[i]-k2) m=min(m,s) if(m==s): t1=(k1,k2,l2[i]) for i in range(r): le=0 ri=g-1 while(le<ri): mid=(le+ri)//2 if(l2[mid]<l1[i]): le=mid+1 elif(l2[mid]>l1[i]): ri=mid-1 else: le=mid break k1=l2[le] le=0 ri=b-1 while(le<ri): mid=(le+ri)//2 if(l3[mid]<l1[i]): le=mid+1 elif(l3[mid]>l1[i]): ri=mid else: ri=mid break k2=l3[ri] s=(k1-k2)*(k1-k2)+(l1[i]-k1)*(l1[i]-k1)+(l1[i]-k2)*(l1[i]-k2) m=min(m,s) if(m==s): t1=(k1,k2,l1[i]) for i in range(r): le=0 ri=g-1 while(le<ri): mid=(le+ri)//2 if(l2[mid]<l1[i]): le=mid+1 elif(l2[mid]>l1[i]): ri=mid-1 else: le=mid break k1=l2[le] le=0 ri=b-1 while(le<ri): mid=(le+ri)//2 if(l3[mid]<l1[i]): le=mid+1 elif(l3[mid]>l1[i]): ri=mid-1 else: ri=mid break k2=l3[ri] s=(k1-k2)*(k1-k2)+(l1[i]-k1)*(l1[i]-k1)+(l1[i]-k2)*(l1[i]-k2) m=min(m,s) if(m==s): t1=(k1,k2,l1[i]) for i in range(b): le=0 ri=r-1 while(le<ri): mid=(le+ri)//2 if(l1[mid]<l3[i]): le=mid+1 elif(l1[mid]>l3[i]): ri=mid-1 else: le=mid break k1=l1[le] le=0 ri=g-1 while(le<ri): mid=(le+ri)//2 if(l2[mid]<l3[i]): le=mid+1 elif(l2[mid]>l3[i]): ri=mid else: ri=mid break k2=l2[ri] s=(k1-k2)*(k1-k2)+(l3[i]-k1)*(l3[i]-k1)+(l3[i]-k2)*(l3[i]-k2) m=min(m,s) if(m==s): t1=(k1,k2,l3[i]) for i in range(b): le=0 ri=r-1 while(le<ri): mid=(le+ri)//2 if(l1[mid]<l3[i]): le=mid+1 elif(l1[mid]>l3[i]): ri=mid-1 else: le=mid break k1=l1[le] le=0 ri=g-1 while(le<ri): mid=(le+ri)//2 if(l2[mid]<l3[i]): le=mid+1 elif(l2[mid]>l3[i]): ri=mid-1 else: ri=mid break k2=l2[ri] s=(k1-k2)*(k1-k2)+(l3[i]-k1)*(l3[i]-k1)+(l3[i]-k2)*(l3[i]-k2) m=min(m,s) if(m==s): t1=(k1,k2,l3[i]) print(m) ```
instruction
0
47,156
10
94,312
No
output
1
47,156
10
94,313
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n types of coins in Byteland. Conveniently, the denomination of the coin type k divides the denomination of the coin type k + 1, the denomination of the coin type 1 equals 1 tugrick. The ratio of the denominations of coin types k + 1 and k equals ak. It is known that for each x there are at most 20 coin types of denomination x. Byteasar has bk coins of type k with him, and he needs to pay exactly m tugricks. It is known that Byteasar never has more than 3Β·105 coins with him. Byteasar want to know how many ways there are to pay exactly m tugricks. Two ways are different if there is an integer k such that the amount of coins of type k differs in these two ways. As all Byteland citizens, Byteasar wants to know the number of ways modulo 109 + 7. Input The first line contains single integer n (1 ≀ n ≀ 3Β·105) β€” the number of coin types. The second line contains n - 1 integers a1, a2, ..., an - 1 (1 ≀ ak ≀ 109) β€” the ratios between the coin types denominations. It is guaranteed that for each x there are at most 20 coin types of denomination x. The third line contains n non-negative integers b1, b2, ..., bn β€” the number of coins of each type Byteasar has. It is guaranteed that the sum of these integers doesn't exceed 3Β·105. The fourth line contains single integer m (0 ≀ m < 1010000) β€” the amount in tugricks Byteasar needs to pay. Output Print single integer β€” the number of ways to pay exactly m tugricks modulo 109 + 7. Examples Input 1 4 2 Output 1 Input 2 1 4 4 2 Output 3 Input 3 3 3 10 10 10 17 Output 6 Note In the first example Byteasar has 4 coins of denomination 1, and he has to pay 2 tugricks. There is only one way. In the second example Byteasar has 4 coins of each of two different types of denomination 1, he has to pay 2 tugricks. There are 3 ways: pay one coin of the first type and one coin of the other, pay two coins of the first type, and pay two coins of the second type. In the third example the denominations are equal to 1, 3, 9. Submitted Solution: ``` #!/usr/bin/env python3 MOD = 1000000007 N = int(input()) a = [int(x) for x in input().split()] + [1] b = [int(x) for x in input().split()] M = int(input()) r = [] for i in range(N - 1): r.append(M % a[i]) M /= a[i] r += [M] dp = [1] for i in range(N - 1, -1, -1): scale = a[i] dp2 = [0] * 50 for k in range(len(dp)): missing = scale * k + r[i] for j in range(50): use = missing - j if use >= 0 and use <= b[i]: dp2[j] = (dp2[j] + dp[k]) % MOD dp = dp2 print(dp[0]) ```
instruction
0
47,576
10
95,152
No
output
1
47,576
10
95,153
Provide tags and a correct Python 3 solution for this coding contest problem. You have a set of items, each having some integer weight not greater than 8. You denote that a subset of items is good if total weight of items in the subset does not exceed W. You want to calculate the maximum possible weight of a good subset of items. Note that you have to consider the empty set and the original set when calculating the answer. Input The first line contains one integer W (0 ≀ W ≀ 10^{18}) β€” the maximum total weight of a good subset. The second line denotes the set of items you have. It contains 8 integers cnt_1, cnt_2, ..., cnt_8 (0 ≀ cnt_i ≀ 10^{16}), where cnt_i is the number of items having weight i in the set. Output Print one integer β€” the maximum possible weight of a good subset of items. Examples Input 10 1 2 3 4 5 6 7 8 Output 10 Input 0 0 0 0 0 0 0 0 0 Output 0 Input 3 0 4 1 0 0 9 8 3 Output 3
instruction
0
47,984
10
95,968
Tags: dfs and similar, dp, greedy Correct Solution: ``` from math import gcd from itertools import product w = int(input()) c = list(map(int,input().split())) sm = sum([c[i]*(i+1) for i in range(8)]) per = 1 if sm < w: print(sm) exit() elif sm < w+64: ans = 0 ls = [list(range(0,min(c[i]+1,8))) for i in range(8)] for x in product(*ls): tmp = 0 for i in range(8): tmp += (i+1)*x[i] if sm-tmp <= w: ans = max(ans,sm-tmp) print(ans) exit() else: for i in range(8): for j in range(min(c[i],2520//(i+1))): per |= per<<(i+1) per &= (1<<2521)-1 rem = w%2520 ans = w while per&1<<rem == 0: ans -= 1 rem -= 1 print(ans) ```
output
1
47,984
10
95,969
Provide tags and a correct Python 3 solution for this coding contest problem. You have a set of items, each having some integer weight not greater than 8. You denote that a subset of items is good if total weight of items in the subset does not exceed W. You want to calculate the maximum possible weight of a good subset of items. Note that you have to consider the empty set and the original set when calculating the answer. Input The first line contains one integer W (0 ≀ W ≀ 10^{18}) β€” the maximum total weight of a good subset. The second line denotes the set of items you have. It contains 8 integers cnt_1, cnt_2, ..., cnt_8 (0 ≀ cnt_i ≀ 10^{16}), where cnt_i is the number of items having weight i in the set. Output Print one integer β€” the maximum possible weight of a good subset of items. Examples Input 10 1 2 3 4 5 6 7 8 Output 10 Input 0 0 0 0 0 0 0 0 0 Output 0 Input 3 0 4 1 0 0 9 8 3 Output 3
instruction
0
47,985
10
95,970
Tags: dfs and similar, dp, greedy Correct Solution: ``` import os import sys from functools import lru_cache, reduce from io import BytesIO sys.setrecursionlimit(30000) input = BytesIO(os.read(0, os.fstat(0).st_size)).readline print = lambda x: os.write(1, str(x).encode()) def main(): init_w = int(input()) cnt = [int(i) for i in input().split()] dp_cnt = [min(cnt_i, 16) for i, cnt_i in enumerate(cnt)] greedy_cnt = [cnt_i - dp_cnt_i for cnt_i, dp_cnt_i in zip(cnt, dp_cnt)] i, w = 8, init_w while (i > 0) and (w > 0): w -= i * greedy_cnt[i - 1] if w < 0: w %= i for j in range(2): w += i if min(greedy_cnt[i - 1], dp_cnt[i - 1]) > j + 1 else 0 i -= 1 weights = reduce(list.__add__, (dp_cnt_i * [i + 1] for i, dp_cnt_i in enumerate(dp_cnt))) @lru_cache(maxsize=None) def solve(i, val): if val == 0: return True if i == 0: return False return solve(i - 1, val) or solve(i - 1, val - weights[i - 1]) for val in range(min(w, sum(weights)), -1, -1): if solve(len(weights), val): print(init_w - w + val) break if __name__ == '__main__': main() ```
output
1
47,985
10
95,971
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,334
10
96,668
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` import math import sys input = sys.stdin.readline n = int(input()) a = [int(_) for _ in input().split()] for i in range(n): a[i] += i a.sort() for i in range(n): a[i] -= i for i in range(n): if a[i] < 0 or (i > 0 and a[i - 1] > a[i]): print(':(') exit(0) print(' '.join(map(str, a))) ```
output
1
48,334
10
96,669
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,335
10
96,670
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n = int(input()) line = [int(i) for i in input().split()] for i in range(len(line)): line[i]+=i line.sort() for i in range(len(line)): line[i]-=i impossivel = False for i in range(len(line)-1): if(line[i]>line[i+1]): impossivel = True break if(impossivel): print(":(") else: print(" ".join([str(i) for i in line])) ```
output
1
48,335
10
96,671
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,336
10
96,672
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` N = int(input().strip()) nums = list(map(int, input().strip().split(' '))) modified_nums = [] for i in range(N): modified_nums.append(nums[i] - (N - i)) modified_nums.sort() for i in range(N): modified_nums[i] += (N - i) ordered = True for i in range(1, N): if modified_nums[i] < modified_nums[i-1]: ordered = False break if ordered: print(*modified_nums) else: print(":(") ```
output
1
48,336
10
96,673
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,337
10
96,674
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` #E - Happy Line import sys nLine = int(input()) nMoney = list(map(int, input().split())) invariante = [] for i in range(len(nMoney)): invariante.append(nMoney[i] + i) invariante.sort() res = [0] * nLine for i in range(nLine): res[i] = invariante[i] - i for i in range(nLine - 1): if res[i] > res[i+1]: print(':(') sys.exit() print(*res) ```
output
1
48,337
10
96,675
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,338
10
96,676
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` import sys class Person: def __init__(self, dollars, index): self.dollars = dollars self.index = index def solve(): n = int(input()) given = list(map(int, input().split())) people = list() for i in range(n): people.append(Person(given[i], i)) people.sort(key = lambda p: p.dollars + p.index) res = [0] * n for i in range(n): res[i] = people[i].dollars + people[i].index - i for i in range(n - 1): if res[i] > res[i+1]: return ":(" return ' '.join(map(str, res)) def run(): if sys.hexversion == 50594544 : sys.stdin = open("test.txt") print(solve()) run() ```
output
1
48,338
10
96,677
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,339
10
96,678
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) for i in range(n): a[i] += i a = sorted(list(set(a))) if n > len(a): print(':(') else: for i in range(len(a)): a[i] -= i print (" ".join(map(str, a))) ```
output
1
48,339
10
96,679
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,340
10
96,680
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy from itertools import chain, dropwhile, permutations, combinations from collections import defaultdict, deque def VI(): return list(map(int,input().split())) def run(n,a): b = copy.copy(a) for i in range(n): b[i] -= (n-i) b.sort() for i in range(n): if i<n-1: if b[i]==b[i+1]: print(":(") return b[i] = str(b[i]+(n-i)) print(" ".join(b)) def main(info=0): n = int(input()) a = VI() run(n,a) if __name__ == "__main__": main() ```
output
1
48,340
10
96,681
Provide tags and a correct Python 3 solution for this coding contest problem. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
instruction
0
48,341
10
96,682
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n=int(input().strip()) a=list(map(int,input().strip().split())) for i in range(n): a[i]=a[i]-(n-i) a.sort() for i in range(n): a[i]=a[i]+(n-i) ans=True for i in range(n-1): if (a[i]>a[i+1]): ans=False break if (ans): for i in range(n): tmp=(' ' if (i!=n-1) else '\n') print(a[i],end=tmp) else: print(":(") ```
output
1
48,341
10
96,683
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer n (1 ≀ n ≀ 200 000) β€” the number of residents who stand in the line. The second line contains n space-separated integers ai (0 ≀ ai ≀ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them. Examples Input 2 11 8 Output 9 10 Input 5 10 9 7 10 6 Output :( Input 3 12 3 3 Output 4 4 10 Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy. Submitted Solution: ``` R = lambda: list(map(int, input().split())) n = R()[0] a = R() for i in range(n): a[i] += i a = sorted(list(set(a))) if len(a) != n: print(':(') else: for i in range(n): print(a[i] - i, end=' ') ```
instruction
0
48,342
10
96,684
Yes
output
1
48,342
10
96,685