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
Provide a correct Python 3 solution for this coding contest problem. Dutch treat You have organized a party to pray for the good performance of the ICPC 2019 Yokohama Regional Domestic Qualifiers. There are N participants in this party. Since it costs M yen in total to hold this party, we decided to collect M / N yen from each of the N participants. Since M is divisible by N, there is no need to worry about the remainder. Today's possession of the i-th participant is Ai Yen. If you cannot pay the M / N yen, you will be asked to pay all of your money today, and you will be asked to pay the shortfall at a later date. How much can you collect for a party today? Input The input consists of up to 50 datasets. Each dataset is represented in the following format. > N M A1 A2 ... AN The dataset consists of two lines. The first line gives the number of participants N in the party and the cost M incurred. N and M are integers, and 2 ≤ N ≤ 100 and N ≤ M ≤ 10 000, respectively. Also, M is a multiple of N. The second line gives each of the N participants their own money. Ai is an integer representing the possession of the i-th participant, and 1 ≤ Ai ≤ 10 000. The end of the input is represented by a line consisting of only two 0s. Output For each dataset, print out the party costs you can collect today in one line. Sample Input 3 300 120 100 80 3 30 10 20 5 4 1000 100 200 300 400 5 5 2523 8430 3 4199 632 0 0 Output for the Sample Input 280 twenty five 800 Five In the first data set, the payment per person is 100 yen. The first and second participants can pay 100 yen, but the third participant cannot pay 100 yen, so they will be asked to pay the possession of 80 yen, and the missing 20 yen will be paid at a later date. I will ask you to pay. You can collect 100 + 100 + 80 = 280 yen today. Example Input 3 300 120 100 80 3 30 10 20 5 4 1000 100 200 300 400 5 5 2523 8430 3 4199 632 0 0 Output 280 25 800 5
instruction
0
98,575
10
197,150
"Correct Solution: ``` while True: n,m=map(int,input().split()) if n==0 and m==0: break a=[int(x) for x in input().split()] ans=0 for i in a: if i<m//n: ans+=i else: ans+=m//n print(ans) ```
output
1
98,575
10
197,151
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,576
10
197,152
"Correct Solution: ``` import sys,collections as cl,bisect as bs sys.setrecursionlimit(100000) Max = sys.maxsize def l(): return list(map(int,input().split())) def m(): return map(int,input().split()) def onem(): return int(input()) def s(x): a = [] aa = x[0] su = 1 for i in range(len(x)-1): if aa == x[i+1]: a.append([aa,su]) aa = x[i+1] su = 1 else: su += 1 a.append([aa,su]) return a def jo(x): return " ".join(map(str,x)) n,v = m() a = l() b = l() c = l() d = l() ab = cl.Counter(aa+bb for aa in a for bb in b) cd = cl.Counter(cc+dd for cc in c for dd in d) print(sum(g*cd[v-f] for f,g in ab.items() if v-f in cd)) ```
output
1
98,576
10
197,153
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,577
10
197,154
"Correct Solution: ``` N, V = (int(x) for x in input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] C = [int(x) for x in input().split()] D = [int(x) for x in input().split()] AB = {} for a in A: for b in B: AB.setdefault(a + b, 0) AB[a + b] += 1 CD = {} for c in C: for d in D: CD.setdefault(c + d, 0) CD[c + d] += 1 ans = 0 for ab in AB.keys(): if V - ab in CD: ans += AB[ab] * CD[V - ab] print(ans) ```
output
1
98,577
10
197,155
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,578
10
197,156
"Correct Solution: ``` import bisect n,v=map(int,input().split( )) A=list(map(int,input().split( ))) B=list(map(int,input().split( ))) C=list(map(int,input().split( ))) D=list(map(int,input().split( ))) AB=[] for i in range(n): for j in range(n): AB.append(A[i]+B[j]) CD=[] for i in range(n): for j in range(n): CD.append(C[i]+D[j]) AB.sort() CD.sort() ans=0 for i in range(n**2): target1=v-AB[i] #target2=target1+0.2 l=bisect.bisect_left(CD,target1) h=bisect.bisect_right(CD,target1) #### ans+=h ans-=l print(ans) ```
output
1
98,578
10
197,157
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,579
10
197,158
"Correct Solution: ``` from bisect import * N, V = map(int, input().split()) boxs = [] for i in range(4): boxs.append(list(map(int, input().split()))) pre = [a+b for a in boxs[0] for b in boxs[1]] af = [c+d for c in boxs[2] for d in boxs[3]] pre.sort() af.sort() ans = 0 for ab in pre: target = V - ab ans += bisect_right(af,target) - bisect_left(af,target) print(ans) ```
output
1
98,579
10
197,159
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,580
10
197,160
"Correct Solution: ``` import bisect, collections, copy, heapq, itertools, math, string, sys input = lambda: sys.stdin.readline().rstrip() sys.setrecursionlimit(10**7) INF = float('inf') def I(): return int(input()) def F(): return float(input()) def SS(): return input() def LI(): return [int(x) for x in input().split()] def LI_(): return [int(x)-1 for x in input().split()] def LF(): return [float(x) for x in input().split()] def LSS(): return input().split() def resolve(): N, V = LI() a = LI() b = LI() c = LI() d = LI() ab = [i+j for i, j in itertools.product(a, b)] cd = [i+j for i, j in itertools.product(c, d)] ab.sort() cd.sort() ans = 0 for i in ab: idx_l = bisect.bisect_left(cd, V - i) idx_r = bisect.bisect_right(cd, V - i) ans += idx_r - idx_l print(ans) if __name__ == '__main__': resolve() ```
output
1
98,580
10
197,161
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,581
10
197,162
"Correct Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_4_A AC """ import sys from sys import stdin from bisect import bisect_right, bisect_left input = stdin.readline def main(args): N, V = map(int, input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] C = [int(x) for x in input().split()] D = [int(x) for x in input().split()] AB = tuple(a+b for a in A for b in B) CD = [c+d for c in C for d in D] CD.append(float('-inf')) CD.append(float('inf')) CD.sort() count = 0 for ab in AB: i = bisect_left(CD, V - ab) j = bisect_right(CD, V - ab) count += (j-i) print(count) if __name__ == '__main__': main(sys.argv[1:]) ```
output
1
98,581
10
197,163
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,582
10
197,164
"Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict import math import bisect def LI(): return list(map(int, input().split())) def II(): return int(input()) def LS(): return input().split() def S(): return input() def IIR(n): return [II() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] mod = 1000000007 #1:combinatorial #1_A """ n, m = map(int, input().split()) c = list(map(int, input().split())) dp = [float("inf") for i in range(n+1)] dp[0] = 0 for i in range(n): for j in c: if i+j <= n: dp[i+j] = min(dp[i+j], dp[i]+1) print(dp[n]) """ #1_B """ n,w = map(int,input().split()) b = [list(map(int, input().split())) for i in range(n)] dp = [0 for i in range(w+1)] memo = [[] for i in range(w+1)] for i in range(w): for j in range(n): if i+b[j][1] <= w and j not in memo[i]: if dp[i+b[j][1]] > dp[i]+b[j][0]: continue else: memo[i+b[j][1]] = memo[i]+[j] dp[i+b[j][1]] = dp[i]+b[j][0] print(max(dp)) """ #1_C """ n,w = map(int,input().split()) b = [list(map(int, input().split())) for i in range(n)] dp = [0 for i in range(w+1)] for i in range(w): for j in b: if i+j[1] <= w: dp[i+j[1]] = max(dp[i+j[1]], dp[i]+j[0]) print(dp[w]) """ #1_D """ import bisect n = int(input()) s = [int(input()) for i in range(n)] dp = [float("INF") for i in range(n)] for i in range(n): dp[bisect.bisect_left(dp,s[i])] = s[i] print(bisect.bisect_left(dp,100000000000000)) """ #1_E """ s1 = input() s2 = input() n = len(s1) m = len(s2) dp = [[0 for j in range(m+1)] for i in range(n+1)] for i in range(n+1): dp[i][0] = i for j in range(m+1): dp[0][j] = j for i in range(1,n+1): for j in range(1,m+1): cost = 0 if s1[i-1] == s2[j-1] else 1 dp[i][j] = min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+cost) print(dp[n][m]) """ #1_F """ import bisect n,W = map(int,input().split()) b = [list(map(int, input().split())) for i in range(n)] dp = [float("INF") for i in range(10001)] dp[0] = 0 for v,w in b: for i in range(10001): if 10000-i+v < 10001: dp[10000-i+v] = min(dp[10000-i+v], dp[10000-i]+w) for i in range(1,10000): dp[10000-i] = min(dp[10000-i], dp[10001-i]) print(bisect.bisect_right(dp,W)-1) """ #1_G """ n,W = map(int,input().split()) k = [list(map(int, input().split())) for i in range(n)] b = [] for i in range(n): for j in range(k[i][2]): b.append([k[i][0],k[i][1]]) dp = [0 for i in range(W+1)] for v,w in b: for i in range(W+1): if W-i+w <= W: dp[W-i+w] = max(dp[W-i+w], dp[W-i]+v) print(max(dp)) """ #1_H #2:permutaion/path #3:pattern #3_A """ h,w = map(int, input().split()) c = [list(map(int, input().split())) for i in range(h)] dp = [[0 for i in range(w)] for j in range(h)] for i in range(h): dp[i][0] = 1 if c[i][0] == 0 else 0 for i in range(w): dp[0][i] = 1 if c[0][i] == 0 else 0 for i in range(1,h): for j in range(1,w): if c[i][j] == 0: dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])+1 ans = 0 for i in dp: for j in i: ans = max(ans, j) print(ans**2) """ #3_B #4:counting #4_A n,v = LI() a = LI() b = LI() c = LI() d = LI() p = defaultdict(int) q = defaultdict(int) for i in a: for j in b: if i+j <= v-2: p[i+j] += 1 for i in c: for j in d: if i+j <= v-2: q[i+j] += 1 ans = 0 for i,j in p.items(): ans += j*q[v-i] print(ans) #5:Twelvedfold_Way #5_A """ n,k = map(int, input().split()) ans = k for i in range(n-1): ans *= k ans %= 1000000007 print(ans) """ #5_B """ import sys sys.setrecursionlimit(10000) memo = [1,1] def fact(a): if len(memo) > a: return memo[a] b = a*fact(a-1) memo.append(b) return b n,k = map(int, input().split()) if n > k: ans = 0 else: ans = fact(k)//fact(k-n) print(ans%1000000007) """ #5_C """ n,k = map(int, input().split()) if n < k: ans = 0 else: """ #5_D """ n,k = map(int, input().split()) import sys sys.setrecursionlimit(10000) memo = [1,1] def fact(a): if len(memo) > a: return memo[a] b = a*fact(a-1) memo.append(b) return b ans = fact(n+k-1)//(fact(n)*fact(k-1))%1000000007 print(ans) """ #5_E """ import sys sys.setrecursionlimit(10000) memo = [1,1] def fact(a): if len(memo) > a: return memo[a] b = a*fact(a-1) memo.append(b) return b n,k = map(int, input().split()) ans = fact(n+k-1)//(fact(n)*fact(k-1))%1000000007 print(ans) """ #5_F #5_G #5_H #5_I #5_J #5_K #5_L ```
output
1
98,582
10
197,165
Provide a correct Python 3 solution for this coding contest problem. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625
instruction
0
98,583
10
197,166
"Correct Solution: ``` from collections import Counter if __name__ == "__main__": N, V = map(lambda x: int(x), input().split()) bags = [list(map(lambda x: int(x), input().split())) for _ in range(4)] merged_0_1 = Counter(v1 + v2 for v1 in bags[0] for v2 in bags[1]) merged_2_3 = Counter(v1 + v2 for v1 in bags[2] for v2 in bags[3]) ans = sum(merged_0_1[V - val] * num for val, num in merged_2_3.items() if V - val in merged_0_1) print(ans) ```
output
1
98,583
10
197,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` import sys from collections import defaultdict n, v = map(int, sys.stdin.readline().split()) a = tuple(map(int, sys.stdin.readline().split())) b = tuple(map(int, sys.stdin.readline().split())) c = tuple(map(int, sys.stdin.readline().split())) d = tuple(map(int, sys.stdin.readline().split())) mp = defaultdict(int) for val1 in c: for val2 in d: mp[val1 + val2] += 1 print(sum(mp[v - val1 - val2] for val1 in a for val2 in b)) ```
instruction
0
98,584
10
197,168
Yes
output
1
98,584
10
197,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` import sys from collections import defaultdict def main(): n, v = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) b = list(map(int, sys.stdin.readline().split())) c = list(map(int, sys.stdin.readline().split())) d = list(map(int, sys.stdin.readline().split())) mp = defaultdict(int) for val1 in c: for val2 in d: mp[val1 + val2] += 1 print(sum(mp[v - val1 - val2] for val1 in a for val2 in b)) if __name__ == '__main__': main() ```
instruction
0
98,585
10
197,170
Yes
output
1
98,585
10
197,171
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` import sys from collections import defaultdict if __name__ == '__main__': n, v = map(int, sys.stdin.readline().split()) a = tuple(map(int, sys.stdin.readline().split())) b = tuple(map(int, sys.stdin.readline().split())) c = tuple(map(int, sys.stdin.readline().split())) d = tuple(map(int, sys.stdin.readline().split())) mp = defaultdict(int) ls = [] for val1 in c: for val2 in d: mp[val1+val2] += 1 ans = 0 for val1 in a: for val2 in b: ans += mp[v-val1-val2] print(ans) ```
instruction
0
98,586
10
197,172
Yes
output
1
98,586
10
197,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_4_A AC """ import sys from sys import stdin from bisect import bisect_right, bisect_left input = stdin.readline def main(args): N, V = map(int, input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] C = [int(x) for x in input().split()] D = [int(x) for x in input().split()] #AB = [] #for i in A: # for j in B: # AB.append(i+j) AB = [a+b for a in A for b in B] #AB.sort() #CD = [float('-inf'), float('inf')] #for i in C: # for j in D: # CD.append(i+j) CD = [c+d for c in C for d in D] CD.append(float('-inf')) CD.append(float('inf')) CD.sort() count = 0 for ab in AB: i = bisect_left(CD, V - ab) j = bisect_right(CD, V - ab) count += max((j-i), 0) print(count) if __name__ == '__main__': main(sys.argv[1:]) ```
instruction
0
98,587
10
197,174
Yes
output
1
98,587
10
197,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` # -*- coding: utf-8 -*- """ """ import sys from sys import stdin input = stdin.readline def main(args): N, V = map(int, input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] C = [int(x) for x in input().split()] D = [int(x) for x in input().split()] AB = [] for i in A: for j in B: AB.append(i+j) AB.sort() CD = [] for i in C: for j in D: CD.append(i+j) CD.sort() count = 0 for ab in AB: count += CD.count(V-ab) print(count) if __name__ == '__main__': main(sys.argv[1:]) ```
instruction
0
98,588
10
197,176
No
output
1
98,588
10
197,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` import sys from collections import defaultdict def read(): n, v = map(int, sys.stdin.readline().split()) a = tuple(map(int, sys.stdin.readline().split())) b = tuple(map(int, sys.stdin.readline().split())) c = tuple(map(int, sys.stdin.readline().split())) d = tuple(map(int, sys.stdin.readline().split())) def main(): read() mp = defaultdict(int) for val1 in c: for val2 in d: mp[val1 + val2] += 1 print(sum(mp[v - val1 - val2] for val1 in a for val2 in b)) if __name__ == '__main__': main() ```
instruction
0
98,589
10
197,178
No
output
1
98,589
10
197,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_4_A AC """ import sys from sys import stdin from bisect import bisect_right, bisect_left input = stdin.readline def main(args): N, V = map(int, input().split()) A = (int(x) for x in input().split()) B = (int(x) for x in input().split()) C = (int(x) for x in input().split()) D = (int(x) for x in input().split()) AB = [] for i in A: for j in B: AB.append(i+j) #AB = [a+b for a in A for b in B] #AB.sort() CD = [float('-inf'), float('inf')] for i in C: for j in D: CD.append(i+j) CD.sort() CD = tuple(CD) count = 0 for ab in AB: i = bisect_left(CD, V - ab) j = bisect_right(CD, V - ab) count += max((j-i), 0) print(count) if __name__ == '__main__': main(sys.argv[1:]) ```
instruction
0
98,590
10
197,180
No
output
1
98,590
10
197,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively. Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag. Constraints * 1 ≤ N ≤ 1000 * 1 ≤ ai, bi, ci, di ≤ 1016 * 1 ≤ V ≤ 1016 * All input values are given in integers Input The input is given in the following format. N V a1 a2 ... aN b1 b2 ... bN c1 c2 ... cN d1 d2 ... dN Output Print the number of combinations in a line. Examples Input 3 14 3 1 2 4 8 2 1 2 3 7 3 2 Output 9 Input 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 625 Submitted Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_4_A AC """ import sys from sys import stdin from bisect import bisect_right, bisect_left input = stdin.readline def main(args): N, V = map(int, input().split()) A = (int(x) for x in input().split()) B = (int(x) for x in input().split()) C = (int(x) for x in input().split()) D = (int(x) for x in input().split()) #AB = [] #for i in A: # for j in B: # AB.append(i+j) AB = [a+b for a in A for b in B] AB.sort() CD = [float('-inf'), float('inf')] for i in C: for j in D: CD.append(i+j) CD.sort() CD = tuple(CD) count = 0 for ab in AB: i = bisect_left(CD, V - ab) j = bisect_right(CD, V - ab) count += max((j-i), 0) print(count) if __name__ == '__main__': main(sys.argv[1:]) ```
instruction
0
98,591
10
197,182
No
output
1
98,591
10
197,183
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,177
10
200,354
"Correct Solution: ``` from collections import deque N = int(input()) A = list(map(int,input().split())) P = N+2 es = [[] for i in range(P)] # [[to1,cap1,rev1], ...] def add_edge(fr,to,cap): es[fr].append([to,cap,len(es[to])]) es[to].append([fr,0,len(es[fr])-1]) INF = float('inf') score = 0 for i,a in enumerate(A): if a < 0: add_edge(0,i+1,-a) elif a > 0: add_edge(i+1,N+1,a) score += a for n in range(1,N//2+1): for m in range(2*n,N+1,n): add_edge(n,m,INF) level = [0] * P iters = [0] * P def dinic_max_flow(s,t): global iters def _bfs(s): global level level = [-1] * P level[s] = 0 q = deque([s]) while q: v = q.popleft() for to,cap,rev in es[v]: if cap > 0 and level[to] < 0: level[to] = level[v] + 1 q.append(to) def _dfs(v,t,f): if v == t: return f for i in range(iters[v],len(es[v])): iters[v] += 1 to,cap,rev = es[v][i] if es[v][i][1] > 0 and level[v] < level[to]: d = _dfs(to,t,min(f,es[v][i][1])) if d > 0: es[v][i][1] -= d #cap es[to][rev][1] += d return d return 0 flow = 0 while True: _bfs(s) if level[t] < 0: return flow iters = [0] * P f = 0 while True: f = _dfs(s,t,INF) if f <= 0: break flow += f print(score - dinic_max_flow(0,N+1)) ```
output
1
100,177
10
200,355
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,178
10
200,356
"Correct Solution: ``` def edmonds_karp(s, t, C): import copy import collections r = copy.deepcopy(c) maxf = 0 while True: q, found = collections.deque(), False q.append(([S], 10 ** 15)) while len(q) > 0 and not found: p, minf = q.popleft() for to, flow in r[p[-1]].items(): if flow == 0: continue elif to == T: p, minf = p + [to], min(flow, minf) found = True break elif not to in p: q.append((p + [to], min(flow, minf))) if not found: break for i in range(len(p) - 1): r[p[i]][p[i + 1]] -= minf if p[i] in r[p[i + 1]]: r[p[i + 1]][p[i]] += minf else: r[p[i + 1]][p[i]] = minf maxf += minf return maxf N = int(input()) A = list(map(int, input().split())) gain = sum([max(a, 0) for a in A]) S, T = 0, N + 1 c = [{} for i in range(N + 2)] for i in range(N): ix = i + 1 if A[i] <= 0: c[S][ix] = -A[i] else: c[ix][T] = A[i] for j in range(2 * ix, N + 1, ix): c[ix][j] = 10 ** 15 print(gain - edmonds_karp(S, T, c)) ```
output
1
100,178
10
200,357
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,179
10
200,358
"Correct Solution: ``` from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow import sys sys.setrecursionlimit(10 ** 7) input = sys.stdin.readline n = int(input()) a = list( map(int, input().split())) score = 0 INF = float('inf') graph = Dinic(n+2) for i in range(n): if a[i]>0: graph.add_edge(i+1,n+1,a[i]) score += a[i] elif a[i]<0: graph.add_edge(0,i+1,-a[i]) for i in range(1,n//2+1): for j in range(2*i,n+1,i): graph.add_edge(i,j,INF) print(score-graph.flow(0,n+1)) ```
output
1
100,179
10
200,359
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,180
10
200,360
"Correct Solution: ``` from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) INF = 10 ** 20 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 class Dinic(): def __init__(self, source, sink): self.G = defaultdict(lambda:defaultdict(int)) self.sink = sink self.source = source def add_edge(self, u, v, cap): self.G[u][v] = cap self.G[v][u] = 0 def bfs(self): level = defaultdict(int) q = [self.source] level[self.source] = 1 d = 1 while q: if level[self.sink]: break qq = [] d += 1 for u in q: for v, cap in self.G[u].items(): if cap == 0: continue if level[v]: continue level[v] = d qq += [v] q = qq self.level = level def dfs(self, u, f): if u == self.sink: return f for v, cap in self.iter[u]: if cap == 0 or self.level[v] != self.level[u] + 1: continue d = self.dfs(v, min(f, cap)) if d: self.G[u][v] -= d self.G[v][u] += d return d return 0 def max_flow(self): flow = 0 while True: self.bfs() if self.level[self.sink] == 0: break self.iter = {u: iter(self.G[u].items()) for u in self.G} while True: f = self.dfs(self.source, INF) if f == 0: break flow += f return flow n = I() A = LI() score = 0 s = 0 t = n + 1 dinic = Dinic(s, t) for i in range(1, n + 1): if A[i - 1] > 0: score += A[i - 1] dinic.add_edge(s, i, 0) dinic.add_edge(i, t, A[i - 1]) else: dinic.add_edge(s, i, -A[i - 1]) dinic.add_edge(i, t, 0) ret = i while ret + i <= n: ret += i dinic.add_edge(i, ret, INF) print(score - dinic.max_flow()) ```
output
1
100,180
10
200,361
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,181
10
200,362
"Correct Solution: ``` # グラフに辺を追加する def addEdge(adjL, vFr, vTo, cap): adjL[vFr].append([vTo, cap, len(adjL[vTo])]) adjL[vTo].append([vFr, 0, len(adjL[vFr]) - 1]) # 逆辺 # Ford-Fulkerson法(最大フローを求める) def Ford_Fulkerson(adjL, vSt, vEn): # 残余グラフの始点から終点までの経路(増加パス)を、DFSで探索する def DFS(vNow, vEn, fNow): if vNow == vEn: # 終点に到達したら、フローの増加量を返す return fNow used[vNow] = True for i, (v2, cap, iRev) in enumerate(adjL[vNow]): if not used[v2] and cap > 0: # 未探索の頂点への辺の容量に空きがある場合、探索する df = DFS(v2, vEn, min(fNow, cap)) if df > 0: # 始点から終点までの経路を遡って、辺のフローを変更する adjL[vNow][i][1] -= df adjL[v2][iRev][1] += df return df # 現在の頂点からの探索先がない場合、ゼロを返す return 0 numV = len(adjL) MaximumFlow = 0 while True: # 残余グラフの始点から終点までの経路(増加パス)を、DFSで探索する used = [False] * numV df = DFS(vSt, vEn, float('inf')) if df == 0: # 経路が見つからない場合、最大フローの値を返す return MaximumFlow # フローを加算する MaximumFlow += df N = int(input()) As = list(map(int, input().split())) adjList = [[] for v in range(N + 2)] for i, A in enumerate(As, 1): if A <= 0: addEdge(adjList, 0, i, -A) else: addEdge(adjList, i, N + 1, A) for i in range(1, N + 1): for j in range(2 * i, N + 1, i): addEdge(adjList, i, j, float('inf')) # Ford-Fulkerson法(最大フローを求める) mf = Ford_Fulkerson(adjList, 0, N + 1) print(sum([A for A in As if A > 0]) - mf) ```
output
1
100,181
10
200,363
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,182
10
200,364
"Correct Solution: ``` from collections import deque import sys sys.setrecursionlimit(200000) n = int(input()) houseki = list(map(int,input().split())) g = [[] for i in range(n+2)] INF = float("inf") MAX = 0 for i in range(n): if houseki[i] <= 0: g[0].append([i+1,-houseki[i],len(g[i+1])]) g[i+1].append([0,0,len(g[0])-1]) else: g[n+1].append([i+1,0,len(g[i+1])]) g[i+1].append([n+1,houseki[i],len(g[n+1])-1]) MAX += houseki[i] j = (i+1)*2 while j <= n: g[i+1].append([j,INF,len(g[j])]) g[j].append([i+1,0,len(g[i+1])-1]) j += i+1 def bfs(s,t): global level que = deque([s]) level[s] = 0 while que: v = que.popleft() lv = level[v] +1 for y, cap, rev in g[v]: if cap and level[y] is None: level[y] = lv que.append(y) return level[t] if level[t] else 0 def dfs(x,t,f): if x == t: return f for j in range(it[x],len(g[x])): it[x] = j y, cap, rev = g[x][j] if cap and level[x] < level[y]: d = dfs(y,t,min(f,cap)) if d: g[x][j][1] -= d g[y][rev][1] += d return d return 0 flow = 0 f = INF = float("inf") level = [None]*(n+2) while bfs(0,n+1): it = [0]* (n+2) f = INF while f: f = dfs(0,n+1,INF) flow += f level = [None]*(n+2) print(MAX-flow) ```
output
1
100,182
10
200,365
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,183
10
200,366
"Correct Solution: ``` """ https://atcoder.jp/contests/arc085/tasks/arc085_c """ from collections import defaultdict from collections import deque def Ford_Fulkerson_Func(s,g,lines,cost): N = len(cost) ans = 0 queue = deque([ [s,float("inf")] ]) ed = [True] * N ed[s] = False route = [0] * N route[s] = -1 while queue: now,flow = queue.pop() for nex in lines[now]: if ed[nex]: flow = min(cost[now][nex],flow) route[nex] = now queue.append([nex,flow]) ed[nex] = False if nex == g: ans += flow break else: continue break else: return False,ans t = g s = route[t] while s != -1: cost[s][t] -= flow if cost[s][t] == 0: lines[s].remove(t) if cost[t][s] == 0: lines[t].add(s) cost[t][s] += flow t = s s = route[t] return True,ans def Ford_Fulkerson(s,g,lines,cost): ans = 0 while True: fl,nans = Ford_Fulkerson_Func(s,g,lines,cost) if fl: ans += nans continue else: break return ans N = int(input()) a = list(map(int,input().split())) S = N+1 T = N+2 lis = defaultdict(set) cost = [[0] * (N+3) for i in range(N+3)] for i in range(N): if a[i] > 0: lis[i+1].add(T) cost[i+1][T] += a[i] elif a[i] < 0: lis[S].add(i+1) cost[S][i+1] -= a[i] for i in range(1,N+1): for j in range(i*2,N+1,i): lis[i].add(j) cost[i][j] += float("inf") m = Ford_Fulkerson(S,T,lis,cost) ss = 0 for i in range(N): if a[i] > 0: ss += a[i] print (ss-m) ```
output
1
100,183
10
200,367
Provide a correct Python 3 solution for this coding contest problem. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000
instruction
0
100,184
10
200,368
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import time,random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def JA(a, sep): return sep.join(map(str, a)) def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) class Flow(): def __init__(self, e, N): self.E = e self.N = N def max_flow(self, s, t): r = 0 e = self.E def f(c, cap): v = self.v v[c] = 1 if c == t: return cap for i in range(self.N): if v[i] or e[c][i] <= 0: continue cp = min(cap, e[c][i]) k = f(i, cp) if k > 0: e[c][i] -= k e[i][c] += k return k return 0 while True: self.v = [None] * self.N fs = f(s, inf) if fs == 0: break r += fs return r def main(): n = I() a = LI() s = n t = n + 1 e = [[0] * (n+2) for _ in range(n+2)] for i in range(n): c = a[i] if c < 0: e[s][i] = -c ii = i + 1 for j in range(ii*2, n+1, ii): e[i][j-1] = inf else: e[i][t] = c fl = Flow(e, n+2) r = fl.max_flow(s,t) return sum(map(lambda x: max(0,x), a)) - r # start = time.time() print(main()) # pe(time.time() - start) ```
output
1
100,184
10
200,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` # Dinic's algorithm from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow N=int(input()) a=list(map(int,input().split())) jew=Dinic(N+2) ans=0 for i in range(N): if a[i]>=0: ans+=a[i] jew.add_edge(0,i+1,a[i]) else: jew.add_edge(i+1,N+1,-a[i]) inf=10**15 for i in range(1,N+1): for j in range(1,N//i+1): jew.add_edge(i*j,i,inf) f=jew.flow(0,N+1) print(ans-f) ```
instruction
0
100,185
10
200,370
Yes
output
1
100,185
10
200,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` N=int(input()) P=[int(i) for i in input().split()] inf = 10**20 table=[[0]*(N+2) for i in range(N+2)] for i in range(1,N+1): if P[i-1]>0: table[i][N+1]=P[i-1] else: table[0][i]=-P[i-1] for j in range(2*i,N+1,i): table[i][j]=inf #print(table) def fk(x,t,f): #print(x) visit[x]=True if x==t: return f for i in range(N+2): if (not visit[i]) and table[x][i]>0: df=fk(i,t,min(f,table[x][i])) if df>0: table[x][i]-=df table[i][x]+=df return df return 0 ans=0 while True: visit=[False]*(N+2) df=fk(0,N+1,inf) if df>0: ans+=df else: break num=sum([p for p in P if p>0]) print(num-ans) ```
instruction
0
100,186
10
200,372
Yes
output
1
100,186
10
200,373
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` from copy import deepcopy N = int(input()) a0 = list(map(int, input().split())) for i in range(N, 0, -1): if a0[i - 1] < 0: tmp = 0 b = [] for j in range(1, 101): if i * j - 1 >= N: break tmp += a0[i * j - 1] b.append(i * j - 1) if tmp < 0: for j in b: a0[j] = 0 for k1 in range(N, 0, -1): for k2 in range(k1-1, 0, -1): a = deepcopy(a0) for j in range(1, 101): if k1*j-1 >= N: break a[k1*j-1] = 0 for j in range(1, 101): if k2*j-1 >= N: break a[k2*j-1] = 0 for i in range(N, 0, -1): if a[i - 1] < 0: tmp = 0 b = [] for j in range(1, 101): if i * j - 1 >= N: break tmp += a[i * j - 1] b.append(i * j - 1) if tmp < 0: for j in b: a[j] = 0 for i in range(N, 0, -1): if a0[i - 1] < 0: tmp = 0 b = [] for j in range(1, 101): if i * j - 1 >= N: break tmp += a0[i * j - 1] b.append(i * j - 1) if tmp < 0: for j in b: a0[j] = 0 if sum(a) > sum(a0): a0 = a print(sum(a0)) ```
instruction
0
100,187
10
200,374
Yes
output
1
100,187
10
200,375
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None]#(行き先、容量、逆辺の参照) forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow N = int(input()) a = list(map(int, input().split())) D = Dinic(N+2) inf = 10**18 profit = 0 cost = 0 for i in range(N): if a[i]>=0: D.add_edge(i, N+1, a[i]) else: D.add_edge(N, i, -a[i]) cost+=a[i] profit+=abs(a[i]) for i in range(1, N+1): for k in range(2, N//i+1): D.add_edge(i-1, i*k-1, inf) f = D.flow(N, N+1) print(profit-f+cost) ```
instruction
0
100,188
10
200,376
Yes
output
1
100,188
10
200,377
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` N = int(input()) As = list(map(int, input().split())) while True: vs = [sum(As[x::x + 1]) for x in range(N)] vMin = min(vs) if vMin >= 0: break xMin = N - 1 - vs[::-1].index(vMin) for x in range(xMin, N, xMin + 1): As[x] = 0 print(vs[0]) ```
instruction
0
100,189
10
200,378
No
output
1
100,189
10
200,379
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` n = int(input()) a = [int(i) for i in input().split()] c,ans = [1]*n,sum(a) def f(x): num = 0 for i in range(x,n+1,x): if c[i-1]: num+=a[i-1] if num<0: for i in range(x,n+1,x): c[i-1] = 0 return num else: return 0 for i in range(n,0,-1): ans-=f(i) print(ans) ```
instruction
0
100,190
10
200,380
No
output
1
100,190
10
200,381
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) flag = 1 for i in a: if i > 0: flag = 0 if flag: print(0) else: flag_ = 1 while flag_: ans = sum(a) prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] b = [[] for i in range(100)] for i in prime: j = i while j <= n: b[i].append(a[j - 1]) j += i c = [] flag_ = 0 for i in b: if sum(i) < 0: flag_ = 1 c.append(sum(i)) dis = 0 mi = 0 for i in range(100): if c[i] < mi: mi = c[i] dis = i if mi >= 0: break for i in range(1, n + 1): if i % dis == 0: a[i - 1] = 0 ans += -mi print(ans) ```
instruction
0
100,191
10
200,382
No
output
1
100,191
10
200,383
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have N gemstones labeled 1 through N. You can perform the following operation any number of times (possibly zero). * Select a positive integer x, and smash all the gems labeled with multiples of x. Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money. By optimally performing the operation, how much yen can you earn? Constraints * All input values are integers. * 1 \leq N \leq 100 * |a_i| \leq 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the maximum amount of money that can be earned. Examples Input 6 1 2 -6 4 5 3 Output 12 Input 6 100 -100 -100 -100 100 -100 Output 200 Input 5 -1 -2 -3 -4 -5 Output 0 Input 2 -1000 100000 Output 99000 Submitted Solution: ``` class Ford_Fulkerson: def __init__(self, v, inf=float("inf")): self.V = v self.inf = inf self.G = [[] for _ in range(v)] self.used = [False for _ in range(v)] def addEdge(self, fm, to, cap): self.G[fm].append({'to':to, 'cap':cap, 'rev':len(self.G[to])}) self.G[to].append({'to':fm, 'cap':0, 'rev':len(self.G[fm])-1}) def dfs(self, v, t, f): if v == t: return f self.used[v] = True for i in range(len(self.G[v])): e = self.G[v][i] if self.used[e["to"]] != True and e['cap'] > 0: d = self.dfs(e['to'], t ,min(f, e['cap'])) if d > 0: e['cap'] -= d self.G[e['to']][e['rev']]['cap'] += d return d return 0 def max_flow(self,s,t): flow = 0 while True: self.used = [False for i in range(self.V)] f = self.dfs(s,t,self.inf) if f == 0: return flow flow += f from sys import stdin, setrecursionlimit def IL():return list(map(int, stdin.readline().split())) setrecursionlimit(1000000) def main(): N = int(input()) a = IL() d = Ford_Fulkerson(102) s=N t=N+1 for i in range(N): d.addEdge(s,i,max(-a[i],0)) d.addEdge(i,t,max(a[i],0)) x = i+1 y=x+x for j in range(y,N+1): d.addEdge(x-1,y-1,float("inf")) res = sum([max(i,0) for i in a]) print(res - d.max_flow(s,t)) if __name__ == "__main__": main() ```
instruction
0
100,192
10
200,384
No
output
1
100,192
10
200,385
Provide a correct Python 3 solution for this coding contest problem. Better things, cheaper. There is a fierce battle at the time sale held in some supermarkets today. "LL-do" here in Aizu is one such supermarket, and we are holding a slightly unusual time sale to compete with other chain stores. In a general time sale, multiple products are cheaper at the same time, but at LL-do, the time to start the time sale is set differently depending on the target product. The Sakai family is one of the households that often use LL-do. Under the leadership of his wife, the Sakai family will hold a strategy meeting for the time sale to be held next Sunday, and analyze in what order the shopping will maximize the discount. .. The Sakai family, who are familiar with the inside of the store, drew a floor plan of the sales floor and succeeded in grasping where the desired items are, how much the discount is, and the time until they are sold out. The Sakai family was perfect so far, but no one could analyze it. So you got to know the Sakai family and decided to write a program. The simulation is performed assuming that one person moves. There are 10 types of products, which are distinguished by the single-digit product number g. The time sale information includes the item number g, the discount amount d, the start time s of the time sale, and the sold-out time e. The inside of the store is represented by a two-dimensional grid consisting of squares X in width and Y in height, and either a passage or a product shelf is assigned to each square. There is one type of product on a shelf, which is distinguished by item number g. You can buy any item, but you must not buy more than one item with the same item number. From the product shelves, you can pick up products in any direction as long as they are in the aisle squares that are adjacent to each other. You can pick up items from the time when the time sale starts, but you cannot pick up items from the time when they are sold out. Also, the time starts from 0 when you enter the store. You can move from the current square to the adjacent aisle squares on the top, bottom, left, and right, but you cannot move to the squares on the product shelves. You can't even go outside the store represented by the grid. One unit time elapses for each move. Also, do not consider the time to pick up the product. Please create a program that outputs the maximum value of the total discount amount of the products that you could get by inputting the floor plan of the store, the initial position of the shopper and the time sale information of the product. input Given multiple datasets. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format: X Y m1,1 m2,1 ... mX,1 m1,2 m2,2 ... mX,2 :: m1, Y m2, Y ... mX, Y n g1 d1 s1 e1 g2 d2 s2 e2 :: gn dn sn en The first line gives the store sizes X, Y (3 ≤ X, Y ≤ 20). In the following Y row, the store information mi, j in the i-th column and j-th row is given with the following contents. . (Period): Aisle square Number: Product number P: The initial position of the shopper The following line is given the number of timesale information n (1 ≤ n ≤ 8). The next n lines are given the i-th time sale information gi di si ei (0 ≤ gi ≤ 9, 1 ≤ di ≤ 10000, 0 ≤ si, ei ≤ 100). The number of datasets does not exceed 50. output For each data set, the maximum value of the total discount amount of the products that can be taken is output on one line. Example Input 6 5 1 1 . 0 0 4 1 . . . . . . . 2 2 . . . . 2 2 3 3 P . . . . . 5 0 50 5 10 1 20 0 10 2 10 5 15 3 150 3 5 4 100 8 9 0 0 Output 180
instruction
0
100,255
10
200,510
"Correct Solution: ``` from heapq import heappush, heappop from string import digits import sys readline = sys.stdin.readline write = sys.stdout.write dd = ((-1, 0), (0, -1), (1, 0), (0, 1)) INF = 10**9 while 1: X, Y = map(int, readline().split()) if X == Y == 0: break MP = [readline().split() for i in range(Y)] N = int(readline()) I = [[] for i in range(10)] for i in range(N): g, d, s, e = map(int, readline().split()) if s < e: I[g].append((i, d, s, e)) for ss in I: ss.sort(key=lambda x: x[1]) V = [0]*(1 << N) for state in range(1 << N): v = 0 for ss in I: x = 0 for i, d, s, e in ss: if state & (1 << i): x = d v += x V[state] = v U = [[None]*X for i in range(Y)] sx = sy = 0 for i in range(Y): for j in range(X): c = MP[i][j] if c in digits: continue if c == 'P': sy = i; sx = j s = set() for dx, dy in dd: nx = j + dx; ny = i + dy if not 0 <= nx < X or not 0 <= ny < Y: continue c = MP[ny][nx] if c in digits: for e in I[int(c)]: s.add(e) U[i][j] = s D = [[[INF]*(1 << N) for i in range(X)] for j in range(Y)] que = [(0, 0, sx, sy)] D[sy][sx][0] = 0 while que: cost, state, x, y = heappop(que) D0 = D[y][x] t = D0[state] if t < cost: continue for i, d, s, e in U[y][x]: if t < e and state & (1 << i) == 0: t0 = max(s, t) n_state = state | (1 << i) if t0 < D0[n_state]: D0[n_state] = t0 heappush(que, (t0, n_state, x, y)) for dx, dy in dd: nx = x + dx; ny = y + dy if not 0 <= nx < X or not 0 <= ny < Y or U[ny][nx] is None: continue if t+1 < D[ny][nx][state]: D[ny][nx][state] = t+1 heappush(que, (t+1, state, nx, ny)) ans = 0 for x in range(X): for y in range(Y): D0 = D[y][x] for state in range(1 << N): if D0[state] < INF: ans = max(ans, V[state]) write("%d\n" % ans) ```
output
1
100,255
10
200,511
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,436
10
200,872
Tags: binary search, data structures, greedy Correct Solution: ``` import sys import heapq def solve(pr, mm): omm = [] n = len(mm) for i in range(n + 1): omm.append([]) for i in range(n): omm[mm[i]].append(pr[i]) heap = [] c = 0 t = n p = 0 for i in range(n, -1, -1): for h in omm[i]: heapq.heappush(heap, h) t -= len(omm[i]) mn = max(i - c - t, 0) c += mn for j in range(mn): p += heapq.heappop(heap) return p if __name__ == "__main__": input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ t = int(input().strip()) for i in range(t): n = int(input().strip()) ms = [] ps = [] for j in range(n): arr = [int(v) for v in input().strip().split(' ')] ms.append(arr[0]) ps.append(arr[1]) print(solve(ps, ms)) ```
output
1
100,436
10
200,873
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,437
10
200,874
Tags: binary search, data structures, greedy Correct Solution: ``` import sys import heapq as hq readline = sys.stdin.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n = ni() vot = [tuple(nm()) for _ in range(n)] vot.sort(key = lambda x: (-x[0], x[1])) q = list() c = 0 cost = 0 for i in range(n): hq.heappush(q, vot[i][1]) while n - i - 1 + c < vot[i][0]: cost += hq.heappop(q) c += 1 print(cost) return # solve() T = ni() for _ in range(T): solve() ```
output
1
100,437
10
200,875
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,438
10
200,876
Tags: binary search, data structures, greedy Correct Solution: ``` import sys from heapq import heappop, heappush reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ t = int(input()) for _ in range(t): n = int(input()) mp = [] for i in range(n): mi, pi = map(int, input().split()) mp.append((mi, pi)) mp.sort() prices = [] cost = 0 bribed = 0 i = n - 1 while i >= 0: currM = mp[i][0] heappush(prices, mp[i][1]) while i >= 1 and mp[i-1][0] == currM: i -= 1 heappush(prices, mp[i][1]) already = i + bribed for k in range(max(0, currM - already)): cost += heappop(prices) bribed += 1 i -= 1 print(cost) ```
output
1
100,438
10
200,877
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,439
10
200,878
Tags: binary search, data structures, greedy Correct Solution: ``` import sys input = sys.stdin.readline import heapq from itertools import accumulate t=int(input()) for test in range(t): n=int(input()) M=[[] for i in range(n)] MCOUNT=[0]*(n) for i in range(n): m,p=map(int,input().split()) M[m].append(p) MCOUNT[m]+=1 #print(M) #print(MCOUNT) ACC=list(accumulate(MCOUNT)) #print(ACC) HQ=[] ANS=0 use=0 for i in range(n-1,-1,-1): for j in M[i]: heapq.heappush(HQ,j) #print(HQ) while ACC[i-1]+use<i: x=heapq.heappop(HQ) ANS+=x use+=1 print(ANS) ```
output
1
100,439
10
200,879
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,440
10
200,880
Tags: binary search, data structures, greedy Correct Solution: ``` import sys input = sys.stdin.readline import heapq as hq t = int(input()) for _ in range(t): n = int(input()) vt = [list(map(int,input().split())) for i in range(n)] vt.sort(reverse=True) q = [] hq.heapify(q) ans = 0 cnt = 0 for i in range(n): hq.heappush(q,vt[i][1]) if vt[i][0] >= n-i+cnt: ans += hq.heappop(q) cnt += 1 print(ans) ```
output
1
100,440
10
200,881
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,441
10
200,882
Tags: binary search, data structures, greedy Correct Solution: ``` import heapq import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) info = [list(map(int, input().split())) for i in range(n)] info = sorted(info) cnt = [0] * n for i in range(n): ind = info[i][0] cnt[ind] += 1 ruiseki_cnt = [0] * (n+1) for i in range(n): ruiseki_cnt[i+1] = ruiseki_cnt[i] + cnt[i] # print(cnt) # print(ruiseki_cnt) need = [0] * n for i in range(1,n): if cnt[i] != 0 and i > ruiseki_cnt[i]: need[i] = min(i - ruiseki_cnt[i], i) # print(need) info = sorted(info, reverse = True) #print(info) num = n - 1 pos = 0 q = [] used_cnt = 0 ans = 0 while True: if num == -1: break while True: if pos < n and info[pos][0] >= num: heapq.heappush(q, info[pos][1]) pos += 1 else: break if need[num] - used_cnt > 0: tmp = need[num] - used_cnt for _ in range(tmp): ans += heapq.heappop(q) used_cnt += tmp num -= 1 print(ans) ```
output
1
100,441
10
200,883
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,442
10
200,884
Tags: binary search, data structures, greedy Correct Solution: ``` from sys import stdin, stdout import heapq class MyHeap(object): def __init__(self, initial=None, key=lambda x:x): self.key = key if initial: self._data = [(key(item), item) for item in initial] heapq.heapify(self._data) else: self._data = [] def push(self, item): heapq.heappush(self._data, (self.key(item), item)) def pop(self): return heapq.heappop(self._data)[1] def print(self): for hd in self._data: print(hd) print('-------------------------------') def getminnumerofcoins(n, mpa): res = 0 mpa.sort(key=lambda x: (x[0], -x[1])) gap = [] cur = 0 for i in range(len(mpa)): mp = mpa[i] #print(mp[0]) if mp[0] > cur: t = [i, mp[0]-cur] gap.append(t) #cur = mp[0] cur += 1 #print(gap) if len(gap) == 0: return 0 hp = MyHeap(key=lambda x: x[1]) gp = gap.pop() lidx = gp[0] remaing = gp[1] #print(remaing) for i in range(lidx, len(mpa)): ci = [i, mpa[i][1]] hp.push(ci) cur = 0 offset = 0 for i in range(len(mpa)): mp = mpa[i] need = mp[0] - cur if need > 0: for j in range(need): if (remaing == 0 or len(hp._data) == 0) and len(gap) > 0: #print(i) lg = gap.pop() while len(gap) > 0 and lg[1] - offset <= 0: lg = gap.pop() for k in range(lg[0], lidx): ci = [k, mpa[k][1]] hp.push(ci) lidx = lg[0] remaing = lg[1] - offset c = hp.pop() #print(c) if c[0] == i: c = hp.pop() #print(c) res += c[1] cur += 1 offset += 1 remaing -= 1 cur += 1 return res if __name__ == '__main__': t = int(stdin.readline()) for i in range(t): n = int(stdin.readline()) mpa = [] for j in range(n): mp = list(map(int, stdin.readline().split())) mpa.append(mp) res = getminnumerofcoins(n, mpa) stdout.write(str(res) + '\n') ```
output
1
100,442
10
200,885
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
instruction
0
100,443
10
200,886
Tags: binary search, data structures, greedy Correct Solution: ``` import sys from array import array # noqa: F401 import typing as Tp # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') def main(): from collections import defaultdict from heapq import heappop, heappush t = int(input()) ans = [''] * t for ti in range(t): n = int(input()) dd: Tp.Dict[int, Tp.List[int]] = defaultdict(list) costs = [0] * n for i in range(n): mi, pi = map(int, input().split()) dd[mi].append(i) costs[i] = pi hq = [] for cnt in range(n): for x in dd[cnt]: heappush(hq, -costs[x]) if hq: heappop(hq) ans[ti] = str(-sum(hq)) sys.stdout.buffer.write(('\n'.join(ans) + '\n').encode('utf-8')) if __name__ == '__main__': main() ```
output
1
100,443
10
200,887
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters. The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n). It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}. In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}. Submitted Solution: ``` import sys def I(): return sys.stdin.readline().rstrip() class Heap: def __init__( self ): self.l = [ -1 ] self.n = 0 def n( self ): return self.n def top( self ): return self.l[ 1 ] def ins( self, x ): self.l.append( x ) n = len( self.l ) - 1 i = n while i > 1: j = i // 2 if self.l[ j ] > self.l[ i ]: self.l[ j ], self.l[ i ] = self.l[ i ], self.l[ j ] i = j else: break def pop( self ): r = self.l[ 1 ] l = self.l.pop() n = len( self.l ) - 1 if n: self.l[ 1 ] = l i = 1 while True: j = i * 2 k = j + 1 if k < len( self.l ) and self.l[ i ] > max( self.l[ j ], self.l[ k ] ): if self.l[ j ] == min( self.l[ j ], self.l[ k ] ): self.l[ i ], self.l[ j ] = self.l[ j ], self.l[ i ] i = j else: self.l[ i ], self.l[ k ] = self.l[ k ], self.l[ i ] i = k elif k < len( self.l ) and self.l[ i ] > self.l[ k ]: self.l[ i ], self.l[ k ] = self.l[ k ], self.l[ i ] i = k elif j < len( self.l ) and self.l[ i ] > self.l[ j ]: self.l[ i ], self.l[ j ] = self.l[ j ], self.l[ i ] i = j else: break return r t = int( I() ) for _ in range( t ): n = int( I() ) voter = [ list( map( int, I().split() ) ) for _ in range( n ) ] h = Heap() d = {} for m, p in voter: if m not in d: d[ m ] = [] d[ m ].append( p ) need = {} c = 0 sk = sorted( d.keys() ) for m in sk: need[ m ] = max( 0, m - c ) c += len( d[ m ] ) c = 0 ans = 0 for m in sk[::-1]: for p in d[ m ]: h.ins( p ) while c < need[ m ]: c += 1 ans += h.pop() print( ans ) ```
instruction
0
100,444
10
200,888
Yes
output
1
100,444
10
200,889
Provide tags and a correct Python 3 solution for this coding contest problem. Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1? Input The first line contains a single integer n (1 ≤ n ≤ 2·109). The second line contains a single integer k (1 ≤ k ≤ 2·109). The third line contains a single integer A (1 ≤ A ≤ 2·109). The fourth line contains a single integer B (1 ≤ B ≤ 2·109). Output Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: * Subtract 1 from x (9 → 8) paying 3 coins. * Divide x by 2 (8 → 4) paying 1 coin. * Divide x by 2 (4 → 2) paying 1 coin. * Divide x by 2 (2 → 1) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
instruction
0
100,902
10
201,804
Tags: dp, greedy Correct Solution: ``` n = int(input()) k = int(input()) A = int(input()) B = int(input()) ans = 0 if k==1: print((n-1)*A) exit() while n>1: if n%k!=0: if n<k: ans += (n-1)*A n = 1 else: ans += (n-n//k*k)*A n = n//k*k else: ans += min((n-n//k)*A, B) n //= k print(ans) ```
output
1
100,902
10
201,805
Provide tags and a correct Python 3 solution for this coding contest problem. Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1? Input The first line contains a single integer n (1 ≤ n ≤ 2·109). The second line contains a single integer k (1 ≤ k ≤ 2·109). The third line contains a single integer A (1 ≤ A ≤ 2·109). The fourth line contains a single integer B (1 ≤ B ≤ 2·109). Output Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: * Subtract 1 from x (9 → 8) paying 3 coins. * Divide x by 2 (8 → 4) paying 1 coin. * Divide x by 2 (4 → 2) paying 1 coin. * Divide x by 2 (2 → 1) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
instruction
0
100,903
10
201,806
Tags: dp, greedy Correct Solution: ``` n=int(input()) k=int(input()) a=int(input()) b=int(input()) x=n c=0 if k==1: c=a*(n-1) else: while x>=k: y=x%k c+=y*a x-=y c+=min(b,a*(x-x//k)) x=(x//k) c+=a*(x-1) print(c) ```
output
1
100,903
10
201,807
Provide tags and a correct Python 3 solution for this coding contest problem. Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1? Input The first line contains a single integer n (1 ≤ n ≤ 2·109). The second line contains a single integer k (1 ≤ k ≤ 2·109). The third line contains a single integer A (1 ≤ A ≤ 2·109). The fourth line contains a single integer B (1 ≤ B ≤ 2·109). Output Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: * Subtract 1 from x (9 → 8) paying 3 coins. * Divide x by 2 (8 → 4) paying 1 coin. * Divide x by 2 (4 → 2) paying 1 coin. * Divide x by 2 (2 → 1) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
instruction
0
100,904
10
201,808
Tags: dp, greedy Correct Solution: ``` n=int(input()) k=int(input()) a=int(input()) b=int(input()) s=0 while n!=1: if k==1: s=(n-1)*a break if n>=k: if n%k==0: if b<a*(n-(n//k)): n=n//k s+=b else: s+=a*(n-(n//k)) n=n//k else: d=n%k s+=d*a n-=d else: s+=(n-1)*a n=1 print(s) ```
output
1
100,904
10
201,809
Provide tags and a correct Python 3 solution for this coding contest problem. Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1? Input The first line contains a single integer n (1 ≤ n ≤ 2·109). The second line contains a single integer k (1 ≤ k ≤ 2·109). The third line contains a single integer A (1 ≤ A ≤ 2·109). The fourth line contains a single integer B (1 ≤ B ≤ 2·109). Output Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: * Subtract 1 from x (9 → 8) paying 3 coins. * Divide x by 2 (8 → 4) paying 1 coin. * Divide x by 2 (4 → 2) paying 1 coin. * Divide x by 2 (2 → 1) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
instruction
0
100,905
10
201,810
Tags: dp, greedy Correct Solution: ``` x = int(input()) k = int(input()) A = int(input()) B = int(input()) p = 0 while x>1: if k==1: print(A*(x-1)) exit() if x%k == 0: p += min(B,(x-x/k)*A) x =x/k else: if x-x%k==0: p+=(x%k-1)*A print(int(p)) exit() else: p+=(x%k)*A x-=x%k print(int(p)) ```
output
1
100,905
10
201,811
Provide tags and a correct Python 3 solution for this coding contest problem. Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1? Input The first line contains a single integer n (1 ≤ n ≤ 2·109). The second line contains a single integer k (1 ≤ k ≤ 2·109). The third line contains a single integer A (1 ≤ A ≤ 2·109). The fourth line contains a single integer B (1 ≤ B ≤ 2·109). Output Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: * Subtract 1 from x (9 → 8) paying 3 coins. * Divide x by 2 (8 → 4) paying 1 coin. * Divide x by 2 (4 → 2) paying 1 coin. * Divide x by 2 (2 → 1) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
instruction
0
100,906
10
201,812
Tags: dp, greedy Correct Solution: ``` n = int(input()) k = int(input()) A = int(input()) B = int(input()) res = 0 while n>k: if n % k >0: res += (n%k) * A n -= (n%k) dif = n - (n//k) if dif*A > B: res += B n = n//k else: res += (n-1)*A n = 1 if n == k: dif = n - (n//k) if dif*A > B: res += B n = n//k else: res += (n-1)*A n = 1 if n >1 : res += (n-1)*A n = 1 print(res) ```
output
1
100,906
10
201,813
Provide tags and a correct Python 3 solution for this coding contest problem. Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1? Input The first line contains a single integer n (1 ≤ n ≤ 2·109). The second line contains a single integer k (1 ≤ k ≤ 2·109). The third line contains a single integer A (1 ≤ A ≤ 2·109). The fourth line contains a single integer B (1 ≤ B ≤ 2·109). Output Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: * Subtract 1 from x (9 → 8) paying 3 coins. * Divide x by 2 (8 → 4) paying 1 coin. * Divide x by 2 (4 → 2) paying 1 coin. * Divide x by 2 (2 → 1) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
instruction
0
100,907
10
201,814
Tags: dp, greedy Correct Solution: ``` n, k, A, B = tuple(map(int, (input(), input(), input(), input()))) ans = 0 while(n != 1 and k != 1): if(n % k == 0): ans += min(B, (n - (n // k)) * A) n //= k elif(n > k): ans += (n % k) * A n -= n % k else: ans += (n-1) * A n -= (n-1) if(k == 1): ans = (n - 1) * A print(ans) ```
output
1
100,907
10
201,815
Provide tags and a correct Python 3 solution for this coding contest problem. Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations: 1. Subtract 1 from x. This operation costs you A coins. 2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins. What is the minimum amount of coins you have to pay to make x equal to 1? Input The first line contains a single integer n (1 ≤ n ≤ 2·109). The second line contains a single integer k (1 ≤ k ≤ 2·109). The third line contains a single integer A (1 ≤ A ≤ 2·109). The fourth line contains a single integer B (1 ≤ B ≤ 2·109). Output Output a single integer — the minimum amount of coins you have to pay to make x equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: * Subtract 1 from x (9 → 8) paying 3 coins. * Divide x by 2 (8 → 4) paying 1 coin. * Divide x by 2 (4 → 2) paying 1 coin. * Divide x by 2 (2 → 1) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.
instruction
0
100,908
10
201,816
Tags: dp, greedy Correct Solution: ``` n,k,a,b=(int(input()) for i in range(4)) x,s=n,0 if k<=1: print((x-1)*a) quit() while x!=1: if k>x: s,x=s+(x-1)*a,1 elif x%k!=0: s,x=s+(x%k)*a,x-x%k elif x*(k-1)//k*a>b: s,x=s+b,x//k else: s,x=s+x*(k-1)//k*a,x//k print(s) ```
output
1
100,908
10
201,817