Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03325 (python)

Fixed submission:
n = int(raw_input())
a = map(int, raw_input().split())
number = 0

for i in xrange(n):
  while a[i] % 2 == 0:
    number += 1
    a[i] /= 2
    
print number

Buggy submission (verdict: Runtime Error):
n = int(raw_input())
a = map(int, raw_input().split())
number = 0

for i in xrange(n):
  while a[n] % 2 == 0:
    number += 1
    a[n] /= 2
    
print number
Problem p03006 (python)

Fixed submission:
N = int(input())
if N == 1:
    print(1)
    quit()
points = []
for _ in range(N):
    x, y = map(int, input().split())
    points.append((x, y))
diff = dict()
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        x1, y1 = points[i]
        x2, y2 = points[j]
        pq = x1-x2, y1-y2
        diff[pq] = diff.get(pq, 0) + 1
val, key = max((v, k) for k, v in diff.items())
print(N-val)

Buggy submission (verdict: Runtime Error):
N = int(input())
if N == 1:
    return 1
points = []
for _ in range(N):
    x, y = map(int, input().split())
    points.append((x, y))
diff = dict()
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        x1, y1 = points[i]
        x2, y2 = points[j]
        pq = x1-x2, y1-y2
        diff[pq] = diff.get(pq, 0) + 1
val, key = max((v, k) for k, v in diff.items())
print(N-val)

Buggy submission (verdict: Runtime Error):
N = int(input())
points = []
for _ in range(N):
    x, y = map(int, input().split())
    points.append((x, y))
diff = dict()
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        x1, y1 = points[i]
        x2, y2 = points[j]
        pq = x1-x2, y1-y2
        diff[pq] = diff.get(pq, 0) + 1
val, key = max((v, k) for k, v in diff.items())
print(N-val)
Problem p03008 (python)

Fixed submission:
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

dp = [0] * (N+1)
for a, b in ((a, b) for a, b in zip(A, B) if a < b):
    for w in range(a, N+1):
        dp[w] = max(dp[w], dp[w-a] + b-a)

M = max(dp) + N
dp = [0] * (M+1)
for a, b in ((a, b) for a, b in zip(A, B) if a > b):
    for w in range(b, M+1):
        dp[w] = max(dp[w], dp[w-b] + a-b)

print(max(dp) + M)

Buggy submission (verdict: Runtime Error):
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

dp = [N] * (N+1)
for w in range(N+1):
    dp[w] = max(dp[w], max((dp[w-a] + b-a for a, b in zip(A, B) if w-a >= 0), default=0))

M = max(dp)
dp = [M] * (M+1)
for w in range(M+1):
    dp[w] = max(dp[w], max((dp[w-b] + a-b for a, b in zip(A, B) if w-b >= 0), default=0))

print(max(dp))
Problem p03046 (python)

Fixed submission:
import itertools
M, K = map(int, input().split())

if K >= (1 << M):
    print(-1)
    quit()

if M == 0:
    print(0, 0)
    quit()

if M == 1:
    if K == 0:
        print(0, 0, 1, 1)
    else:
        print(-1)
    quit()


A = [i for i in range(1 << M) if i != K]
print(*(A + [K] + A[::-1] + [K]))

Buggy submission (verdict: Runtime Error):
M, K = map(int, input().split())

if M == 0:
    print(0, 0)
    quit()

if M == 1:
    if K == 0:
        print(0, 0, 1, 1)
    else:
        print(-1)
    quit()

if K >= (1 << M):
    print(-1)
    quit()


A = [i for i in range(1 << M) if i != K]
print(*A, K, *A[::-1], K)
Problem p03152 (python)

Fixed submission:
import bisect
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A.sort()
B.sort()
if len(set(A)) != N or len(set(B)) != M:
    print(0)
    quit()
if A[-1] != N * M or B[-1] != N * M:
    print(0)
    quit()
ans = 1
mod = 10 ** 9 + 7
for i in reversed(range(1, N * M + 1)):
    ai = bisect.bisect_left(A, i)
    bi = bisect.bisect_left(B, i)
    a_flag = A[ai] == i
    b_flag = B[bi] == i
    if a_flag and b_flag:
        pass
    elif a_flag:
        cand = M - bi
        ans = (ans * cand) % mod
    elif b_flag:
        cand = N - ai
        ans = (ans * cand) % mod
    else:
        cand = (N - ai) * (M - bi) - (N * M - i)
        if cand <= 0:
            print(0)
            quit()
        ans = (ans * cand) % mod
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A.sort()
B.sort()
if len(set(A)) != N or len(set(B)) != M:
    print(0)
    quit()
ans = 1
mod = 10 ** 9 + 7
for i in reversed(range(1, N * M + 1)):
    ai = bisect.bisect_left(A, i)
    bi = bisect.bisect_left(B, i)
    a_flag = A[ai] == i
    b_flag = B[bi] == i
    if a_flag and b_flag:
        pass
    elif a_flag:
        cand = M - bi
        ans = (ans * cand) % mod
    elif b_flag:
        cand = N - ai
        ans = (ans * cand) % mod
    else:
        cand = (N - ai) * (M - bi) - (N * M - i)
        if cand <= 0:
            print(0)
            quit()
        ans = (ans * cand) % mod
print(ans)
Problem p03247 (python)

Fixed submission:
N = int(input())
points = [tuple(int(x) for x in input().split()) for _ in range(N)]
if any(abs(x + y) % 2 != abs(sum(points[0])) % 2 for x, y in points):
    print(-1)
    quit()
even_flag = abs(sum(points[0])) % 2 == 0
points = [(x + y, x - y) for x, y in points]

M = 31 + even_flag
print(M)
print(*([1] if even_flag else []) + [1 << i for i in range(31)])
d_uv = {(0, 0): "L", (1, 1): "R", (1, 0): "U", (0, 1): "D"}
for u, v in points:
    ans = []
    if even_flag:
        ans.append("R")
        u, v = u - 1, v - 1
    sum_d_u = (u + (1 << 31) - 1) // 2
    sum_d_v = (v + (1 << 31) - 1) // 2
    for i in range(31):
        u_flag = sum_d_u >> i & 1
        v_flag = sum_d_v >> i & 1
        ans.append(d_uv[u_flag, v_flag])
    print(''.join(ans))

Buggy submission (verdict: Runtime Error):
N = int(input())
points = [tuple(int(x) for x in input().split()) for _ in range(N)]
if any(abs(x + y) % 2 != abs(sum(points[0])) % 2 for x, y in points):
    print(-1)
    quit()
if any(not (-10 <= x <= 10 and -10 <= y <= 10) for x, y in points):
    assert False

M = 20 + (abs(sum(points[0])) % 2)
print(M)
print(*[1] * M)
for x, y in points:
    print("LR"[x > 0] * abs(x) + "DU"[y > 0] * abs(y) + "DU" * ((M - abs(x) - abs(y)) // 2))
Problem p03482 (python)

Fixed submission:
S = input()
if all(S[0] == x for x in S):
    print(len(S))
else:
    print(min(max(i + 1, len(S) - (i + 1)) for i in range(len(S) - 1) if S[i] != S[i + 1]))

Buggy submission (verdict: Runtime Error):
S = input()
idx = 0
cnts = []
while idx < len(S):
    char = S[idx]
    cnt = 0
    while idx < len(S) and S[idx] == char:
        idx += 1
        cnt += 1
    cnts.append(cnt)
if len(cnts) <= 2:
    print(max(cnts))
    quit()
ans = (len(S) + 1) // 2
for i in range(len(cnts) - 2):
    ans = max(ans, min(cnts[i] + cnts[i + 1], cnts[i + 1] + cnts[i + 2]))
ans = max(ans, max(cnts))

flips = []
for i in range(len(cnts) - 1):
    flips.append((cnts[i] + cnts[i + 1], i))
flips.sort()

from bisect import bisect_left
left, right = (len(S) + 1) // 2, len(S)
while left + 1 < right:
    mid = (left + right) // 2
    _, mid_idx = flips[bisect_left(flips, (mid, -1))]
    ret = [False] * (len(cnts))
    for _, idx in flips[mid_idx:]:
        ret[idx] = ret[idx + 1] = True
    if all(x for x in ret):
        left = mid
    else:
        right = mid
ans = max(left, ans)
print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
idx = 0
cnts = []
while idx < len(S):
    char = S[idx]
    cnt = 0
    while idx < len(S) and S[idx] == char:
        idx += 1
        cnt += 1
    cnts.append(cnt)
if len(cnts) <= 2:
    print(max(cnts))
    quit()
ans = (len(S) + 1) // 2
for i in range(len(cnts) - 2):
    ans = max(ans, min(cnts[i] + cnts[i + 1], cnts[i + 1] + cnts[i + 2]))
ans = max(ans, max(cnts))

flips = []
for i in range(len(cnts) - 1):
    flips.append((cnts[i] + cnts[i + 1], i))
flips.sort()

from bisect import bisect_left
left, right = (len(S) + 1) // 2, len(S)
while left + 1 < right:
    mid = (left + right) // 2
    _, mid_idx = bisect_left(flips, (mid, -1))
    ret = [False] * (len(cnts))
    for _, idx in flips[mid_idx:]:
        ret[idx] = ret[idx + 1] = True
    if all(x for x in ret):
        left = mid
    else:
        right = mid
ans = max(left, ans)
print(ans)
Problem p03491 (python)

Fixed submission:
import sys
sys.setrecursionlimit(2 * 10 ** 5)


class TrieNode:
    def __init__(self, char):
        self.char = char
        self.nextnode = dict()
        self.is_indict = False


class Trie:
    def __init__(self, charset):
        self.charset = charset
        self.root = TrieNode('')

    def add(self, a_str):
        node = self.root
        for i, char in enumerate(a_str):
            if char not in node.nextnode:
                node.nextnode[char] = TrieNode(char)
            node = node.nextnode[char]
            if i == len(a_str) - 1:
                node.is_indict = True

    def dfs(self, node, dep):
        ret, cnt = 0, 0
        if node.is_indict:
            return 0
        for s in '01':
            if s not in node.nextnode:
                cnt += 1
            else:
                ret ^= self.dfs(node.nextnode[s], dep + 1)
        height = L - dep
        if cnt % 2:
            power2 = 0
            while height > 0 and height % 2 == 0:
                power2 += 1
                height //= 2
            ret ^= 2 ** power2
        return ret

    def debug_output(self, node, now):
        print(node.char, list(node.nextnode.items()), node.is_indict, now)
        if node.is_indict:
            print(now)
        for n in node.nextnode.values():
            self.debug_output(n, now + n.char)

N, L = map(int, input().split())
T = Trie('01')
for _ in range(N):
    T.add(input())
# T.debug_output(T.root, '')
print("Alice" if T.dfs(T.root, 0) else "Bob")

Buggy submission (verdict: Runtime Error):
class TrieNode:
    def __init__(self, char):
        self.char = char
        self.nextnode = dict()
        self.is_indict = False


class Trie:
    def __init__(self, charset):
        self.charset = charset
        self.root = TrieNode('')

    def add(self, a_str):
        node = self.root
        for i, char in enumerate(a_str):
            if char not in node.nextnode:
                node.nextnode[char] = TrieNode(char)
            node = node.nextnode[char]
            if i == len(a_str) - 1:
                node.is_indict = True

    def dfs(self, node, dep):
        ret, cnt = 0, 0
        for s in '01':
            if s not in node.nextnode:
                cnt += 1
            else:
                ret ^= self.dfs(node.nextnode[s], dep + 1)
        dep += 1
        if cnt % 2:
            power2 = 0
            while dep > 0 and dep % 2 == 0:
                power2 += 1
                dep //= 2
            ret ^= 2 ** power2
        return ret

    def debug_output(self, node, now):
        print(node.char, list(node.nextnode.items()), node.is_indict, now)
        if node.is_indict:
            print(now)
        for n in node.nextnode.values():
            self.debug_output(n, now + n.char)

N, L = map(int, input().split())
T = Trie('01')
for _ in range(N):
    T.add(input())
# T.debug_output(T.root, '')
print("Alice" if T.dfs(T.root, 0) else "Bob")
Problem p03569 (python)

Fixed submission:
S = input()
N = len(S)
non_x = []
s_notx = []
for i, s in enumerate(S):
    if s != 'x':
        non_x.append(i)
        s_notx.append(s)
s_notx = ''.join(s_notx)
if s_notx != s_notx[::-1]:
    print(-1)
    quit()
if not non_x:
    print(0)
    quit()
ans = 0
L = len(non_x)
if L % 2 == 0:
    left, right = L // 2 - 1, L // 2
else:
    left = right = L // 2
left, right = left - 1, right + 1
for i in range(left + 1):
    l, r = left - i, right + i
    ans += abs((non_x[l + 1] - non_x[l]) - (non_x[r] - non_x[r - 1]))
ans += abs(non_x[0] - (N - non_x[-1] - 1))
print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
N = len(S)
non_x = []
s_notx = []
for i, s in enumerate(S):
    if s != 'x':
        non_x.append(i)
        s_notx.append(s)
s_notx = ''.join(s_notx)
if s_notx != s_notx[::-1]:
    print(-1)
    quit()

ans = 0
L = len(non_x)
if L % 2 == 0:
    left, right = L // 2 - 1, L // 2
else:
    left = right = L // 2
left, right = left - 1, right + 1
for i in range(left + 1):
    l, r = left - i, right + i
    ans += abs((non_x[l + 1] - non_x[l]) - (non_x[r] - non_x[r - 1]))
ans += abs(non_x[0] - (N - non_x[-1] - 1))
print(ans)
Problem p03584 (python)

Fixed submission:
N, K = map(int, input().split())
A = [tuple(int(x) for x in input().split()) for _ in range(N)]
A = [(a, b) for a, b in A if a <= K]
L = K.bit_length()
ans = sum(b for a, b in A if (a | K) <= K)
for i in reversed(range(L)):
    bit = (1 << i) & K
    if bit != 1:
        cand = 0
        bor = 0
        tmp_K = K & ~(1 << i) | ((1 << i) - 1)
        ans = max(ans, sum(b for a, b in A if (a | tmp_K) <= K))
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [tuple(int(x) for x in input().split()) for _ in range(N)]
A = [(a, b) for a, b in A if a <= K]
# print(A)
L = K.bit_length()
ans = 0
for i in reversed(range(L)):
    bit = (1 << i) & K
    if bit != 1:
        # print(bit, ':', i)
        cand = 0
        bor = 0
        for a, b in A:
            if ((a >> i) & 1) == 0:
                # print(a, (a >> i) & 1)
                cand += b
                bor ^= a
        ans = max(ans, cand)
        assert(bor <= K)
print(ans)
Problem p04026 (python)

Fixed submission:
import string
S = input()
N = len(S)

for i in range(N):
    if i < N-1 and S[i] == S[i+1]:
        print(i+1, i+2)
        quit()
    elif i < N-2 and S[i] == S[i+2]:
        print(i+1, i+3)
        quit()
print(-1, -1)

Buggy submission (verdict: Runtime Error):
import string
S = input()
N = len(S)
assert(len(S) <= 100)

for i in range(N):
    for j in range(i, N):
        s = S[i:j+1]
        if len(s) > 1 and any(s.count(a) * 2 > j-i+1 for a in string.ascii_lowercase):
            print(i+1, j+1)
            quit(0)
print(-1, -1)
Problem p04048 (python)

Fixed submission:
from fractions import gcd
N, X = map(int, input().split())
print(3 * (N - gcd(N, X)))

Buggy submission (verdict: Runtime Error):
from math import gcd
N, X = map(int, input().split())
print(3 * (N - gcd(N, X)))
Problem p02642 (python)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
data = [0 for _ in range(1000001)]
for x in a:
    data[x] += 1

ans = 0
for i in range(1,1000001):
    if not data[i]:continue
    if data[i] == 1:ans += 1
    j = i
    while j < 1000001:
        data[j] = 0
        j += i

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
a.sort()
data = [0 for _ in range(200001)]
for x in a:
    data[x] += 1

ans = 0
for i in range(1,200001):
    if not data[i]:continue
    if data[i] == 1:ans += 1
    j = i
    while j < 200001:
        data[j] = 0
        j += i

print(ans)
Problem p02684 (python)

Fixed submission:
from collections import defaultdict
n,k = map(int, input().split())
a = list(map(lambda z:int(z)-1, input().split()))
if k <= n:
    now = 0
    for i in range(k):
        now = a[now]
    print(now+1)
    exit()
root = [0]
use = [-1 for _ in range(n)]
use[0] = 0
now = 0
l = -1
for i in range(1,n+1):
    now = a[now]
    if use[now] == -1:
        use[now] = i
    else:
        k -= i
        l = i - use[now]
        break
k %= l
for i in range(k):
    now = a[now]
print(now+1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
n,k = map(int, input().split())
a = list(map(lambda z:int(z)-1, input().split()))
if k <= n:
    now = 0
    for i in range(k):
        now = a[now]
    print(now+1)
    exit()
root = [0]
use = defaultdict(int)
now = 0
use[0] = 1
for i in range(n):
    now = a[now]
    if use[now] == 0:
        use[now] += i
        root.append(now)
    else:
        k -= use[now]
        root = root[use[now]:]
        break

k %= len(root)
print(root[k-1]+1)
Problem p02727 (python)

Fixed submission:
x,y,a,b,c = map(int, input().split())
p = sorted(list(map(int, input().split())),reverse=True)[:x]
q = sorted(list(map(int, input().split())),reverse=True)[:y]
pq = sorted(p + q,reverse=True)

r = sorted(list(map(int, input().split())),reverse=True)
ans = 0
data_pq = [0]
for s in pq:
    data_pq.append(data_pq[-1] + s)

data_r = [0]
for t in r:
    data_r.append(data_r[-1] + t)


for i in range(c+1):
    if x + y - i < 0:break
    ans = max(ans,data_r[i] + data_pq[x+y-i])

print(ans)

Buggy submission (verdict: Runtime Error):
from heapq import *
x,y,a,b,c = map(int, input().split())
p = sorted(list(map(int, input().split())),reverse=True)[:x]
q = sorted(list(map(int, input().split())),reverse=True)[:y]
pq = sorted(p + q,reverse=True)

r = sorted(list(map(int, input().split())),reverse=True)
ans = 0
data_pq = [0]
for s in pq:
    data_pq.append(data_pq[-1] + s)

data_r = [0]
for t in r:
    data_r.append(data_r[-1] + t)


for i in range(c+1):
    ans = max(ans,data_r[i] + data_pq[x+y-i])

print(ans)
Problem p02806 (python)

Fixed submission:
n = int(input())
data=[list(input().split()) for _ in range(n)]
s = input()
ans = 0
c = False
for i in range(n):
   if c:
      ans += int(data[i][1])
   if data[i][0] == s:
      c = True
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
data=[list(inout().split()) for _ in range(n)]
s = input()
ans = 0
c = False
for i in range(n):
  if c:
    ans += int(data[i][1])
  if data[i][0] == s:
    c = True
print(ans)
Problem p02822 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n = int(input())
G = [[] for _ in range(n)]
mod = 10**9 + 7

for _ in range(n-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

stack = [0]
depth = [-1]*n
depth[0] = 0
while stack:
    v = stack.pop()
    d = depth[v]
    for nv in G[v]:
        if depth[nv] >= 0:continue
        depth[nv] = depth[v] + 1
        stack.append(nv)

d_to_i = [[] for _ in range(n)]

for i in range(n):
    d_to_i[depth[i]].append(i)

data = [[] for _ in range(n)]

for d in range(n-1,-1,-1):
    for v in d_to_i[d]:
        for nv in G[v]:
            if depth[nv] < depth[v]:continue
            if data[nv]:
                data[v].append(n-data[nv][-1])
        if data[v]:
            data[v].append(n-1-sum(data[v]))
        else:
            data[v].append(n-1)

ans = 0
two = [1]
for i in range(n):
    two.append(two[-1]*2%mod)
for i in range(n):
    now = two[n-1]-1
    for x in data[i]:
        now -= two[x]-1
        now %= mod
    ans += now

#print(ans)

x = two[n]
x = pow(x,mod-2,mod)
print(ans*x%mod)

Buggy submission (verdict: Runtime Error):
mod = 10**9 + 7

def factorial(N,MOD,r=True):
    fact = [1]*(N+1)
    rfact = [1]*(N+1)
    r = 1
    for i in range(1,N+1):
        fact[i] = r = r * i % MOD
    rfact[N] = r = pow(fact[N],MOD-2,MOD)
    for i in range(N, 0, -1):
        rfact[i-1] = r = r * i % MOD
    if r:
        return fact,rfact
    else:
        return fact
fact,rfact = factorial(2*10**5,mod)
def perm(n,k): #上のfactorialと併用
    return fact[n]*rfact[n-k]%mod
def comb(n,k): #上のfactorialと併用
    return fact[n]*rfact[k]*rfact[n-k]%mod

n = int(input())
G = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)

euler = []
def dfs(pre,now):
    euler.append(now)
    for to in G[now]:
        if to == pre:
            continue
        dfs(now,to)
    euler.append(pre)

dfs(-1,0)

data = [[] for i in range(n)]
last = [-1]*n
for i,x in enumerate(euler[:-1]):
    if last[x] == -1:
        last[x] = i
        continue
    data[x].append((i-last[x])//2)
    last[x] = i

for i in range(n):
    if sum(data[i]) == n-1:
        continue
    data[i].append(n-1-sum(data[i]))

two = [1]
now = 1
for i in range(n):
    now *= 2
    now %= mod
    two.append(now)

ans = 0
for i in range(n):
    now = 1
    for x in data[i]:
        now += two[x]-1
        now %= mod
    ans += two[n-1]-now
    ans %= mod


ans *= pow(two[n],mod-2,mod)
ans %= mod

print(ans)
Problem p02918 (python)

Fixed submission:
n,k = map(int,input().split())
s = input()
if len(s) == 1:
    print(0)
    exit()

ans = 0
c = 0
for i in range(1,n):
    if s[i] == s[i-1]:
        ans += 1
    else:
        c += 1

if c == 1:
    print(n-1)
    exit()


t = -1
left = s[0]
q = -1
for i in range(1,n):
    if s[i] != left:
        t += 1
        left = s[i]
    if t == 1 and k > 0:
        k -= 1
        ans += 2
        t = -1

if t == 0 and k > 0:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
s = input()

ans = 0
c = 0
for i in range(1,n):
    if s[i] == s[i-1]:
     ans += 1
    else:
        c += 1
if c == 1:
    print(ans+1)
    exit()

t = -1
for i in range(1,n):
    now = s[i-1]
    if s[i] != now:
        t += 1
    if t == 1 and k > 0:
        k -= 1
        ans += 2
        t = -1
    #print(ans)

if k > 0 and now != s[n-1]:
    ans += 1
print(ans)
Problem p02921 (python)

Fixed submission:
s = input()
t = input()
ans = 0
for i in range(3):
    if s[i] == t[i]:
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
import time
start = time.time()
n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]
s = [0 for i in range(n)] #それぞれ何試合目か
ans = 0
need = n*(n-1)//2
while need:
    c = [0 for i in range(n)] #今日試合をするかどうか
    ans += 1
    for first in range(n):
        if c[first] == 0 and s[first] < n-1:
            second = a[first][s[first]]-1 # firstが対戦したい相手
            second_num = s[second] # #secondの対戦番号
            if c[second] == 0 and a[second][second_num]-1 == first:
                c[first] = 1
                c[second] = 1
                s[first] += 1
                s[second] += 1
                need -= 1
    if sum(c) == 0:
        print(-1)
        exit()
    now = time.time()-start
    if now > 1.83:
        print(n*(n-1)//2)
        exit()
print(ans)

Buggy submission (verdict: Runtime Error):
import time
start = time.time()
n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]
s = [0 for i in range(n)] #それぞれ何試合目か
ans = 0
need = n*(n-1)//2
while need:
    c = [0 for i in range(n)] #今日試合をするかどうか
    ans += 1
    for first in range(n):
        if c[first] == 0 and s[first] < n-1:
            second = a[first][s[first]]-1 # firstが対戦したい相手
            second_num = s[second] # #secondの対戦番号
            if c[second] == 0 and a[second][second_num]-1 == first:
                c[first] = 1
                c[second] = 1
                s[first] += 1
                s[second] += 1
                need -= 1
    if sum(c) == 0:
        print(-1)
        exit()
    now = time.time()-start
    if now > 1.81:
        print(n*(n-1)//2)
        exit()
print(ans)
Problem p02936 (python)

Fixed submission:
n,q = map(int,input().split())
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    tree[a].append(b)

ans = [0 for i in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    p -= 1
    ans[p] += x

#入力

stack = [0]
while len(stack) > 0:
    now = stack.pop()
    for i in tree[now]:
        ans[i] += ans[now]
        stack.append(i)

for i in range(n):
    print(ans[i],end=" ")
print()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.input
n,q = map(int,input().split())
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)

ans = [0 for i in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    p -= 1
    ans[p] += x

#入力

def dfs(pre,now,w):
    ans[now] += w
    for i in tree[now]:
        if i != pre:
            dfs(now,i,ans[now])

dfs(-1,0,0)

print(*ans)