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

Fixed submission:
from heapq import heappop, heappush


n,m = map(int,input().split())
A = list(map(int,input().split()))
pq = []
ans = 0

for a in A:
    heappush(pq, [-a, 1])
    
for _ in range(m):
    b,c = map(int,input().split())
    heappush(pq, [-c, b])

for i in range(n):
    c,b = heappop(pq)
    c = -c
    ans += c
    if b > 1:
        b -= 1
        heappush(pq, [-c, b])
        
print(ans)

Buggy submission (verdict: Runtime Error):
import sys

n,m = map(int, input().split())
a = list(map(int,input().split()))
D = []

j = 0
for _ in range(m):
    b,c = map(int, input().split())
    D += [c]*b
D.sort(reverse=True)
for i in range(n):
    if a[i] < D[j]:
        a[i] = D[j]
        j += 1

print(sum(a))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n,m = map(int, input().split())
a = list(map(int,input().split()))
D = []
idx = 0
j = 0
for _ in range(m):
    b,c = map(int, input().split())
    D += [c]*b
D.sort(reverse=True)
for i in range(n):
    if a[i] < D[j]:
        a[i] < D[j]
        j += 1

print(sum(a[idx:]))
Problem p03774 (python)

Fixed submission:
n,m = map(int,input().split())
AB = [[int(i) for i in input().split()] for j in range(n)]
CD = [[int(k) for k in input().split()] for l in range(m)]

ans = [0]*n
for ab in AB:
    a,b = ab[0],ab[1]
    distance = 1000000000
    for cd in CD:
        c,d = cd[0], cd[1]
        if distance > abs(a-c) + abs(b-d):
            distance = abs(a-c) + abs(b-d)
            ans[AB.index(ab)] = CD.index(cd)+1
        
for _ in ans:
    print(_)

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
AB = [[int(i) for i in input().split()] for j in range(n)]
CD = [[int(k) for k in input().split()] for l in range(n)]

ans = [0]*n
for ab in AB:
    a,b = ab[0],ab[1]
    distance = 1000000000
    for cd in CD:
        c,d = cd[0], cd[1]
        if distance > abs(a-c) + abs(b-d):
            distance = abs(a-c) + abs(b-d)
            ans[AB.index(ab)] = CD.index(cd)+1
        
for _ in ans:
    print(_)

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
AB = [[int(i) for i in input().split()] for j in range(n)]
CD = [[int(k) for k in input().split()] for l in range(n)]

ans = [0]*n
for ab in AB:
    a,b = ab[0],ab[1]
    distance = 100000000
    for cd in CD:
        c,d = cd[0], cd[1]
        if distance > abs(a-c) + abs(b-d):
            distance = abs(a-c) + abs(b-d)
            ans[AB.index(ab)] = CD.index(cd)+1
        
for _ in ans:
    print(_)
Problem p02679 (python)

Fixed submission:
#!/usr/bin/python3
from math import gcd
import sys
input = lambda: sys.stdin.readline().strip()

n = int(input())
indet = 0
d = {}
for i in range(n):
    a, b = [int(x) for x in input().split()]
    if b < 0:
        a *= -1
        b *= -1
    if a == 0 and b == 0:
        indet += 1
        continue
    elif a == 0:
        p = (0, 1)
    elif b == 0:
        p = (-1, 0)
    else:
        g = gcd(a, b)
        p = (a // g, b // g)
    d[p] = d.get(p, 0) + 1

sizes = []
ignore = set()
for p, size in d.items():
    if p not in ignore:
        a, b = -p[1], p[0]
        if b < 0:
            a *= -1
            b *= -1
        if (a, b) in d:
            ignore.add((a, b))
            sizes.append((size, d[(a, b)]))
        else:
            sizes.append((size, 0))
M = 10**9 + 7
ans = 1
for n1, n2 in sizes:
    ans *= (pow(2, n1, M) + pow(2, n2, M) - 1)
    ans %= M
print((indet + ans - 1) % M)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/python3
from math import gcd
import sys
input = lambda: sys.stdin.readline().strip()

n = int(input())
indet = 0
d = {}
for i in range(n):
    a, b = [int(x) for x in input().split()]
    if b < 0:
        a *= -1
        b *= -1
    if a == 0 and b == 0:
        indet += 1
    elif b == 0:
        p = (0, 0)
    else:
        g = gcd(a, b)
        p = (a // g, b // g)
    d[p] = d.get(p, 0) + 1

sizes = []
ignore = set()
for p, size in d.items():
    if p not in ignore:
        a, b = -p[1], p[0]
        if b < 0:
            a *= -1
            b *= -1
        if (a, b) in d:
            ignore.add((a, b))
            sizes.append((size, d[(a, b)]))
        else:
            sizes.append((size, 0))
M = 10**9 + 7
ans = 1
for n1, n2 in sizes:
    ans *= (pow(2, n1, M) + pow(2, n2, M) - 1)
    ans %= M
print((indet + ans - 1) % M)
Problem p03036 (python)

Fixed submission:
r, d, x = [int(x) for x in input().split()]
for i in range(2001, 2011):
  x = r * x - d
  print(x)

Buggy submission (verdict: Runtime Error):
r, d, x = [int(x) for x in input().split()]
for i in range(2001, 2011):
  x = r * x - D
  print(x)
Problem p02629 (python)

Fixed submission:
n = int(input())
lis = [chr(ord('a') + i) for i in range(26)]

i=1
while True:
    if n <= 26**i:
        break

    else:
        n -= 26**i
        i += 1

ans = []

for j in range(i-1, -1, -1):
    s1 = n // (26**j)
    s2 = n % (26**j)
    if s2 == 0:
        s1 -= 1
    if j > 0:
        if s1 >= 26:
            s1 = 25
            ans.append(lis[-1])
        else:
            ans.append(lis[s1])
        n -= (26**j) * s1
    elif j == 0:
        ans.append(lis[n-1])

print(''.join(ans))

Buggy submission (verdict: Runtime Error):
n = int(input())
lis = [chr(ord('a') + i) for i in range(26)]

m = n
i=1
while True:
    if m <= 26**i:
        break

    else:
        m -= 26**i
        i += 1

ans = []

for j in range(i-1, -1, -1):
    s1 = n // (26**j)
    if s1 == 0:
        ans.append(lis[s1])
    else:
        ans.append(lis[s1-1])
    n -= (26**j) * s1

print(''.join(ans))

Buggy submission (verdict: Runtime Error):
n = int(input())
lis = [chr(ord('a') + i) for i in range(26)]

m = n
i=1
while True:
    if m <= 26**i:
        break

    else:
        m -= 26**i
        i += 1

ans = []

for j in range(i-1, -1, -1):
    s1 = n // 26**j
    if s1 == 0:
        ans.append(list[s1])
    else:
        ans.append(lis[s1-1])
    n -= (26**j) * s1

print(''.join(ans))
Problem p02689 (python)

Fixed submission:
n, m = map(int, input().split())

h_list = list(map(int,input().split()))

way_list = []
for i in range(m):
    org = list(map(int, input().split()))
    org.sort()
    way_list.append(org)

bad = []

for j in range(m):
    way = way_list[j]
    if h_list[way[0]-1] > h_list[way[1]-1]:
        bad.append(way[1])
    elif h_list[way[0]-1] < h_list[way[1]-1]:
        bad.append(way[0])
    else:
        bad.append(way[0])
        bad.append(way[1])

bad = list(set(bad))
print(n - len(bad))

Buggy submission (verdict: Runtime Error):
import math

x = int(input())

end = math.floor(x**0.2)

for i in range(1, x+1):
    if x % i == 0:
        for a in range(i//2,end,1):
            b = a - i
            if a**5 - b**5 == x:
                print(a, b)
                break
Problem p02831 (python)

Fixed submission:
import sys
import fractions

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

a, b = map(int, readline().split())
print(a * b // fractions.gcd(a,b))

Buggy submission (verdict: Runtime Error):
import sys
import math

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

a, b = map(int, readline().split())
print(a * b // math.gcd(a,b))
Problem p03705 (python)

Fixed submission:
import itertools

n, a, b = map(int, input().split())

if n == 1:
    if a == b:
        print(1)
    else:
        print(0)
elif n == 2:
    print(1)
elif n >= 3:
    if a > b:
        print(0)
    else:
        smin = a * n + (b-a)
        smax = b * n - (b-a)
        print(smax-smin+1)

Buggy submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())

candidate = list(range(a, b+1, 1))
if n == 1:
    if a == b:
        print(1)
    else:
        print(0)
elif n == 2:
    print(1)
elif n >= 3:
    if a > b:
        print(0)
    else:
        smin = a * n + (b-a)
        smax = b * n - (b-a)
        print(smax-smin+1)

Buggy submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())

candidate = list(range(a, b+1, 1))
if n == 1:
    if a == b:
        print(1)
    else:
        print(0)
elif n == 2:
    print(1)
elif n >= 3:
    smin = a * n + (b-a)
    smax = b * n - (b-a)
    print(smax-smin+1)

Buggy submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())

candidate = list(range(a, b+1, 1))
if n == 1:
    if a == b:
        print(1)
    else:
        print(0)
elif n == 2:
    print(1)
elif n >= 3:

    smin = a * n + (b-a)
    smax = b * n - (b-a)
    sl = list(range(smin, smax+1, 1))
    print(len(sl))

Buggy submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())

candidate = list(range(a, b+1, 1))
if n == 1:
    if a == b:
        print(1)
    else:
        print(0)
elif n == 2:
    print(a)
elif n >= 3:
    c = list(itertools.combinations_with_replacement(candidate, n-2))
    suml = []
    for i in c:
        suml.append(sum(i) + 4 + 6)
    sum_set = set(suml)
    print(len(sum_set))

Buggy submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())

candidate = list(range(a, b+1, 1))
if n == 1:
    if a == b:
        print(a)
    else:
        print(0)
elif n == 2:
    print(a)
elif n >= 3:
    c = list(itertools.combinations_with_replacement(candidate, n-2))
    suml = []
    for i in c:
        suml.append(sum(i) + 4 + 6)
    sum_set = set(suml)
    print(len(sum_set))

Buggy submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())

candidate = list(range(a, b+1, 1))
if n == 1:
    print(0)
elif n == 2:
    print(a)
elif n >= 3:
    c = list(itertools.combinations_with_replacement(candidate, n-2))
    suml = []
    for i in c:
        suml.append(sum(i) + 4 + 6)
    sum_set = set(suml)
    print(len(sum_set))

Buggy submission (verdict: Runtime Error):
import itertools

n, a, b = map(int, input().split())

candidate = list(range(a, b+1, 1))
c = list(itertools.combinations_with_replacement(candidate, n))
suml = []
for i in c:
    if min(i) == a and max(i) == b:
        suml.append(sum(i))
sum_set = set(suml)
print(len(sum_set))
Problem p02723 (python)

Fixed submission:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


S = sys.stdin.buffer.readline().decode().rstrip()
ok =S[2] == S[3] and S[4] == S[5]
if ok:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string
import sys
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from functools import lru_cache, reduce
from math import gcd
from operator import add, itemgetter, mul, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
if S[2] == S[3] and S[4] == S[5]:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string
import sys
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from functools import lru_cache, reduce
from math import gcd
from operator import add, itemgetter, mul, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
if S[2] == S[3] and S[4] == S[5]:
    print('Yes')
else:
    print('No')
Problem p02759 (python)

Fixed submission:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N = int(sys.stdin.buffer.readline())
print((N +1)//2)

Buggy submission (verdict: Runtime Error):
import math

ans = (N+1) // 2
print(ans
     )
Problem p02884 (python)

Fixed submission:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
ST = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

graph = [[] for _ in range(N + 1)]
for s, t in ST:
    graph[s].append(t)

# 解説AC
# 閉路ないから後ろから見てけばOK


# dp[v][u]: u からの出口を塞いだときの、v から N までのステップ数の期待値の最小値
dp = np.full((N + 1, N + 1), IINF, dtype=float)
dp[N] = 0.0
for v in reversed(range(1, N)):
    # 塞がない
    dp[v, :] = dp[graph[v], :].sum(axis=0) / len(graph[v]) + 1
    if len(graph[v]) >= 2:
        # 塞ぐ
        s = dp[graph[v], v].sum() - dp[graph[v], v].max()
        dp[v, v] = s / (len(graph[v]) - 1) + 1
print(dp[1].min())

Buggy submission (verdict: Runtime Error):
import os
import sys

import numpy as np

from libs.debug import debug

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
ST = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

graph = [[] for _ in range(N + 1)]
for s, t in ST:
    graph[s].append(t)

# 解説AC
# 閉路ないから後ろから見てけばOK


# dp[v][u]: u からの出口を塞いだときの、v から N までのステップ数の期待値の最小値
dp = np.full((N + 1, N + 1), IINF, dtype=float)
dp[N] = 0.0
for v in reversed(range(1, N)):
    # 塞がない
    dp[v, :] = dp[graph[v], :].sum(axis=0) / len(graph[v]) + 1
    if len(graph[v]) >= 2:
        # 塞ぐ
        s = dp[graph[v], v].sum() - dp[graph[v], v].max()
        dp[v, v] = s / (len(graph[v]) - 1) + 1
print(dp[1].min())
Problem p02984 (python)

Fixed submission:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))


def bisect_left_callable(fn, x, lo, hi):
    """
    (memo) いっぱい呼ぶなら np.searchsorted のほうがベクトル化されててはやそう
    lo から hi-1 のうち、fn の結果が x 以上となる、最も左の値
    bisect.bisect_left と同じ
    https://docs.python.org/ja/3/library/bisect.html
    :param callable fn:
    :param x:
    :param int lo: 最小値
    :param int hi: 最大値 + 1
    :return: lo <= ret <= hi
    """
    while lo < hi:
        mid = (lo + hi) // 2
        if fn(mid) < x:
            lo = mid + 1
        else:
            hi = mid
    return lo


def solve(n, a=False):
    mts = A.copy()
    mts[0] -= n
    mts[-1] -= n
    hist = [n*2]
    for i in range(N):
        mts[(i + 1) % N] -= mts[i]
        hist.append(mts[i] * 2)
    if a:
        return hist
    else:
        return -mts[-1]


n = bisect_left_callable(solve, 0, 0, A[0] + 1)
print(*solve(n, True)[:-1])

Buggy submission (verdict: Runtime Error):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))


def bisect_left_callable(fn, x, lo, hi):
    """
    (memo) いっぱい呼ぶなら np.searchsorted のほうがベクトル化されててはやそう
    lo から hi-1 のうち、fn の結果が x 以上となる、最も左の値
    bisect.bisect_left と同じ
    https://docs.python.org/ja/3/library/bisect.html
    :param callable fn:
    :param x:
    :param int lo: 最小値
    :param int hi: 最大値 + 1
    :return: lo <= ret <= hi
    """
    while lo < hi:
        mid = (lo + hi) // 2
        if fn(mid) < x:
            lo = mid + 1
        else:
            hi = mid
    return lo


def solve(n, a=False):
    mts = A.copy()
    mts[0] -= n
    mts[-1] -= n
    hist = [n*2]
    for i in range(N):
        mts[(i + 1) % N] -= mts[i]
        hist.append(mts[i] * 2)
    if a:
        return hist
    else:
        return -mts[-1]


n = bisect_left_callable(solve, 0, 0, A[0] + 1)
print(*solve(n, True)[:-1])
Problem p03038 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = [list(map(int, reversed(input().split()))) for _ in range(M)]

A.sort()
CB.sort()

ans = 0
for _ in range(N):
    a = A[-1] if A else -INF
    c, b = CB[-1] if CB else (-INF, -INF)
    if a > c:
        ans += a
        A.pop()
    else:
        ans += c
        CB[-1][1] -= 1
        if CB[-1][1] <= 0:
            CB.pop()
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
import operator
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
B, C = zip(*[list(map(int, input().split())) for _ in range(M)])

heap = A
heapq.heapify(heap)


for b, c in zip(B, C):
    for _ in range(b):
        a = heapq.heappop(heap)
        if a < c:
            heapq.heappush(heap, c)
        else:
            heapq.heappush(heap, a)
            break

print(sum(heap))
Problem p03426 (python)

Fixed submission:
import sys

import numpy as np

sys.setrecursionlimit(10000)
INF = float('inf')

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
L, R = zip(*[list(map(int, input().split())) for _ in range(Q)])

# h_pos[a]: a がある位置の h
# w_pos[a]: a がある位置の w
h_pos = np.zeros(H * W + 1)
w_pos = np.zeros(H * W + 1)
for h in range(H):
    for w in range(W):
        h_pos[A[h][w]] = h
        w_pos[A[h][w]] = w

dp = np.zeros(H * W + 1, dtype=int)
for i in range(1, H * W + 1):
    if i - D >= 0:
        dp[i] = dp[i - D] + abs(h_pos[i - D] - h_pos[i]) + abs(w_pos[i - D] - w_pos[i])

for l, r in zip(L, R):
    print(dp[r] - dp[l])

Buggy submission (verdict: Runtime Error):
import sys

import numpy as np

sys.setrecursionlimit(10000)
INF = float('inf')

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
L, R = zip(*[list(map(int, input().split())) for _ in range(Q)])

# h_pos[a]: a がある位置の h
# w_pos[a]: a がある位置の w
h_pos = np.zeros(H * W + 1)
w_pos = np.zeros(H * W + 1)
for h in range(H):
    for w in range(W):
        h_pos[A[h][w]] = h
        w_pos[A[h][w]] = w

dp = np.zeros(H * W + 1, dtype=int)
for i in range(1, H * W + 1):
    if i - D >= 0:
        dp[i] = dp[i - D] + abs(h_pos[i - D] - h_pos[i]) + abs(w_pos[i - D] - w_pos[i])

for l, r in zip(L, R):
    print(dp[r] - dp[l])
Problem p03634 (python)

Fixed submission:
import heapq
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

N = int(sys.stdin.readline())
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)]
Q, K = list(map(int, sys.stdin.readline().split()))
X, Y = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))


def dijkstra(graph, start):
    """
    TODO: start していしなかったときに全部の距離を知りたい

    マイナスの辺があっちゃダメ
    scipy.sparse.csgraph.dijkstra は内部で使われてるフィボナッチヒープが一部のケースでめっちゃ遅いのであんまり使わないほうがよさげ
    https://atcoder.jp/contests/soundhound2018-summer-qual/submissions/5311823

    :param list of (list of (int, int)) graph:
        graph[from_index]: (to_index, weight)
    :param int start:
    :return:
    """
    dist = [float('inf') for _ in range(len(graph))]
    dist[start] = 0
    heap = []
    heapq.heappush(heap, (0, start))

    while len(heap) > 0:
        w, v = heapq.heappop(heap)
        if w > dist[v]:
            continue
        for u, dw in graph[v]:
            if w + dw < dist[u]:
                dist[u] = w + dw
                heapq.heappush(heap, (w + dw, u))
    return dist


#: :type: list of (list of (int, int))
graph = [[] for _ in range(N + 1)]
for a, b, c in ABC:
    graph[a].append((b, c))
    graph[b].append((a, c))

dists = dijkstra(graph, K)

for x, y in zip(X, Y):
    print(int(dists[x] + dists[y]))

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(100000)
INF = float('inf')

N = int(sys.stdin.readline())
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)]
Q, K = list(map(int, sys.stdin.readline().split()))
X, Y = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))


def dfs(v, a, graph, ret=None):
    ret = ret or {}
    ret[v] = a
    for u, w in graph[v]:
        if u in ret:
            continue
        dfs(u, a + w, graph, ret)
    return ret


#: :type: list of (list of (int, int))
graph = [[] for _ in range(N + 1)]
for a, b, c in ABC:
    graph[a].append((b, c))
    graph[b].append((a, c))

dists = dfs(K, 0, graph)
for x, y in zip(X, Y):
    print(int(dists[x] + dists[y]))

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

N = int(sys.stdin.readline())
ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)]
Q, K = list(map(int, sys.stdin.readline().split()))
X, Y = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))


def dfs(v, a, graph, ret=None):
    ret = ret or {}
    ret[v] = a
    for u, w in graph[v]:
        if u in ret:
            continue
        dfs(u, a + w, graph, ret)
    return ret


#: :type: list of (list of (int, int))
graph = [[] for _ in range(N + 1)]
for a, b, c in ABC:
    graph[a].append((b, c))
    graph[b].append((a, c))

dists = dfs(K, 0, graph)
for x, y in zip(X, Y):
    print(int(dists[x] + dists[y]))
Problem p03652 (python)

Fixed submission:
import os
import sys
from collections import Counter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
A = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

A = np.array(A, dtype=int)
idx = np.zeros(N, dtype=int)
used = np.zeros(M + 1, dtype=bool)

ans = INF
for _ in range(M):
    k, cnt = Counter(A[np.arange(N), idx]).most_common(1)[0]
    ans = min(ans, cnt)
    used[k] = True
    for i in range(N):
        while idx[i] < M and used[A[i, idx[i]]]:
            idx[i] += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import os
import sys
from collections import Counter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
A = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

A = np.array(A, dtype=int)
idx = np.zeros(N, dtype=int)
used = np.zeros(M + 1, dtype=bool)

ans = INF
for _ in range(N):
    k, cnt = Counter(A[np.arange(N), idx]).most_common(1)[0]
    ans = min(ans, cnt)
    used[k] = True
    for i in range(N):
        while idx[i] < M and used[A[i, idx[i]]]:
            idx[i] += 1
print(ans)
Problem p02829 (python)

Fixed submission:
a = int(input())
b = int(input())
l = [1, 2, 3]
l.remove(a)
l.remove(b)
print(l[0])

Buggy submission (verdict: Runtime Error):
print(6-int(input()-int(input()))