message
stringlengths
2
16.2k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
575
109k
cluster
float64
16
16
__index_level_0__
int64
1.15k
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975 Submitted Solution: ``` import heapq x, y, z = [int(item) for item in input().split()] gsb = [] for i in range(x + y + z): gsb.append([int(item) for item in input().split()]) gsb.sort(key=lambda x: x[0] - x[1], reverse=True) g_sum = sum(item[0] for item in gsb[:x]) s_sum = sum(item[1] for item in gsb[x+z:x+y+z]) b_sum = sum(item[2] for item in gsb[x:x+z]) gb_pq = [a - c for a, b, c in gsb[:x]] sb_pq = [b - c for a, b, c in gsb[x+z:x+y+z]] heapq.heapify(gb_pq) heapq.heapify(sb_pq) ans_gb = [0] gb_total_delta = 0 for a, b, c in gsb[x:x+z]: new_gb = a - c small_gb = heapq.heappushpop(gb_pq, new_gb) gb_total_delta += new_gb - small_gb ans_gb.append(gb_total_delta) ans_sb = [0] sb_total_delta = 0 for a, b, c in gsb[x:x+z][::-1]: new_sb = b - c small_sb = heapq.heappushpop(sb_pq, new_sb) sb_total_delta += new_sb - small_sb ans_sb.append(sb_total_delta) ans_sb.reverse() max_delta = 0 for gb, sb in zip(ans_gb, ans_sb): max_delta = max(max_delta, gb + sb) print(g_sum + s_sum + b_sum + max_delta) ```
instruction
0
95,140
16
190,280
Yes
output
1
95,140
16
190,281
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975 Submitted Solution: ``` import sys input = sys.stdin.readline from heapq import heappush, heappushpop """ 銅貨の集合を固定すると、金-銀でソートして貪欲に金をとることになる 逆に金-銀でソートしておくと、金が埋まるまでは金or銅の2択 最後にとる金の番号X+nをfix → 手前は金-銅で貪欲 → queueで管理できる """ X,Y,Z = map(int,input().split()) ABC = [[int(x) for x in input().split()] for _ in range(X+Y+Z)] ABC.sort(key = lambda x: x[0]-x[1],reverse=True) q = [] sum_a = 0 sum_c = 0 for a,b,c in ABC[:X]: # 金を入れる。銅-金の優先度 heappush(q,(a-c,a)) sum_a += a A = [0] * (Z+1) LC = [0] * (Z+1) A[0] = sum_a for i,(a,b,c) in enumerate(ABC[X:X+Z],1): sum_a += a x,del_a = heappushpop(q,(a-c,a)) sum_a -= del_a sum_c += del_a-x A[i] = sum_a LC[i] = sum_c ABC_rev = ABC[::-1] q = [] sum_b = 0 sum_c = 0 for a,b,c in ABC_rev[:Y]: heappush(q,(b-c,b)) sum_b += b B = [0] * (Z+1) RC = [0] * (Z+1) B[0] += sum_b for i,(a,b,c) in enumerate(ABC_rev[Y:Y+Z],1): sum_b += b x,del_b = heappushpop(q,(b-c,b)) sum_b -= del_b sum_c += del_b-x B[i] = sum_b RC[i] = sum_c answer = max(sum(x) for x in zip(A,LC,B[::-1],RC[::-1])) print(answer) ```
instruction
0
95,141
16
190,282
Yes
output
1
95,141
16
190,283
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975 Submitted Solution: ``` from heapq import heappushpop import sys X, Y, Z = map(int, sys.stdin.readline().split()) N = X+Y+Z ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(N)] ABC.sort(key = lambda x: x[0] - x[1], reverse = True) GB = [None]*N Q = [a - c for a, _, c in ABC[:X]] Q.sort() gs = sum(a for a, _, _ in ABC[:X]) GB[X-1] = gs for i, (a, b, c) in enumerate(ABC[X:X+Z], X): gs += - heappushpop(Q, a - c) + a GB[i] = gs SB = [None]*N Q = [b - c for _, b, c in ABC[X+Z:]] Q.sort() ss = sum(b for _, b, _ in ABC[X+Z:]) SB[-Y-1] = ss for i, (a, b, c) in enumerate(ABC[X+Z-1:X-1:-1], 1): i = -Y - i ss += - heappushpop(Q, b - c) + b SB[i-1] = ss print(max(i + j for i, j in zip(GB[X-1:X+Z], SB[X-1:X+Z]))) ```
instruction
0
95,142
16
190,284
Yes
output
1
95,142
16
190,285
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975 Submitted Solution: ``` # encoding:utf-8 def main(): N, K = list(map(int, input().split())) nums = list(map(int, input().split())) max_num = max(nums) min_num = min(nums) # 最大値が越えてたらだめ if K > max_num: return("IMPOSSIBLE") decomp_min = decomp(min_num) for x in decomp_min: res = [y % x for y in nums] # 共通因子を持ってなかったらだめ if max(res) == min(res): if K % min_num > 0: return("IMPOSSIBLE") else: return("POSSIBLE") def decomp(n): i = 2 table = [n] while i * i <= n: if n % i == 0: table.append(int(i)) i += 1 return(table) if __name__ == '__main__': print(main()) ```
instruction
0
95,143
16
190,286
No
output
1
95,143
16
190,287
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975 Submitted Solution: ``` import heapq x, y, z = [int(item) for item in input().split()] gsb = [] for i in range(x + y + z): gsb.append([int(item) for item in input().split()]) gsb.sort(key=lambda x: x[0] - x[1], reverse=True) g_sum = sum(item[0] for item in gsb[:x]) s_sum = sum(item[1] for item in gsb[x+z:x+y+z]) b_sum = sum(item[2] for item in gsb[x:x+z]) gb_pq = [a - c for a, b, c in gsb[:x]] sb_pq = [b - c for a, b, c in gsb[x+z:x+y+z]] heapq.heapify(gb_pq) heapq.heapify(sb_pq) ans_gb = [0] gb_total_delta = 0 for a, b, c in gsb[x:x+z]: new_gb = a - c small_gb = heapq.heappushpop(gb_pq, new_gb) gb_total_delta += new_gb - small_gb ans_gb.append(gb_total_delta) ans_sb = [0] sb_total_delta = 0 for a, b, c in gsb[x:x+z][::-1]: new_sb = b - c small_sb = heapq.heappushpop(sb_pq, new_sb) sb_total_delta = new_sb - small_sb ans_sb.append(sb_total_delta) ans_sb.reverse() max_delta = 0 for gb, sb in zip(ans_gb, ans_sb): max_delta = max(max_delta, gb + sb) print(g_sum + s_sum + b_sum + max_delta) ```
instruction
0
95,144
16
190,288
No
output
1
95,144
16
190,289
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975 Submitted Solution: ``` X, Y, Z = map(int, input().split()) N = X + Y + Z a = [] a_ordered = [] for _ in range(N): a_i = list(map(int, input().split())) a.append(a_i) a_ordered.append((a_i[0] - a_i[1], a_i[0], a_i[1], a_i[2])) a_ordered.sort(key=lambda x: x[0]) max_value = 0 for k in range(Y, N - X): a_left = a_ordered[:k] a_left_ordered = [] for i, v in enumerate(a_left): a_left_ordered.append((v[2] - v[3], v[2], v[3])) a_left_ordered.sort(key=lambda x: x[0]) left_value = 0 for i, v in enumerate(a_left_ordered): if i < k - Y: left_value += v[2] else: left_value += v[1] a_right = a_ordered[k:N] a_right_ordered = [] for i, v in enumerate(a_right): a_right_ordered.append((v[1] - v[3], v[1], v[3])) a_right_ordered.sort(key=lambda x: x[0]) right_value = 0 for i, v in enumerate(a_right_ordered): if i < N - k - X: right_value += v[2] else: right_value += v[1] total_value = left_value + right_value #print(total_value) if max_value < total_value: max_value = total_value print(max_value) ```
instruction
0
95,145
16
190,290
No
output
1
95,145
16
190,291
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints * 1 \leq X * 1 \leq Y * 1 \leq Z * X+Y+Z \leq 10^5 * 1 \leq A_i \leq 10^9 * 1 \leq B_i \leq 10^9 * 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Examples Input 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Output 18 Input 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Output 110 Input 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Output 3093929975 Submitted Solution: ``` import math def sum_coin(a,b): #転置して該当列を合算 temp1 = list(map(list, zip(*a))) temp2 = list(map(list, zip(*b))) coins = sum(temp1[2][:Y]) + sum(temp1[3][Y:]) + sum(temp2[3][:(-X)]) + sum(temp2[1][(-X):]) return coins ###入力### input1 = input("").split(" ") X = int(input1[0]) Y = int(input1[1]) Z = int(input1[2]) N = X + Y + Z temp = [ input() for _ in range(N) ] matrix = [None]*N for i in range(N): a = int(temp[i].split(" ")[0]) b = int(temp[i].split(" ")[1]) c = int(temp[i].split(" ")[2]) matrix[i] = [i, a, b, c, (a-b), (b-c), (c-a)] ###処理### matrix.sort(key=lambda k: k[4]) #(金-銀)で昇順ソート #print(matrix) head = sorted(matrix[:Y], key=lambda k: k[5], reverse=True) #(銀-銅)で降順ソート tail = sorted(matrix[Y:], key=lambda k: k[6], reverse=True) #(銅-金)で降順ソート #print("head:",head) #print("tail:",tail) result = sum_coin(head, tail) #print("coins",result) # Y<=k<=(N-X)の中でコイン最大値を求める for k in range(Y,N-X): #print("k",k) #headにmatrix[Y+1]を追加 if head[0][5] < matrix[k][5]: p = 0 elif head[-1][5] > matrix[k][5]: p = len(head) - 1 else: #二分探索 start = 0 last = len(head) - 1 p = last while (last-start) > 1: j = math.ceil((start+last)/2) #print("j",j) if head[j][5] < matrix[k][5] : last = j elif head[j][5] > matrix[k][5]: start = j else: p = j break p = last head.insert(p,matrix[k]) #tailからmatrix[Y+1]を削除 tail.pop(tail.index(matrix[k])) #print("head:",head) #print("tail:",tail) coins = sum_coin(head, tail) #print("coins",coins) #暫定1位更新 if result < coins: result = coins ###出力### print(result) ```
instruction
0
95,146
16
190,292
No
output
1
95,146
16
190,293
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has come up with the following problem. > You are given a sequence d of length N. Find the number of the undirected graphs with N vertices labeled 1,2,...,N satisfying the following conditions, modulo 10^{9} + 7: > > * The graph is simple and connected. > * The degree of Vertex i is d_i. > When 2 \leq N, 1 \leq d_i \leq N-1, {\rm Σ} d_i = 2(N-1), it can be proved that the answer to the problem is \frac{(N-2)!}{(d_{1} -1)!(d_{2} - 1)! ... (d_{N}-1)!}. Snuke is wondering what the answer is when 3 \leq N, 1 \leq d_i \leq N-1, { \rm Σ} d_i = 2N. Solve the problem under this condition for him. Constraints * 3 \leq N \leq 300 * 1 \leq d_i \leq N-1 * { \rm Σ} d_i = 2N Input Input is given from Standard Input in the following format: N d_1 d_2 ... d_{N} Output Print the answer. Examples Input 5 1 2 2 3 2 Output 6 Input 16 2 1 3 1 2 1 4 1 1 2 1 1 3 2 4 3 Output 555275958 Submitted Solution: ``` import sys import numpy as np import numba from numba import njit i8 = numba.int64 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 10**9 + 7 @njit((i8, ), cache=True) def fact_table(N): inv = np.empty(N, np.int64) inv[0] = 0 inv[1] = 1 for n in range(2, N): q, r = divmod(MOD, n) inv[n] = inv[r] * (-q) % MOD fact = np.empty(N, np.int64) fact[0] = 1 for n in range(1, N): fact[n] = n * fact[n - 1] % MOD fact_inv = np.empty(N, np.int64) fact_inv[0] = 1 for n in range(1, N): fact_inv[n] = fact_inv[n - 1] * inv[n] % MOD return fact, fact_inv, inv @njit((i8[::1], ), cache=True) def main(A): # key:サイクルに選んだ個数、(d-2)の和 # value:選んでいないもの:1/(d-1)!、選んだもの:1/(d-2)! の積の和 N = len(A) U = N + 10 dp = np.zeros((U, U), np.int64) dp[0, 0] = 1 fact, fact_inv, inv = fact_table(10**3) for d in A: newdp = np.zeros_like(dp) # 選ばない場合の遷移 newdp += dp * fact_inv[d - 1] % MOD # 選ぶ場合の遷移 if d >= 2: newdp[1:, d - 2:U] += dp[:-1, 0:U + 2 - d] * fact_inv[d - 2] % MOD dp = newdp % MOD ret = 0 for n in range(3, N + 1): for a in range(N + 1): x = a * fact[n - 1] % MOD * fact[N - n - 1] % MOD x = x * inv[2] % MOD * dp[n, a] % MOD ret += x return ret % MOD A = np.array(read().split(), np.int64)[1:] print(main(A)) ```
instruction
0
95,933
16
191,866
No
output
1
95,933
16
191,867
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,778
16
193,556
"Correct Solution: ``` _,*s=open(0).read().split() a=b=c=d=0 for S in s:a+=S[-1]=='A';b+=S[0]=='B';c+=S[-1]+S[0]=='AB';d+=S.count('AB') print(d+min(a,b)-(0<a==b==c)) ```
output
1
96,778
16
193,557
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,779
16
193,558
"Correct Solution: ``` n = int(input()) S = [input() for i in range(n)] ans = 0 a_cnt = 0 b_cnt = 0 ba_cnt = 0 for i in range(n): ans+=S[i].count('AB') if S[i][0]=='B' and S[i][-1]=='A': ba_cnt+=1 elif S[i][-1]=='A': a_cnt+=1 elif S[i][0]=='B': b_cnt+=1 print(ans+min(a_cnt,b_cnt)+ba_cnt-1*(ba_cnt>0 and a_cnt==b_cnt==0)) ```
output
1
96,779
16
193,559
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,780
16
193,560
"Correct Solution: ``` N=int(input()) S=[input() for _ in [0]*N] AB=0 X_A=0 B_X=0 B_A=0 for s in S: AB+=s.count("AB") if s[0]!="B" and s[-1]=="A":X_A+=1 if s[0]=="B" and s[-1]!="A":B_X+=1 if s[0]=="B" and s[-1]=="A":B_A+=1 r=AB if B_A>0: r+=B_A-1 if X_A>0:r+=1;X_A-=1 if B_X>0:r+=1;B_X-=1 r+=min(X_A,B_X) print(r) ```
output
1
96,780
16
193,561
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,781
16
193,562
"Correct Solution: ``` n=int(input()) l=[0,0,0,0]#non,~a,b~,b~a ans=0 for i in range(n): s=input() ans+=s.count("AB") l[2*int(s[0]=="B")+1*int(s[-1]=="A")]+=1 if l[-1]: ans+=l[-1]-1 if l[1]: l[1]-=1 ans+=1 if l[2]: l[2]-=1 ans+=1 print(ans+min(l[1:3])) ```
output
1
96,781
16
193,563
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,782
16
193,564
"Correct Solution: ``` n=int(input()) s=[] ans=0 a=0 b=0 ab=0 for i in range(n): x=input() s.append(x) for j in range(len(x)-1): if x[j]=="A" and x[j+1]=="B": ans+=1 if x[0]=="B" and x[-1]=="A": ab+=1 elif x[0]=="B": b+=1 elif x[-1]=="A": a+=1 if a==b==0: print(ans+max(ab-1,0)) else: print(ans+max(0,ab+min(a,b))) ```
output
1
96,782
16
193,565
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,783
16
193,566
"Correct Solution: ``` N = int(input()) a, b, ab, ret = 0, 0, 0, 0 for i in range(N): s = input() l = len(s) - 1 if s[0] == "B" and s[l] == "A": ab += 1 if s[0] == "B": b += 1 if s[l] == "A": a += 1 ret += s.count("AB") if a == b and a == ab and a > 0: a -= 1 ab = min(a,b) print(ret + ab) ```
output
1
96,783
16
193,567
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,784
16
193,568
"Correct Solution: ``` n = int(input()) ans = 0 ab = 0 a = 0 b = 0 for i in range(n): s = input() ans += s.count("AB") if s[0] == "B" and s[-1] == "A": ab += 1 elif s[0] == "B": b += 1 elif s[-1] == "A": a += 1 b,a = sorted([a,b]) if ab > 0: ans += ab-1 ab = 1 if a > 0: ans += ab ans += b print(ans) ```
output
1
96,784
16
193,569
Provide a correct Python 3 solution for this coding contest problem. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4
instruction
0
96,785
16
193,570
"Correct Solution: ``` X=[input() for _ in range(int(input()))] a,b,e,s=0,0,0,0 for x in X: a += x.count("AB") if x.endswith("A") and x.startswith("B"): b += 1 elif x.endswith("A"): e += 1 elif x.startswith("B"): s += 1 if b > 0: a += b-1 if e > 0: e -= 1 a += 1 if s > 0: s -= 1 a += 1 a += min(e,s) print(a) ```
output
1
96,785
16
193,571
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4 Submitted Solution: ``` n=int(input()) c=0 d=0 a=0 k=0 for i in range(n): s=input() if "AB" in s: c+=s.count("AB") if s[0]=="B" and s[len(s)-1]=="A": d+=1 if s[0]=="B" and s[len(s)-1]!="A": a+=1 if s[len(s)-1]=="A" and s[0]!="B": k+=1 if a+k==0: print(max(c+d-1,c)) elif a+k>0: print(c+min(d+a,d+k)) else: print(c+min(a,k)) ```
instruction
0
96,786
16
193,572
Yes
output
1
96,786
16
193,573
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4 Submitted Solution: ``` N =int(input()) co = {0:0,1:0,2:0,3:0} ans = 0 foo =0 for _ in range(N): i = input() foo = 0 if i[0] =="B": foo += 1 if i[-1] =="A": foo += 2 co[foo] += 1 ans += i.count("AB") ans += min(co[1],co[2]) ans += co[3] if co[1]==0 and co[2] == 0 and co[3]!= 0: ans -= 1 print(ans) ```
instruction
0
96,787
16
193,574
Yes
output
1
96,787
16
193,575
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4 Submitted Solution: ``` N = int(input()) A = [input() for _ in range(N)] n = 0 Acount, Bcount,both = 0,0,0 for a in A: n += a.count("AB") if (a[0]=="B")&(a[-1]=="A"):both+=1 elif a[0] == "B":Bcount+=1 elif a[-1] == "A":Acount+=1 # print(n,Acount,Bcount,both) n += min(Acount,Bcount) if Acount+Bcount>0:n += both elif both>0:n += both-1 print(n) ```
instruction
0
96,788
16
193,576
Yes
output
1
96,788
16
193,577
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4 Submitted Solution: ``` n = int(input()) ab = 0 bx = 0 xa = 0 bxa = 0 for ln in range(n): line = input().rstrip() ab += line.count("AB") first , last = line[0], line[-1] if (first, last) == ('B', 'A'): bxa += 1 elif first == 'B': bx += 1 elif last == 'A': xa += 1 if bx + xa == 0: bxa = max(0, bxa - 1) print(ab + bxa + min(bx, xa)) ```
instruction
0
96,789
16
193,578
Yes
output
1
96,789
16
193,579
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4 Submitted Solution: ``` n=int(input()) count=countA=countB=countAB=0 for i in range(n): s=input() if "AB" in s: count+=1 if s[-1]=="A":countA+=1 if s[0]=="B":countB+=1 if s[-1]=="A" and s[0]=="B":countAB+=1 print(count+min(countA,countB) if countAB!=0 else count+min(countA,countB)-countAB) ```
instruction
0
96,790
16
193,580
No
output
1
96,790
16
193,581
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4 Submitted Solution: ``` n = int(input()) cnt_a = 0 # Bで始まらないがAで終わる cnt_b = 0 # Bで始まるがAで終わらない cnt_ba = 0 # Bで始まりAで終わる ans = 0 for _ in range(n): s = input() ans += s.count('AB') if s.endswith('A') and s.startswith('B'): cnt_ba += 1 else: if s.endswith('A'): cnt_a += 1 if s.startswith('B'): cnt_b += 1 if cnt_a == 0: ans += cnt_ba - 1 if cnt_b > 0: ans += 1 else: ans += cnt_ba cnt_a -= 1 if cnt_b > 0: ans += 1 cnt_b -= 1 # 残ったxxAとBxxを組み合わせてABを作る ans += min(cnt_a, cnt_b) print(ans) ```
instruction
0
96,792
16
193,584
No
output
1
96,792
16
193,585
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. Input Input is given from Standard Input in the following format: N s_1 \vdots s_N Output Print the answer. Examples Input 3 ABCA XBAZ BAD Output 2 Input 9 BEWPVCRWH ZZNQYIJX BAVREA PA HJMYITEOX BCJHMRMNK BP QVFABZ PRGKSPUNA Output 4 Input 7 RABYBBE JOZ BMHQUVA BPA ISU MCMABAOBHZ SZMEHMA Output 4 Submitted Solution: ``` #Rs,Gs,Bs,Ns= input().split() #R=int(Rs) #G=int(Gs) #B=int(Bs) #N=int(Ns) #ans=0 #sub_ans=0 #while(sub_ans>=N): ans=0 f_a=0 b_b=0 a_b=0 str_list=[] N= int(input()) for i in range(N): str=input() ans=ans+str.count("AB") if(str[-1]=="A"): f_a=f_a+1 if(str[0]=="B"): b_b=b_b+1 if(str[-1]=="A"and str[0]=="B"): a_b=a_b+1 if(f_a==N and b_b==N): ans=ans+N-1 elif((f_a==a_b)and(b_b==a_b)): ans=ans+f_a-1 elif(f_a< b_b): ans=ans+f_a else: ans=ans+b_b #print(f_a) #print(b_b) print(ans) ```
instruction
0
96,793
16
193,586
No
output
1
96,793
16
193,587
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,814
16
193,628
"Correct Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10**7) from collections import deque MOD = 10 ** 9 + 7 N = int(input()) graph = [[] for _ in range(N+1)] for i,x in enumerate(input().rstrip().split(),1): x = int(x) graph[i].append(x) graph[x].append(i) # 各深さから来ている (0個、1個、2個以上) の分布を 確率 mod MODで持つ。 half = (MOD + 1) // 2 def merge(dp,dp1): L = len(dp1) for i in range(L): # 0個,1個,2個以上 a,b,c = dp[i] d,e,f = dp1[i] a,b,c = a*d, a*e + b*d, a*f + b*e + b*f + c*d + c*e + c*f a %= MOD b %= MOD c %= MOD dp[i] = (a,b,c) return def dfs(v,parent = None): dp = None L = 0 for u in graph[v]: if u == parent: continue dp1 = dfs(u,v) if dp is None: dp = dp1 else: if len(dp) < len(dp1): dp,dp1 = dp1,dp # 2個以上が入っているインデックス if L < len(dp1): L = len(dp1) merge(dp,dp1) if dp is None: dp = deque() else: # 2個以上あるときに、0個化する for i in range(L): a,b,c = dp[i] dp[i] = (a+c,b,0) dp.appendleft((half,half,0)) return dp dp = dfs(0) answer = sum(b for a,b,c in dp) answer *= pow(2,N+1,MOD) answer %= MOD print(answer) ```
output
1
96,814
16
193,629
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,815
16
193,630
"Correct Solution: ``` # coding: utf-8 # Your code here! import sys read = sys.stdin.read readline = sys.stdin.readline n, = map(int, readline().split()) p = [-1] + [*map(int, readline().split())] MOD = 10**9+7 child = [[] for i in range(n+1)] tot = [None for i in range(n+1)] one = [None for i in range(n+1)] dep = [0]*(n+1) p2 = [1]*(n+1) for i in range(n): p2[i+1] = p2[i]*2%MOD for v in range(n,-1,-1): if dep[v]==0: tot[v] = [] one[v] = [] else: child[v].sort(key=lambda i: dep[i]) one[v] = one[child[v][-1]] tot[v] = tot[child[v][-1]] #one_sum = [0]*(dep[v]) #zero_sum = [0]*(dep[v]) child[v].pop() if child[v]: zero = [p2[tot[v][j]]-one[v][j] for j in range(-len(one[child[v][-1]]),0)] for c in child[v]: for j in range(-len(one[c]),0): z = p2[tot[c][j]]-one[c][j] one[v][j] = (one[v][j]*z+zero[j]*one[c][j])%MOD zero[j] = zero[j]*z%MOD tot[v][j] += tot[c][j] tot[v].append(1) one[v].append(1) child[p[v]].append(v) dep[p[v]] = max(dep[p[v]],dep[v]+1) #print(v,tot[v],one[v]) #print("tot",tot[0]) #print("one",one[0]) ans = 0 for i,j in zip(tot[0],one[0]): ans += pow(2,n+1-i,MOD)*j%MOD print(ans%MOD) #print(sum(tot[0])) ```
output
1
96,815
16
193,631
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,816
16
193,632
"Correct Solution: ``` from collections import deque def get_pow(): cache = {} def func(x): if x not in cache: cache[x] = pow(2, x, mod) return cache[x] return func mod = 1000000007 n = int(input()) parents = list(map(int, input().split())) children = [set() for _ in range(n + 1)] for c, p in enumerate(parents): children[p].add(c + 1) levels = [{0}] while True: level = set() for p in levels[-1]: level.update(children[p]) if not level: break levels.append(level) levels.reverse() level_node_count = [] balls = [None] * (n + 1) for i, level in enumerate(levels): level_node_count.append(len(level)) for node in level: cn = children[node] if cn: if len(cn) == 1: bs = balls[cn.pop()] bs.appendleft([1, 1, 0]) balls[node] = bs continue balls_from_children = [balls[c] for c in children[node]] balls_from_children.sort(key=len) bs1 = balls_from_children[0] for bs2 in balls_from_children[1:]: for (b10, b11, b12), b2 in zip(bs1, bs2): b2[2] = ((b11 + b12) * b2[1] + b12 * b2[0]) % mod b2[1] = (b10 * b2[1] + b11 * b2[0]) % mod b2[0] = b2[0] * b10 % mod bs1 = bs2 lim = len(balls_from_children[-2]) for i, b in enumerate(bs1): if i >= lim: break b[0] = (b[0] + b[2]) % mod b[2] = 0 bs1.appendleft([1, 1, 0]) balls[node] = bs1 else: balls[node] = deque([[1, 1, 0]]) level_node_count.reverse() pow2 = get_pow() print(sum(b[1] * pow2(n - l + 1) % mod for l, b in zip(level_node_count, balls[0])) % mod) ```
output
1
96,816
16
193,633
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,817
16
193,634
"Correct Solution: ``` # seishin.py from collections import deque N = int(input()) *P, = map(int, input().split()) MOD = 10**9 + 7 G = [[] for i in range(N+1)] U = [0]*(N+1) C = [0]*(N+1) for i, p in enumerate(P): G[p].append(i+1) U[i+1] = u = U[p]+1 C[u] += 1 Q = [None]*(N+1) PP = {} def pp(k): if k not in PP: PP[k] = p = pow(2, k, MOD) return p return PP[k] L = [0]*(N+1) ept = [] sz = L.__getitem__ for i in range(N, -1, -1): g = G[i] if not g: continue # 子ノードのdequeを集める g.sort(key=sz, reverse=1) k = len(g) e = [pp(k) - k, k, 0] g0 = g[0] L[i] = L[g0] + 1 if L[g0] == 0: Q[i] = deque([e]) continue Q[i] = R = Q[g0] if k > 1: # a0 <- a2 for s, r in zip(Q[g[1]] or ept, R): r[0] += r[2]; r[2] = 0 for j in g[1:]: S = Q[j] if not S: break # dequeの小さい方から大きい方へマージする処理 for (a0, a1, a2), r in zip(S, R): b0, b1, b2 = r; a0 += a2 r[0] = a0*b0 % MOD r[1] = (a0*b1 + a1*b0) % MOD r[2] = ((a0+a1)*b2 + a1*b1) % MOD R.appendleft(e) print((pp(N) + sum(pp(N+1-c) * a1 % MOD for (a0, a1, a2), c in zip(Q[0], C[1:]))) % MOD) ```
output
1
96,817
16
193,635
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,818
16
193,636
"Correct Solution: ``` import sys sys.setrecursionlimit(10 ** 6) input = sys.stdin.readline int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def main(): def dfs(u=0): def merge(dpu, dpv): vn = len(dpv) for d in range(-1, -1 - vn, -1): u0, u1, u2 = dpu[d] v0, v1, v2 = dpv[d] n0 = (u0 * v0) % md n1 = (u0 * v1 + u1 * v0) % md n2 = (u2 * (v0 + v1 + v2) + v2 * (u0 + u1) + u1 * v1) % md dpu[d] = (n0, n1, n2) # 葉の場合 if len(to[u]) == 0: return [(inv2, inv2, 0)] # すべての子をマージ dpu = [] mxlen=0 for v in to[u]: dpv = dfs(v) #深さが2段以上あったらu2をu0に if not dpu: dpu = dpv else: if len(dpu) < len(dpv): dpu, dpv = dpv, dpu mxlen=max(mxlen,len(dpv)) merge(dpu, dpv) for d in range(-1,-1-mxlen,-1): u0,u1,u2=dpu[d] dpu[d] = (u0 + u2, u1, 0) dpu.append((inv2, inv2, 0)) return dpu md = 10 ** 9 + 7 # 1/2のmod inv2 = pow(2, md - 2, md) n = int(input()) to = [[] for _ in range(n+1)] pp = list(map(int, input().split())) for i, p in enumerate(pp, 1): to[p].append(i) # print(to) dp0 = dfs() # print(dp0) ans = sum(u1 for _, u1, _ in dp0) print((ans * pow(2, n + 1, md)) % md) main() ```
output
1
96,818
16
193,637
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,819
16
193,638
"Correct Solution: ``` from collections import deque def get_pow(): cache = {} def func(x): if x not in cache: cache[x] = pow(2, x, mod) return cache[x] return func mod = 1000000007 n = int(input()) parents = list(map(int, input().split())) children = [set() for _ in range(n + 1)] for c, p in enumerate(parents): children[p].add(c + 1) levels = [{0}] while True: level = set() for p in levels[-1]: level.update(children[p]) if not level: break levels.append(level) levels.reverse() level_node_count = [] balls = [None] * (n + 1) for i, level in enumerate(levels): level_node_count.append(len(level)) for node in level: cn = children[node] if cn: if len(cn) == 1: bs = balls[cn.pop()] bs.appendleft([1, 1, 0]) balls[node] = bs continue balls_from_children = [balls[c] for c in children[node]] balls_from_children.sort(key=len) bs1 = balls_from_children[0] for bs2 in balls_from_children[1:]: for (b10, b11, b12), b2 in zip(bs1, bs2): b2[2] = ((b11 + b12) * b2[1] + b12 * b2[0]) % mod b2[1] = (b10 * b2[1] + b11 * b2[0]) % mod b2[0] = b2[0] * b10 % mod bs1 = bs2 for b in bs1: b[0] = (b[0] + b[2]) % mod b[2] = 0 bs1.appendleft([1, 1, 0]) balls[node] = bs1 else: balls[node] = deque([[1, 1, 0]]) level_node_count.reverse() pow2 = get_pow() print(sum(b[1] * pow2(n - l + 1) % mod for l, b in zip(level_node_count, balls[0])) % mod) ```
output
1
96,819
16
193,639
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,820
16
193,640
"Correct Solution: ``` """ https://atcoder.jp/contests/arc086/tasks/arc086_c 対消滅する可能性があるのは同じ深さの点に置かれたビー玉だけ →1つも残らない or 1つだけ残るである すなわち、ある深さに関して、そのうち1つ残るのがいくつあるかを数えればよい 1つだけ置く場合→絶対のこる 2つ置く場合→絶対消える 3つ置く場合→3つのLCAが等しくなければ残る 木dpみたいにする? dp[0] = 一つも含んでいない場合の通り数 dp[1] = 1つ含んでいる場合の通り数 dp[0] = すべての場合 - dp[1] dp[1] = 1つだけ1を持っている場合 で、子から親に伝播させていく…? マージテクで計算量削減か? 3sだからそうっぽいな… 3つ以上のマージ書くのだるすぎん? またはマージの順番をどっかにメモっておく maxd-d = indexでやる 最長のやつにマージする dp[maxd-d][1] = 1つだけ元深さdが残っている場合の数 c = 子の数 計算量は? サイズは高々1しか増えないので可能っぽい 2倍処理がまずい もっと簡潔に? 単一のdだけで考えよう 2倍処理なんてしない 最後に各dに関してかければいい →するとマージの際に2番目の大きさだけでなんとかなる 必要のないマージをしない 両方に関係ないdは操作しない むしろばらつきは深い部分にだけ存在するか 浅い部分は共通。よって-xで管理すればいいか """ import sys mod = 10**9 + 7 sys.setrecursionlimit(200000) from collections import deque def NC_Dij(lis,start): ret = [float("inf")] * len(lis) ret[start] = 0 q = deque([start]) plis = [i for i in range(len(lis))] while len(q) > 0: now = q.popleft() for nex in lis[now]: if ret[nex] > ret[now] + 1: ret[nex] = ret[now] + 1 plis[nex] = now q.append(nex) return ret,plis def inverse(a): #aのmodを法にした逆元を返す return pow(a,mod-2,mod) def dfs(v): if len(lis[v]) == 0: ret = [ [1,1] ] return ret else: retlis = [] for nex in lis[v]: nret = dfs(nex) retlis.append( [len(nret),nret] ) retlis.sort() #1つしかない場合マージしない if len(retlis) == 1: retlis[-1][1].append([1,1]) return retlis[-1][1] #2つ以上の場合最大のやつにマージする for revd in range(retlis[-2][0]): zmul = 1 amul = 1 for i in range(len(retlis)-1,-1,-1): if revd < retlis[i][0]: zmul *= retlis[i][1][-1-revd][0] amul *= sum(retlis[i][1][-1-revd]) zmul %= mod amul %= mod else: break nsum = 0 for i in range(len(retlis)-1,-1,-1): if revd < retlis[i][0]: nsum += zmul * inverse(retlis[i][1][-1-revd][0]) * retlis[i][1][-1-revd][1] nsum %= mod else: break retlis[-1][1][-1-revd][1] = nsum retlis[-1][1][-1-revd][0] = (amul-nsum) % mod retlis[-1][1].append([1,1]) return retlis[-1][1] N = int(input()) p = list(map(int,input().split())) lis = [ [] for i in range(N+1) ] for i in range(N): #lis[i+1].append(p[i]) lis[p[i]].append(i+1) dlis,plis = NC_Dij(lis,0) maxd = max(dlis) dn = [0] * (maxd+1) for i in dlis: dn[i] += 1 ans = dfs(0) #print (dn,ans) A = 0 for i in range(maxd+1): A += ans[-1-i][1] * pow(2,N+1-dn[i],mod) A %= mod print (A) ```
output
1
96,820
16
193,641
Provide a correct Python 3 solution for this coding contest problem. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550
instruction
0
96,821
16
193,642
"Correct Solution: ``` n, = map(int, input().split()) p = [-1] + [*map(int, input().split())] MOD = 10**9+7 dp = [[] for _ in range(n+1)] dep = [0]*(n+1) nxt = [0]*(n+1) for v in range(n,0,-1): _,nxt[p[v]],dep[p[v]] = sorted([nxt[p[v]],dep[p[v]],dep[v]+1]) tot = [0]*(dep[0]+1) for i in range(n+1): tot[dep[i]] += 1 def merge(p,v): if len(dp[p]) < len(dp[v]): dp[p],dp[v]=dp[v],dp[p] for i in range(-len(dp[v]),0): a,b,c = dp[p][i] d,e,f = dp[v][i] dp[p][i][:] = [a*d%MOD,(b*d+a*e)%MOD,c*f%MOD] for v in range(n,-1,-1): dp[v].append([1,1,2]) for i in range(-nxt[v]-1,0): dp[v][i][0] = dp[v][i][2] - dp[v][i][1] if v: merge(p[v],v) ans = 0 for d in dp[0]: ans += pow(d[2],MOD-2,MOD)*d[1]%MOD print(ans*pow(2,n+1,MOD)%MOD) ```
output
1
96,821
16
193,643
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550 Submitted Solution: ``` # seishin.py N = int(input()) *P, = map(int, input().split()) MOD = 10**9 + 7 G = [[] for i in range(N+1)] for i, p in enumerate(P): G[p].append(i+1) Q = [None]*(N+1) for i in range(N, -1, -1): # 子ノードのqueを集める R = None for j in G[i]: S = Q[j] if not R: R = S continue # |R| > |S| if len(R) < len(S): R, S = S, R # queの小さい方から大きい方へマージする処理 for s, r in zip(S, R): a0, a1, a2, c0 = s b0, b1, b2, c1 = r r[:] = a0*b0 % MOD, (a0*b1 + a1*b0) % MOD, (a2*(b0 + b1 + b2) + (a0 + a1)*b2 + a1*b1) % MOD, c0+c1 if R: # a0 <- a2 for e in R: e[0] += e[2] e[2] = 0 Q[i] = [[1, 1, 0, 1]] + R else: Q[i] = [[1, 1, 0, 1]] ans = 0 for a0, a1, a2, c in Q[0]: ans += pow(2, N+1-c, MOD) * a1 % MOD print(ans % MOD) ```
instruction
0
96,822
16
193,644
No
output
1
96,822
16
193,645
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550 Submitted Solution: ``` # seishin.py import sys sys.setrecursionlimit(10**6) N = int(input()) *P, = map(int, input().split()) MOD = 10**9 + 7 from itertools import tee G = [[] for i in range(N+1)] for i, p in enumerate(P): G[p].append(i+1) Q = [None]*(N+1) E = [1, 1, 0, 1] for i in range(N, -1, -1): def gen(i): *I, = map(Q.__getitem__, G[i]) r = E while r: a,b,c,d=r yield (a+c,b,0,d) r = None for it in I: s = next(it, None) if s: if r: a0, a1, a2, c0 = s b0, b1, b2, c1 = r r = a0*b0 % MOD, (a0*b1 + a1*b0) % MOD, ((a0 + a1)*b2 + a1*b1) % MOD, c0+c1 else: r = s Q[i] = gen(i) ans = 0 for a0, a1, a2, c in Q[0]: ans += pow(2, N+1-c, MOD) * a1 % MOD print(ans % MOD) ```
instruction
0
96,823
16
193,646
No
output
1
96,823
16
193,647
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550 Submitted Solution: ``` # seishin.py from collections import deque N = int(input()) *P, = map(int, input().split()) MOD = 10**9 + 7 G = [[] for i in range(N+1)] for i, p in enumerate(P): G[p].append(i+1) Q = [None]*(N+1) for i in range(N, -1, -1): # i+1 <- P[i] Q[i] = q = deque([[1, 1, 0, 1]]) # 子ノードのdequeを集める R = [] for j in G[i]: S = Q[j] if not R: R = S continue # |R| > |S| if len(R) < len(S): R, S = S, R # dequeの小さい方から大きい方へマージする処理 for k in range(len(S)): a0, a1, a2, c0 = S.popleft() b0, b1, b2, c1 = R[k] R[k] = [a0*b0 % MOD, (a0*b1 + a1*b0) % MOD, (a0*b2 + a1*b1 + a2*b0 + a1*b2 + a2*b2 + a2*b1) % MOD, c0+c1] # a0 <- a2 for e in R: e[0] += e[2] e[2] = 0 q.extend(R) ans = 0 for a0, a1, a2, c in Q[0]: ans += pow(2, N+1-c, MOD) * a1 % MOD print(ans % MOD) ```
instruction
0
96,824
16
193,648
No
output
1
96,824
16
193,649
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one marble on some of the vertices, then proceeds as follows: 1. If there is a marble on Vertex 0, move the marble into the box. 2. Move each marble from the vertex to its parent (all at once). 3. For each vertex occupied by two or more marbles, remove all the marbles from the vertex. 4. If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play. There are 2^{N+1} ways to place marbles on some of the vertices. For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007. Constraints * 1 \leq N < 2 \times 10^{5} * 0 \leq p_i < i Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_{N} Output Print the answer. Examples Input 2 0 0 Output 8 Input 5 0 1 1 0 4 Output 96 Input 31 0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23 Output 730395550 Submitted Solution: ``` #!/usr/bin/env python3 import sys sys.setrecursionlimit(202020) def powmod(a, x, m): y = 1 while 0 < x: if x % 2 == 1: y *= a y %= m x //= 2 a = a ** 2 a %= m return y M = 10 ** 9 + 7 I2 = powmod(2, M - 2, M) def dfs(g, v): r = [I2] if len(g[v]) == 0: return r cr = [] for w in g[v]: cr.append(dfs(g, w)) cr.sort(key = lambda v: -len(v)) d_max = len(cr[0]) for j in range(d_max): pr = 1 sm = 0 nc = 0 for cri in cr: if len(cri) <= j: break c = cri[j] pr *= c pr %= M sm += powmod(c, M - 2, M) sm %= M nc += 1 pr *= sm + M - nc pr %= M pp = 1 + M - pr pp %= M r.append(pp) return r def solve(n, p): g = [[] for _ in range(n + 1)] for i in range(n): g[p[i]].append(i + 1) r = dfs(g, 0) ans = 0 P2 = powmod(2, n, M) for c in r: ans += P2 * (1 + M - c) ans %= M return ans def main(): n = input() n = int(n) p = list(map(int, input().split())) print(solve(n, p)) if __name__ == '__main__': main() ```
instruction
0
96,825
16
193,650
No
output
1
96,825
16
193,651
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,826
16
193,652
"Correct Solution: ``` N,K=map(int,input().split()) l=list(map(int,input().split())) l.sort() print(sum(l[N-K:N])) ```
output
1
96,826
16
193,653
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,827
16
193,654
"Correct Solution: ``` n,k=map(int,input().split()) l=list(map(int,input().split())) sl=sorted(l) print(sum(sl[-k:])) ```
output
1
96,827
16
193,655
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,828
16
193,656
"Correct Solution: ``` n,k=map(int,input().split());print(sum(sorted(map(int,input().split()))[:-k-1:-1])) ```
output
1
96,828
16
193,657
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,829
16
193,658
"Correct Solution: ``` n,k=map(int,input().split()) l=list(map(int,input().split())) l.sort() l=l[n-k:n] print(sum(l)) ```
output
1
96,829
16
193,659
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,830
16
193,660
"Correct Solution: ``` N,K=map(int,input().split()) L=sorted(list(map(int,input().split()))) ans=sum(L[-K:]) print(ans) ```
output
1
96,830
16
193,661
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,831
16
193,662
"Correct Solution: ``` n,k = map(int,input().split()) l = [int(i) for i in input().split()] l.sort() print(sum(l[-k:])) ```
output
1
96,831
16
193,663
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,832
16
193,664
"Correct Solution: ``` N,K = list(map(int,input().split())) L =list(map(int,input().split())) L.sort() print(sum(L[-K:])) ```
output
1
96,832
16
193,665
Provide a correct Python 3 solution for this coding contest problem. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386
instruction
0
96,833
16
193,666
"Correct Solution: ``` #067_B n,k=map(int,input().split()) l=sorted(list(map(int,input().split())))[::-1] print(sum(l[:k])) ```
output
1
96,833
16
193,667
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386 Submitted Solution: ``` n,k=map(int, input().split()) List=sorted(list(map(int, input().split()))) print(sum(List[-k:])) ```
instruction
0
96,834
16
193,668
Yes
output
1
96,834
16
193,669
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386 Submitted Solution: ``` n,k=map(int,input().split()) l=list(map(int,input().split())) print(sum(sorted(l)[-k::])) ```
instruction
0
96,835
16
193,670
Yes
output
1
96,835
16
193,671
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386 Submitted Solution: ``` n, k = map(int, input().split()) li = sorted(list(map(int, input().split()))) print(sum(li[n - k:])) ```
instruction
0
96,836
16
193,672
Yes
output
1
96,836
16
193,673
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386 Submitted Solution: ``` _, k = [int(i) for i in input().split()] print(sum(sorted(int(i) for i in input().split())[-k:])) ```
instruction
0
96,837
16
193,674
Yes
output
1
96,837
16
193,675
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386 Submitted Solution: ``` N, K = map(int, input().split()) l = list(map(int, input().split())) l.sort(reverse=True) print(l) print(sum(l[:K])) ```
instruction
0
96,838
16
193,676
No
output
1
96,838
16
193,677
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386 Submitted Solution: ``` n, k = list(map(int, input().split())) l = sorted(list(map(int, input().split())), reverse=True) print(l) ans = 0 for i in range(k): ans += l[i] print(ans) ```
instruction
0
96,839
16
193,678
No
output
1
96,839
16
193,679
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 * l_i is an integer. Input Input is given from Standard Input in the following format: N K l_1 l_2 l_3 ... l_{N} Output Print the answer. Examples Input 5 3 1 2 3 4 5 Output 12 Input 15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 Output 386 Submitted Solution: ``` N,K=map(int,input().split()) li=list(map(int,input().split())) li.sort(reverse=True) ans=0 for k in range(K): ans=ans+li[k] print(ans) ```
instruction
0
96,840
16
193,680
No
output
1
96,840
16
193,681