Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
for i in range(m):
    a.sort(reverse=True)
    a[0] = a[0] >> 1
print(sum(a))


Fixed submission:
# -*- coding: utf-8 -*-
import heapq as hp
n, m = map(int, input().split())
a = list(map(int, input().split()))
items = []
for ai in a:
    hp.heappush(items, (-ai, ai))

for i in range(m):
    item = hp.heappop(items)
    nv = item[1] >> 1
    hp.heappush(items, (-nv, nv))
count = 0
while len(items) > 0:
    v = hp.heappop(items)
    count += v[1]
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
for i in range(m):
    a.sort(reverse=True)
    a[0] = a[0] >> 1
print(sum(a))


Fixed submission:
# -*- coding: utf-8 -*-
import heapq as hp
n, m = map(int, input().split())
a = list(map(int, input().split()))
items = []
for ai in a:
    hp.heappush(items, (-ai, ai))

for i in range(m):
    item = hp.heappop(items)
    nv = item[1] >> 1
    hp.heappush(items, (-nv, nv))
count = 0
while len(items) > 0:
    v = hp.heappop(items)
    count += v[1]
print(count)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import deque
n, k = map(int, input().split())
s = input()

unhapiness = 0
div = deque([])
conv = deque([])
left = "R"
right = "L"
cntr = 0
s = s + "L"
for si in s:
    right = si
    if (left == "R") & (right == "L"):
        if 0 < cntr < n:
            unhapiness += 2
            div.append(cntr)
        else:
            unhapiness += 1
            div.appendleft(cntr)
    if (left == "L") & (right == "R"):
        conv.appendleft(cntr)

    left = si
    cntr +=1

for i in range(k):
    p = div.pop()
    conv.pop()
    if 0 < p < n:
        unhapiness -= 2
    else:
        unhapiness -= 1

print(n-unhapiness)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
n, k = map(int, input().split())
s = input()

unhapiness = 0
div = deque([])
left = "R"
right = "L"
cntr = 0
s = s + "L"
for si in s:
    right = si
    if (left == "R") & (right == "L"):
        if 0 < cntr < n:
            unhapiness += 2
            div.append(cntr)
        else:
            unhapiness += 1
            div.appendleft(cntr)

    left = si
    cntr +=1

for i in range(k):
    p = div.pop()
    if 0 < p < n:
        unhapiness -= 2
    else:
        unhapiness -= 1
    if len(div) < 1:
        unhapiness += 1
        break
print(n-unhapiness)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
h = list(map(int, input().split()))

max_count = 0
for i in range(n):
    tmp_count = 0
    for j in range(i+1, n):
        if h[j-1] >= h[j]:
            tmp_count += 1
        else:
            break
    if max_count < tmp_count:
        max_count = tmp_count

print(max_count)


Fixed submission:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
h = list(map(int, input().split()))

max_count = 0
tmp_count = 0
for i in range(n-1):
    if h[i] >= h[i+1]:
        tmp_count += 1
    else:
        if max_count < tmp_count:
            max_count = tmp_count
        tmp_count = 0

if max_count < tmp_count:
    max_count = tmp_count

print(max_count)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n = int(input())
s = [0]*n
count = 0

for i in range(n):
    tmp = list(str(input()))
    tmp.sort()
    s[i] = "".join(tmp)

for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            count += 1

print(count)


Fixed submission:
# -*- coding: utf-8 -*-
n = int(input())
s = [0]*n
count = 0
each_count = {}

for i in range(n):
    tmp = list(str(input()))
    tmp.sort()
    s[i] = "".join(tmp)

for si in s:
    if each_count.setdefault(si, 0) == 0:
        each_count[si] = 1
    else:
        count += each_count[si]
        each_count[si] += 1

print(count)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
INF = 1000000
n = int(input())
h = list(map(int, input().split()))
dp = [INF]*n


def rec(i):
    # print("i = " + str(i))
    if dp[i] < INF:
        return dp[i]
    if i == 0:
        return 0
    res = INF
    # print("before: " + str(res))
    res = min(res, rec(i-1) + abs(h[i] - h[i-1]))
    # print("after: " + str(res))
    if i > 1:
        res = min(res, rec(i - 2) + abs(h[i] - h[i - 2]))
    dp[i] = res
    return dp[i]


print(rec(n-1))


Fixed submission:
# -*- coding: utf-8 -*-
INF = 1000000000
n = int(input())
h = list(map(int, input().split()))
dp = [INF]*n
dp[0] = 0

for i in range(1, n):
    dp[i] = min(dp[i], dp[i-1] + abs(h[i] - h[i-1]))
    if i > 1:
        dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]))

print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
INF = 1000000
n = int(input())
h = list(map(int, input().split()))
dp = [INF]*n

def chmin(a, b):
    if a > b:
        return b
    else:
        return a


def chmax(a, b):
    if a < b:
        return b
    else:
        return a


def rec(i):
    if dp[i] < INF:
        return dp[i]
    if i == 0:
        return 0
    res = INF
    res = chmin(res, rec(i-1) + abs(h[i] - h[i-1]))
    if i > 1:
        res = chmin(res, rec(i - 2) + abs(h[i] - h[i - 2]))
    dp[i] = res
    return dp[i]

print(rec(n-1))


Fixed submission:
# -*- coding: utf-8 -*-
INF = 1000000000
n = int(input())
h = list(map(int, input().split()))
dp = [INF]*n
dp[0] = 0

for i in range(1, n):
    dp[i] = min(dp[i], dp[i-1] + abs(h[i] - h[i-1]))
    if i > 1:
        dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]))

print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
INF = 10000
n = int(input())
h = list(map(int, input().split()))
dp = [INF]*n

def chmin(a, b):
    if a > b:
        return b
    else:
        return a


def chmax(a, b):
    if a < b:
        return b
    else:
        return a


def rec(i):
    if dp[i] < INF:
        return dp[i]
    if i == 0:
        return 0
    res = INF
    res = chmin(res, rec(i-1) + abs(h[i] - h[i-1]))
    if i > 1:
        res = chmin(res, rec(i - 2) + abs(h[i] - h[i - 2]))
    dp[i] = res
    return dp[i]

print(rec(n-1))


Fixed submission:
# -*- coding: utf-8 -*-
INF = 1000000000
n = int(input())
h = list(map(int, input().split()))
dp = [INF]*n
dp[0] = 0

for i in range(1, n):
    dp[i] = min(dp[i], dp[i-1] + abs(h[i] - h[i-1]))
    if i > 1:
        dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]))

print(dp[n-1])
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
# INF=10^7程度じゃインプットの桁数ふえた時に更新処理が正しく走らず結果エラーになってしまう..
INF = 1000000000
n, k = map(int,  input().split())
h = list(map(int, input().split()))
dp = [INF]*n
dp[0] = 0

for i in range(n):
    for j in range(1, k+1):
        if i + j < n:
            dp[i+j] = min(dp[i+j], dp[i] + abs(h[i]-h[i+j]))

print(dp[n-1])


Fixed submission:
# -*- coding: utf-8 -*-
# INF=10^7程度じゃインプットの桁数ふえた時に更新処理が正しく走らず結果エラーになってしまう..
INF = 1000000000
n, k = map(int,  input().split())
h = list(map(int, input().split()))
dp = [INF]*n
dp[0] = 0

for i in range(n):
    for j in range(1, k+1):
        if i + j < n:
            dp[i+j] = min(dp[i+j], dp[i] + abs(h[i]-h[i+j]))

print(dp[n-1])
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
v_max = 100010
INF = 1000000100
n, w = map(int, input().split())
weight = [0]*n
value = [0]*n
dp = [[INF for i in range(v_max+1)] for i in range(n+1)]
dp[0][0] = 0

for i in range(n):
    weight[i], value[i] = map(int, input().split())

for i in range(n):
    for j in range(v_max+1):
        if j - value[i] >= 0:
            dp[i+1][j] = min(dp[i+1][j], dp[i][j - value[i]] + weight[i])
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])

result = 0
for j in range(v_max+1):
    # print("v_sum: " + str(j) + " , weight: " + str(dp[n][j]))
    if dp[n][j] <= w:
        result = j
print(result)


Fixed submission:
# -*- coding: utf-8 -*-
v_max = 100010
INF = 1000000100
n, w = map(int, input().split())
weight = [0]*n
value = [0]*n
dp = [[INF for i in range(v_max+1)] for i in range(n+1)]
dp[0][0] = 0

for i in range(n):
    weight[i], value[i] = map(int, input().split())

for i in range(n):
    for j in range(v_max+1):
        if j - value[i] >= 0:
            dp[i+1][j] = min(dp[i+1][j], dp[i][j - value[i]] + weight[i])
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])

result = 0
for j in range(v_max+1):
    if dp[n][j] <= w:
        result = j
print(result)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
As = list(map(int, input().split()))

history = {}
cycle = []
pos = 1
isEnd = False
for i in range(2*N):
    if pos in history:
        for j in range(len(cycle)):
            if cycle[j] == pos:
                head = cycle[:j]
                cycle = cycle[j:]
                isEnd = True
                break

    if isEnd:
        break

    history[pos] = 1
    cycle.append(pos)
    pos = As[pos-1]


#print(cycle)

if K <= len(head):
    print(head[K])
else:
    mod = (K-len(head)) % len(cycle)
    print(cycle[mod])


Fixed submission:
N, K = map(int, input().split())
As = list(map(int, input().split()))

history = {}
cycle = []
pos = 1
isEnd = False
for i in range(2*N):
    if pos in history:
        for j in range(len(cycle)):
            if cycle[j] == pos:
                head = cycle[:j]
                cycle = cycle[j:]
                isEnd = True
                break

    if isEnd:
        break

    history[pos] = 1
    cycle.append(pos)
    pos = As[pos-1]


#print(cycle)

if K < len(head):
    print(head[K])
else:
    mod = (K-len(head)) % len(cycle)
    print(cycle[mod])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))

i_plus_Ai = [0 for i in range(N)]
dict = {}
for i in range(N):
    i_plus_Ai[i] = i + As[i]
    tmp = i - As[i]
    if tmp not in dict:
        dict[tmp] = [i]
    else:
        dict[tmp].append(i)
    dict[tmp].sort()

count = 0
for i in range(N):
    if i_plus_Ai[i] in dict:
        kouho = dict[i_plus_Ai[i]]
        for j in range(len(kouho)):
            if kouho[j] > i:
                count += len(kouho) - j
                break

print(count)


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

i_plus_Ai = [0 for i in range(N)]
dict = {}
for i in range(N):
    i_plus_Ai[i] = i + As[i]
    tmp = i - As[i]
    if tmp not in dict:
        dict[tmp] = [i]
    else:
        dict[tmp].append(i)

count = 0
for i in range(N):
    if i_plus_Ai[i] in dict:
        kouho = dict[i_plus_Ai[i]]
        for j in range(len(kouho)):
            if kouho[j] > i:
                count += len(kouho) - j
                break

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))

i_plus_Ai = [0 for i in range(N)]
dict = {}
for i in range(N):
    i_plus_Ai[i] = i + As[i]
    tmp = i - As[i]
    if tmp not in dict:
        dict[tmp] = [i]
    else:
        dict[tmp].append(i)
    dict[tmp].sort()

count = 0
for i in range(N):
    if i_plus_Ai[i] in dict:
        kouho = dict[i_plus_Ai[i]]
        for j in range(len(kouho)):
            if kouho[j] > i:
                count += len(kouho) - j
                break

print(count)


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

i_plus_Ai = [0 for i in range(N)]
dict = {}
for i in range(N):
    i_plus_Ai[i] = i + As[i]
    tmp = i - As[i]
    if tmp not in dict:
        dict[tmp] = [i]
    else:
        dict[tmp].append(i)

count = 0
for i in range(N):
    if i_plus_Ai[i] in dict:
        kouho = dict[i_plus_Ai[i]]
        for j in range(len(kouho)):
            if kouho[j] > i:
                count += len(kouho) - j
                break

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))

count = 0
for i in range(N):
    for j in range(i+1,N):
        if abs(i-j) == As[i] + As[j]:
            count += 1

print(count)


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

i_plus_Ai = [0 for i in range(N)]
dict = {}
for i in range(N):
    i_plus_Ai[i] = i + As[i]
    tmp = i - As[i]
    if tmp not in dict:
        dict[tmp] = [i]
    else:
        dict[tmp].append(i)

count = 0
for i in range(N):
    if i_plus_Ai[i] in dict:
        kouho = dict[i_plus_Ai[i]]
        for j in range(len(kouho)):
            if kouho[j] > i:
                count += len(kouho) - j
                break

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

counter = []

for i in range(N):
    si = input()
    if si not in counter:
        counter.append(si)

print(len(counter))


Fixed submission:
N = int(input())

counter = set([])

for i in range(N):
    si = input()
    if si not in counter:
        counter.add(si)

print(len(counter))
Buggy python submission (verdict: Time Limit Exceeded):
def get_exp_mod(base, exp, mod):
    exp_bin = str(format(exp, 'b'))  # ２進表現
    res = 1
    for i in range(len(exp_bin)):
        if exp_bin[-i - 1] == '1':
            res = res * base % mod
        base = base * base % mod  # base^2^nをmodで割ったあまり

    return res


mod = 10 ** 9 + 7
n, a, b = map(int, input().split())

## TODO:0の時

# 2^n -1のmodを求める
all_case = (get_exp_mod(2, n, mod) - 1) % mod
#print(all_case)

# nCa modを求める
case_a = 1
for i in range(a):
    case_a = case_a * (n - i) % mod
    case_a = case_a * get_exp_mod(i+1, mod-2, mod) % mod
#print(case_a)

case_b = 1
for i in range(b):
    case_b = case_b * (n - i) % mod
    case_b = case_b * get_exp_mod(i+1, mod-2, mod) % mod
#print(case_b)

print((all_case - case_a - case_b) % mod)


Fixed submission:
def get_exp_mod(base, exp, mod):
    exp_bin = str(format(exp, 'b'))  # ２進表現
    res = 1
    for i in range(len(exp_bin)):
        if exp_bin[-i - 1] == '1':
            res = res * base % mod
        base = base * base % mod  # base^2^nをmodで割ったあまり

    return res


def comb(n, r, mod):
    X = 1
    Y = 1
    for i in range(r):
        X = X * (n - i) % mod
        Y = Y * (i + 1) % mod
    return X * get_exp_mod(Y, mod - 2, mod) % mod


mod = 10 ** 9 + 7
n, a, b = map(int, input().split())


# 2^n -1のmodを求める
all_case = (get_exp_mod(2, n, mod) - 1) % mod

# nCa modを引く
print((all_case - comb(n, a, mod) - comb(n, b, mod)) % mod)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
As = list(map(int, input().split()))

count = 0
while count < M:
    As.sort(reverse=True)
    As[0] = int(As[0]/2)
    count += 1

print(sum(As))


Fixed submission:
import heapq

N, M = map(int, input().split())
As = list(map(int, input().split()))
As = list(map(lambda x: -x, As))
heapq.heapify(As)

count = 0
while count < M:
    tmp = heapq.heappop(As)
    tmp = int(tmp/2)
    heapq.heappush(As, tmp)
    count += 1

print(-sum(As))
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
As = list(map(int, input().split()))

for _ in range(M):
    As.sort()
    b, c = map(int, input().split())
    for i in range(b):
        if As[i] < c:
            As[i] = c
        else:
            break

print(sum(As))


Fixed submission:
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort(reverse=True)

BCs = []
for _ in range(M):
    b, c = map(int, input().split())
    BCs.append({'b': b, 'c': c})

BCs = sorted(BCs, key=lambda bc: bc['c'], reverse=True)
#print(BCs)

new_cards = []
for i in range(M):
    new_cards += [BCs[i]['c'] for _ in range(BCs[i]['b'])]
    if len(new_cards) >= N:
        break

if len(new_cards) < N:
    new_cards += [0 for _ in range(N-len(new_cards))]
#print(new_cards)

A_index = 0
new_index = 0
new_As = []
for i in range(N):
    if As[A_index] >= new_cards[new_index]:
        new_As.append(As[A_index])
        A_index += 1
    else:
        new_As.append(new_cards[new_index])
        new_index += 1

print(sum(new_As))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
As = list(map(int, input().split()))

for _ in range(M):
    As.sort()
    b, c = map(int, input().split())
    for i in range(b):
        if As[i] < c:
            As[i] = c

print(sum(As))


Fixed submission:
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort(reverse=True)

BCs = []
for _ in range(M):
    b, c = map(int, input().split())
    BCs.append({'b': b, 'c': c})

BCs = sorted(BCs, key=lambda bc: bc['c'], reverse=True)
#print(BCs)

new_cards = []
for i in range(M):
    new_cards += [BCs[i]['c'] for _ in range(BCs[i]['b'])]
    if len(new_cards) >= N:
        break

if len(new_cards) < N:
    new_cards += [0 for _ in range(N-len(new_cards))]
#print(new_cards)

A_index = 0
new_index = 0
new_As = []
for i in range(N):
    if As[A_index] >= new_cards[new_index]:
        new_As.append(As[A_index])
        A_index += 1
    else:
        new_As.append(new_cards[new_index])
        new_index += 1

print(sum(new_As))
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())

Ss = []
white_count = 0
for i in range(H):
    s = list(input())
    for masu in s:
        if masu == '.':
            white_count += 1
    Ss.append(s)

# print('white_count', white_count)


def search():
    global Ss_hosu
    #global min_hosu
    Ss_hosu[0][0] = 1
    que = [[0, 0]]
    while len(que) > 0:
        x, y = que.pop(0)
        my_value = Ss_hosu[y][x]
        for dx, dy in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            if 0 <= x + dx <= W - 1 and 0 <= y + dy <= H - 1:
                if Ss[y + dy][x + dx] == '.' and Ss_hosu[y + dy][x + dx] is None:
                    Ss_hosu[y+dy][x+dx] = my_value + 1
                    que.append([x + dx, y + dy])

    # Ss_hosu[y][x] = value
    # if x == W-1 and y == H-1:
    #     min_hosu = value
    # for dx, dy in [[1,0], [-1,0], [0,1], [0,-1]]:
    #     if 0 <= x+dx <= W-1 and 0 <= y+dy <= H-1:
    #         if Ss[y+dy][x+dx] == '.' and Ss_hosu[y+dy][x+dx] is None:
    #             search(x+dx, y+dy, value=value+1)


Ss_hosu = [[None for _ in range(W)] for _ in range(H)]
search()
min_hosu = Ss_hosu[H-1][W-1]

# for s in Ss_hosu:
#     print(s)

# print('min_hosu', min_hosu)

print(white_count - min_hosu)


Fixed submission:
H, W = map(int, input().split())

Ss = []
white_count = 0
for i in range(H):
    s = list(input())
    for masu in s:
        if masu == '.':
            white_count += 1
    Ss.append(s)

# print('white_count', white_count)


def search():
    global Ss_hosu
    #global min_hosu
    Ss_hosu[0][0] = 1
    que = [[0, 0]]
    while len(que) > 0:
        x, y = que.pop(0)
        my_value = Ss_hosu[y][x]
        for dx, dy in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            if 0 <= x + dx <= W - 1 and 0 <= y + dy <= H - 1:
                if Ss[y + dy][x + dx] == '.' and Ss_hosu[y + dy][x + dx] is None:
                    Ss_hosu[y+dy][x+dx] = my_value + 1
                    que.append([x + dx, y + dy])

  
Ss_hosu = [[None for _ in range(W)] for _ in range(H)]
search()
min_hosu = Ss_hosu[H-1][W-1]

# for s in Ss_hosu:
#     print(s)

# print('min_hosu', min_hosu)

if min_hosu is None:
    print(-1)
else:
    print(white_count - min_hosu)
Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(100000)

N = int(input())
Nodes = [{} for i in range(N)]


def search(index, dist):
    neighbors = Nodes[index]
    for key in neighbors:
        if distances[key] is not None:
            continue

        distances[key] = dist + neighbors[key]
        search(key, distances[key])


# 木を作る
for _ in range(N-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    Nodes[a][b] = c
    Nodes[b][a] = c

# 頂点Kから各点への距離を求める
Q, K = map(int, input().split())
distances = [None for i in range(N)]
distances[K-1] = 0
search(index=K-1, dist=0)

#print(distances)

# 解答
for _ in range(Q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(distances[x] + distances[y])


Fixed submission:
import sys

sys.setrecursionlimit(1000000)

N = int(input())
Nodes = [{} for i in range(N)]


def search(index, dist):
    neighbors = Nodes[index]
    for key in neighbors:
        if distances[key] is not None:
            continue

        distances[key] = dist + neighbors[key]
        search(key, distances[key])


# 木を作る
for _ in range(N-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    Nodes[a][b] = c
    Nodes[b][a] = c

# 頂点Kから各点への距離を求める
Q, K = map(int, input().split())
distances = [None for i in range(N)]
distances[K-1] = 0
search(index=K-1, dist=0)

#print(distances)

# 解答
for _ in range(Q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(distances[x] + distances[y])

Buggy python submission (verdict: Runtime Error):
N = int(input())
Nodes = [{} for i in range(N)]


def search(index, dist):
    neighbors = Nodes[index]
    for key in neighbors:
        if distances[key] is not None:
            continue

        distances[key] = dist + neighbors[key]
        search(key, distances[key])


# 木を作る
for _ in range(N-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    Nodes[a][b] = c
    Nodes[b][a] = c

# 頂点Kから各点への距離を求める
Q, K = map(int, input().split())
distances = [None for i in range(N)]
distances[K-1] = 0
search(index=K-1, dist=0)

#print(distances)

# 解答
for _ in range(Q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(distances[x] + distances[y])


Fixed submission:
import sys

sys.setrecursionlimit(1000000)

N = int(input())
Nodes = [{} for i in range(N)]


def search(index, dist):
    neighbors = Nodes[index]
    for key in neighbors:
        if distances[key] is not None:
            continue

        distances[key] = dist + neighbors[key]
        search(key, distances[key])


# 木を作る
for _ in range(N-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    Nodes[a][b] = c
    Nodes[b][a] = c

# 頂点Kから各点への距離を求める
Q, K = map(int, input().split())
distances = [None for i in range(N)]
distances[K-1] = 0
search(index=K-1, dist=0)

#print(distances)

# 解答
for _ in range(Q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(distances[x] + distances[y])
Buggy python submission (verdict: Runtime Error):
import sys


def start_with(s):
    if s == '':
        print('YES')
        sys.exit(0)

    try:
        if s[0:5] == 'dream':
            start_with(s[5:])
        if s[0:7] == 'dreamer':
            start_with(s[7:])
        if s[0:5] == 'erase':
            start_with(s[5:])
        if s[0:6] == 'eraser':
            start_with(s[6:])
    except IndexError:
        return


S = input()
start_with(S)
print('NO')


Fixed submission:
import sys

sys.setrecursionlimit(1000000)
S = input()


def start_with(i):
    if i == len(S):
        print('YES')
        sys.exit(0)

    try:
        if S[i:i+5] == 'dream':
            start_with(i+5)
        if S[i:i+7] == 'dreamer':
            start_with(i+7)
        if S[i:i+5] == 'erase':
            start_with(i+5)
        if S[i:i+6] == 'eraser':
            start_with(i+6)
        return
    except IndexError:
        return


start_with(0)
print('NO')