Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys

import numpy as np

readline = sys.stdin.buffer.readline


def main():
    H, W = map(int, readline().split())
    A = np.array([
        list(map(lambda x: int(x) % 2,
                 readline().split())) for _ in range(H)
    ])
    B = np.reshape(np.arange(H * W), [H, W])
    for i in range(1, H, 2):
        A[i, :] = A[i, :][::-1]
        B[i, :] = B[i, :][::-1]

    C = A.ravel()
    D = B.ravel()

    aa = []
    for i in range(len(C) - 1):
        if C[i] % 2 != 0:
            C[i + 1] += 1
            aa.append(i)

    print(len(aa))
    for i in aa:
        w0, h0 = divmod(D[i], W)
        w1, h1 = divmod(D[i + 1], W)
        print(w0 + 1, h0 + 1, w1 + 1, h1 + 1)


main()


Fixed submission:
import sys

import numpy as np

readline = sys.stdin.buffer.readline


def main():
    H, W = map(int, readline().split())
    A = np.array([
        list(map(lambda x: int(x) % 2,
                 readline().split())) for _ in range(H)
    ])

    aa = []
    for h in range(H):
        for w in range(W - 1):
            if A[h, w] == 1:
                A[h, w + 1] += 1
                aa.append([h + 1, w + 1, h + 1, w + 2])

    for h in range(H - 1):
        w = W - 1
        if A[h, w] % 2 != 0:
            A[h + 1, w] += 1
            aa.append([h + 1, w + 1, h + 2, w + 1])

    print(len(aa))

    for a in aa:
        print(*a)


main()

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np


def main():
    H, W = map(int, input().split())
    A = np.array(
        [list(map(lambda x: int(x) % 2,
                  input().split())) for _ in range(H)])
    AB = np.zeros([2, H, W], dtype=np.int32)
    AB[0, :, :] = A
    AB[1, :, :] = np.reshape(np.arange(H * W), [H, W])
    for i in range(1, H, 2):
        AB[0, i, :] = AB[0, i, :][::-1]
        AB[1, i, :] = AB[1, i, :][::-1]

    C = np.zeros([2, H * W], dtype=np.int32)
    C[0] = AB[0].flatten()
    C[1] = AB[1].flatten()

    aa = []
    for i in range(len(C[0]) - 1):
        if C[0, i] % 2 == 0:
            continue
        else:
            C[0, i] -= 1
            C[0, i + 1] += 1
            aa.append((C[1, i], C[1, i + 1]))

    print(len(aa))
    for a, b in aa:
        w0, h0 = divmod(a, W)
        w1, h1 = divmod(b, W)
        print(w0 + 1, h0 + 1, w1 + 1, h1 + 1)


main()


Fixed submission:
import sys

import numpy as np

readline = sys.stdin.buffer.readline


def main():
    H, W = map(int, readline().split())
    A = np.array([
        list(map(lambda x: int(x) % 2,
                 readline().split())) for _ in range(H)
    ])

    aa = []
    for h in range(H):
        for w in range(W - 1):
            if A[h, w] == 1:
                A[h, w + 1] += 1
                aa.append([h + 1, w + 1, h + 1, w + 2])

    for h in range(H - 1):
        w = W - 1
        if A[h, w] % 2 != 0:
            A[h + 1, w] += 1
            aa.append([h + 1, w + 1, h + 2, w + 1])

    print(len(aa))

    for a in aa:
        print(*a)


main()
Buggy python submission (verdict: Runtime Error):
import sys

readline = sys.stdin.buffer.readline


def main():
    N, K = map(int, readline().split())
    X = list(map(int, readline().split()))

    if N == 1:
        print(abs(X[0]))
        sys.exit()

    dm = X[K] - X[0]
    for i in range(0, N-K+1):
        d = X[i+K-1] - X[i] + min(abs(X[i+K-1]), abs(X[i]))
        if d < dm:
            dm = d

    print(dm)


main()


Fixed submission:
import sys

readline = sys.stdin.buffer.readline


def main():
    N, K = map(int, readline().split())
    X = list(map(int, readline().split()))

    if N == 1:
        print(abs(X[0]))
        sys.exit()

    dm = 10**9
    for i in range(0, N-K+1):
        d = X[i+K-1] - X[i] + min(abs(X[i+K-1]), abs(X[i]))
        if d < dm:
            dm = d

    print(dm)


main()
Buggy python submission (verdict: Runtime Error):
import copy

import numpy as np


def main():
    N = int(inpupt())
    X = np.array(list(map(int, input().split())))
    m = np.median(X)

    XX = copy.deepcopy(X)
    mm = np.argmin(X)
    XX = np.delete(XX, mm)
    m0 = np.median(XX)

    XX = copy.deepcopy(X)
    mm = np.argmax(X)
    XX = np.delete(XX, mm)
    m1 = np.median(XX)

    for x in X:
        if x <= m:
            print(m0)
        else:
            print(m1)


main()


Fixed submission:
import copy

import numpy as np


def main():
    N = int(input())
    X = np.array(list(map(int, input().split())))
    m = np.median(X)

    XX = copy.deepcopy(X)
    mm = np.argmin(X)
    XX = np.delete(XX, mm)
    m0 = int(np.median(XX))

    XX = copy.deepcopy(X)
    mm = np.argmax(X)
    XX = np.delete(XX, mm)
    m1 = int(np.median(XX))

    for x in X:
        if x <= m:
            print(m0)
        else:
            print(m1)


main()
Buggy python submission (verdict: Runtime Error):
import sys

import numpy as np


def main():
    N = int(input())
    red = np.zeros([N, 3])
    blue = np.zeros([N, 3])

    for i in range(N):
        red[i, 0:2] = list(map(int, input().split()))

    for i in range(N):
        blue[i, 0:2] = list(map(int, input().split()))

    blue[:, 2] = blue[:, 0] + blue[:, 1]
    blue = blue[np.argsort(blue[:, 2])]

    red[:, 2] = red[:, 0] + red[:, 1]
    red = red[np.argsort(blue[:, 2])]

    d = np.zeros([N, 2])
    cnt = 0

    for i in range(N):
        d[0:N, 0] = blue[i, 0] - red[0:N, 0]
        d[0:N, 1] = blue[i, 1] - red[0:N, 1]

        if np.any(d[:, 0] > 0) and np.any(d[:, 1] > 0):
            dd = d[(d[:, 0] > 0) & (d[:, 1] > 0)]
            ind = np.where(d == dd[-1])
            row = ind[0][0]
            red[row, :] = 10**9
            cnt += 1

    print(cnt)


main()


Fixed submission:
import numpy as np


def main():
    N = int(input())
    red = np.zeros([N, 2])
    blue = np.zeros([N, 2])

    for i in range(N):
        red[i, 0:2] = list(map(int, input().split()))

    for i in range(N):
        blue[i, 0:2] = list(map(int, input().split()))

    blue = blue[np.argsort(blue[:, 0])]
    red = red[np.argsort(red[:, 1])[::-1]]

    d = np.zeros([N, 2])
    cnt = 0

    for i in range(N):
        d[0:N, 0:2] = blue[i, 0:2] - red[0:N, 0:2]

        if np.any((d[:, 0] > 0) & (d[:, 1] > 0)):
            dd = d[(d[:, 0] > 0) & (d[:, 1] > 0)]
            dd = dd[np.argsort(dd[:, 1])]
            ind = list(zip(*np.where(d == dd[0])))
            row = ind[0][0]
            red[row, :] = 10**9
            cnt += 1

    print(cnt)


main()
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

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

# 1 -> 1+D ->
# 2 -> 2+D ->
# 3 -> 3+D -> みたいなものを事前に計算

# 辞書で管理する
# d[A[i][j]] = [i,j] みたいな感じ
d = defaultdict(list)
for i in range(H):
    for j in range(W):
        d[A[i][j]].append([i, j])


p = []
for s in range(1, D + 1):
    c = 0
    q = [0]
    for i in range(s + D, H * W + 1, D):
        i0, j0 = d[i - D][0]
        i1, j1 = d[i][0]
        c += abs(i0 - i1) + abs(j0 - j1)
        q.append(c)

    p.append(q)


for l, r in LR:
    i = l % D - 1
    s = (l + i) // D
    e = (r + i) // D
    print(p[i][e] - p[i][s])


Fixed submission:
from collections import defaultdict

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

# 1 -> 1+D ->
# 2 -> 2+D ->
# 3 -> 3+D -> みたいなものを事前に計算

# 辞書で管理する
# d[A[i][j]] = [i,j] みたいな感じ
d = defaultdict(list)
for i in range(H):
    for j in range(W):
        d[A[i][j]].append([i, j])


p = []
for s in range(1, D + 1):
    c = 0
    q = [0]
    for i in range(s + D, H * W + 1, D):
        i0, j0 = d[i - D][0]
        i1, j1 = d[i][0]
        c += abs(i0 - i1) + abs(j0 - j1)
        q.append(c)

    p.append(q)


for l, r in LR:
    i = l % D - 1
    s = (l - 1) // D
    e = (r - 1) // D
    print(p[i][e] - p[i][s])

Buggy python submission (verdict: Runtime Error):
H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

# 1 -> 1+D ->
# 2 -> 2+D ->
# 3 -> 3+D -> みたいなものを事前に計算

P = []
for k in range(1, D + 1):
    q = []
    for d in range(k, H * W + 1, D):
        for i in range(H):
            try:
                n = A[i].index(d)
                q.append([i, n])
                break
            except:
                continue
    P.append(q)

d = []

for p in P:
    c = 0
    q = [0]
    for i in range(len(p) - 1):
        c += abs(p[i][0] - p[i + 1][0])
        c += abs(p[i][1] - p[i + 1][1])
        q.append(c)
    d.append(q)

for l, r in LR:
    i = l % D - 1
    s = (l + i) // D
    e = (r + i) // D
    print(d[i][e] - d[i][s])


Fixed submission:
from collections import defaultdict

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

# 1 -> 1+D ->
# 2 -> 2+D ->
# 3 -> 3+D -> みたいなものを事前に計算

# 辞書で管理する
# d[A[i][j]] = [i,j] みたいな感じ
d = defaultdict(list)
for i in range(H):
    for j in range(W):
        d[A[i][j]].append([i, j])


p = []
for s in range(1, D + 1):
    c = 0
    q = [0]
    for i in range(s + D, H * W + 1, D):
        i0, j0 = d[i - D][0]
        i1, j1 = d[i][0]
        c += abs(i0 - i1) + abs(j0 - j1)
        q.append(c)

    p.append(q)


for l, r in LR:
    i = l % D - 1
    s = (l - 1) // D
    e = (r - 1) // D
    print(p[i][e] - p[i][s])
Buggy python submission (verdict: Runtime Error):
import numpy as np


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    C = np.zeros([2, N], dtype=np.int64)
    C[0, :] = A
    C[1, :] = B
    d = np.diff(C, axis=0)
    cnt1 = np.sum(d)
    cnt2 = np.sum(d)
    for i in range(len(d[0, :])):
        if d[0, i] > 0:
            cnt1 -= dimport numpy as np


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    C = np.zeros([2, N], dtype=np.int64)
    C[0, :] = A
    C[1, :] = B
    d = np.diff(C, axis=0)
    cnt1 = np.sum(d)
    cnt2 = np.sum(d)
    for i in range(len(d[0, :])):
        if d[0, i] > 0:
            cnt1 -= d[0, i] / 2
        if d[0, i] < 0:
            cnt2 -= -d[0, i]

    print('Yes' if min(cnt1, cnt2) >= 0 else 'No')


main()
[0, i] / 2
        if d[0, i] < 0:
            cnt2 += d[0, i]

    print('Yes' if max(cnt1, cnt2) >= 0 else 'No')


main()


Fixed submission:
import numpy as np


def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    B = np.array(list(map(int, input().split())))
    sum_A = np.sum(A)
    sum_B = np.sum(B)
    diff_AB = A - B

    if sum_A > sum_B:
        ans = 'No'
    else:
        cnt_1 = 0
        cnt_2 = 0
        for d in diff_AB:
            if d > 0:
                cnt_1 += d
            else:
                cnt_2 += (-d + 1) // 2

        if max(cnt_1, cnt_2) <= sum_B - sum_A:
            ans = 'Yes'
        else:
            ans = 'No'

    print(ans)


main()

Buggy python submission (verdict: Runtime Error):
import numpy as np


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    if A == B:
        print('Yes')
    else:
        C = np.zeros([2, N], dtype=np.int32)
        C[0, :] = A
        C[1, :] = B
        d = np.diff(C, axis=0)
        a = np.sum(d[d > 0])
        b = np.sum(d[d < 0])
        if a >= -2 * b:
            print('Yes')
        else:
            if (-2 * b - a) % len(d[d > 0]) == 0 and (
                (-2 * b - a) / len(a)) % 2 == 0:
                print('Yes')
            else:
                print('No')


main()


Fixed submission:
import numpy as np


def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    B = np.array(list(map(int, input().split())))
    sum_A = np.sum(A)
    sum_B = np.sum(B)
    diff_AB = A - B

    if sum_A > sum_B:
        ans = 'No'
    else:
        cnt_1 = 0
        cnt_2 = 0
        for d in diff_AB:
            if d > 0:
                cnt_1 += d
            else:
                cnt_2 += (-d + 1) // 2

        if max(cnt_1, cnt_2) <= sum_B - sum_A:
            ans = 'Yes'
        else:
            ans = 'No'

    print(ans)


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


read = sys.stdin.buffer.read


def main():
    N = int(input())
    m = map(int, read().split())
    CSF = list(zip(m, m, m))
    tt = []

    for j in range(N-1):
        t = CSF[j][1] + CSF[j][0]
        for i in range(j+1, N-1):
            if t >= CSF[i][1]:
                n = (t - CSF[i][1]) / CSF[i][2]
                t = CSF[i][1] + CSF[i][2] * math.ceil(n)
                t += CSF[i][0]
            else:
                t = CSF[i][1] + CSF[i][0]

        tt.append(t)

    tt.append(0)
    for t in tt:
        print(t)


main()


Fixed submission:
import math


def main():
    N = int(input())
    CSF = [[int(x) for x in input().split()] for _ in range(N-1)]
    tt = []

    for j in range(N-1):
        t = CSF[j][1] + CSF[j][0]
        for i in range(j+1, N-1):
            if t >= CSF[i][1]:
                n = (t - CSF[i][1]) / CSF[i][2]
                t = CSF[i][1] + CSF[i][2] * math.ceil(n)
                t += CSF[i][0]
            else:
                t = CSF[i][1] + CSF[i][0]

        tt.append(t)

    tt.append(0)
    for t in tt:
        print(t)


main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys

import numpy as np

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


def searchPrimeNumber(N):
    N = N + 1
    is_prime = np.ones((N,), dtype=bool)
    is_prime[:2] = 0
    N_max = int(np.sqrt(N))
    for j in range(2, N_max+1):
        if (is_prime[j]):
            is_prime[j*j::j] = False
    prime = np.arange(N)[is_prime]
    return prime


def main():
    Q = int(readline())
    m = map(int, read().split())
    LR = list(zip(m, m))
    # 事前に10**5までにある2017に似た素数リストを作る
    A = searchPrimeNumber(10**5)

    like_2017 = np.zeros(10**5 + 1, dtype=bool)
    for a in A:
        if (a + 1) / 2 in A:
            like_2017[a] = True

    B = np.arange(10**5+1)[like_2017]
    for l, r in LR:
        C = B[B <= r]
        print(len(C[l <= C]))
        


main()


Fixed submission:
import sys

import numpy as np

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


def searchPrimeNumber(N):
    N = N + 1
    is_prime = np.ones(N, dtype=bool)
    is_prime[:2] = 0
    N_max = int(np.sqrt(N))
    for j in range(2, N_max+1):
        if (is_prime[j]):
            is_prime[j*j::j] = False
    prime = np.arange(N)[is_prime]
    return prime


def main():
    Q = int(readline())
    m = map(int, read().split())
    LR = list(zip(m, m))
    A = searchPrimeNumber(10**5)
    B = np.zeros(10**5+3, dtype=np.int32)  # Bはゼロ始まり
    for a in A:
        if (a + 1) / 2 in A:
            B[a+1:] += 1  # like 2017の数より１つ大きい数から1増やす

    for l, r in LR:
        print(B[r+1] - B[l])


main()
Buggy python submission (verdict: Runtime Error):
import sys

import numpy as np

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


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    A = np.array(A)

    cnt = 0
    for _ in range(10**9):
        A, F = np.divmod(A, 2)
        if np.max(F) == 1:
            break
        cnt += 1

    print(cnt)


main()


Fixed submission:
import sys

import numpy as np

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


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    A = np.array(A)

    f = 0
    cnt = 0
    for _ in range(10**9):
        f = np.max(np.mod(A, 2))
        if f == 1:
            break
        A[:] = A[:]/2
        cnt += 1

    print(cnt)


main()
Buggy python submission (verdict: Runtime Error):
S = list(input())

AK = list('AKIHABARA')

if len(S) >= len(AK):
    print('NO')
else:
    for i in range(len(S)):
        if S[i] != AK[i]:
            S.insert(i, AK[i])
    for i in range(len(S)):
        if S[i] != AK[i]:
            S.insert(i, AK[i])

    if len(S) != len(AK):
        S.append('A')

    if ''.join(S) == ''.join(AK):
        print('YES')
    else:
        print('NO')


Fixed submission:
S = list(input())

AK = list('AKIHABARA')

if S == AK:
    ans = 'YES'
elif len(S) >= len(AK):
    ans = 'NO'
else:
    for i in range(len(S)):
        if S[i] != AK[i]:
            S.insert(i, AK[i])
    if len(S) > len(AK):
        ans = 'NO'
    else:
        for i in range(len(S)):
            if S[i] != AK[i]:
                S.insert(i, AK[i])

        if len(S) > len(AK):
            ans = 'NO'
        else:
            if len(S) != len(AK):
                S.append('A')

            if S == AK:
                ans = 'YES'
            else:
                ans = 'NO'

print(ans)
Buggy python submission (verdict: Runtime Error):
from copy import deepcopy
import itertools

A, B, C, D, E, F = map(int, input().split())

qA = [i // 100 for i in range(0, F + 1, A * 100)]
qB = [i // 100 for i in range(0, F + 1, B * 100)]
qC = [i for i in range(0, F + 1, C)]
qD = [i for i in range(0, F + 1, D)]

W = set(qA + qB)
for i in range(F // min(A, B)):
    nW = deepcopy(W)
    for w in W:
        if (w + A) * 100 <= F:
            nW.add(w + A)
        if (w + B) * 100 <= F:
            nW.add(w + B)

    if len(W) == len(nW):
        break
    W = deepcopy(nW)

W = nW

S = set(qC + qD)
for i in range(F // min(C, D)):
    nS = deepcopy(S)
    for s in S:
        if (s + C) <= F:
            nS.add(s + C)
        if (s + D) <= F:
            nS.add(s + D)

    if len(S) == len(nS):
        break
    S = deepcopy(nS)
S = nS

M = 0
for w, s in itertools.product(W, S):
    if w != 0 and s <= w * E and s + 100 * w <= F:
        if M < s / (w * 100 + s):
            M = s / (w * 100 + s)
            Ms = s
            Mw = w

print(Ms + Mw * 100)
print(Ms)


Fixed submission:
from copy import deepcopy
import itertools

A, B, C, D, E, F = map(int, input().split())

qA = [i // 100 for i in range(0, F + 1, A * 100)]
qB = [i // 100 for i in range(0, F + 1, B * 100)]
qC = [i for i in range(0, F + 1, C)]
qD = [i for i in range(0, F + 1, D)]

W = set(qA + qB)
for i in range(F // min(A, B)):
    nW = deepcopy(W)
    for w in W:
        if (w + A) * 100 <= F:
            nW.add(w + A)
        if (w + B) * 100 <= F:
            nW.add(w + B)

    if len(W) == len(nW):
        break
    W = deepcopy(nW)

W = nW

S = set(qC + qD)
for i in range(F // min(C, D)):
    nS = deepcopy(S)
    for s in S:
        if (s + C) <= F:
            nS.add(s + C)
        if (s + D) <= F:
            nS.add(s + D)

    if len(S) == len(nS):
        break
    S = deepcopy(nS)
S = nS

M = 0
Ms = 0
Mw = A
for w, s in itertools.product(W, S):
    if w != 0 and s <= w * E and s + 100 * w <= F:
        if M < s / (w * 100 + s):
            M = s / (w * 100 + s)
            Ms = s
            Mw = w

print(Ms + Mw * 100)
print(Ms)
Buggy python submission (verdict: Runtime Error):
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    P = list(map(int, readline().split()))
    P = list(map(lambda x: x-1, P))

    cnt = 0
    for i in range(0, N):
        if P[i] == i:
            if P[i+1] == i+1 and i != N-1:
                P[i], P[i+1] = P[i+1], P[i]
                cnt += 1
            elif i != 0:
                P[i], P[i-1] = P[i-1], P[i]
                cnt += 1
            else:
                cnt += 1

    print(cnt)


main()


Fixed submission:
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    P = list(map(int, readline().split()))
    P = list(map(lambda x: x-1, P))

    cnt = 0
    for i in range(0, N):
        if i == N-1:
            if P[i] == i:
                P[i], P[i-1] = P[i-1], P[i]
                cnt += 1
        elif P[i] == i:
            if P[i+1] == i+1 and i != N-1:
                P[i], P[i+1] = P[i+1], P[i]
                cnt += 1
            elif i != 0:
                P[i], P[i-1] = P[i-1], P[i]
                cnt += 1
            else:
                P[i], P[i+1] = P[i+1], P[i]
                cnt += 1

    print(cnt)


main()
Buggy python submission (verdict: Runtime Error):
import math
import sys
from functools import reduce

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


def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def main():
    N = int(readline())
    T = list(map(int, read().split()))
    print(lcm(*T))


main()


Fixed submission:
import fractions
import sys
from functools import reduce

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


def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def main():
    N = int(readline())
    T = list(map(int, read().split()))
    print(lcm(*T))


main()
Buggy python submission (verdict: Runtime Error):
import networkx as nx

N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N - 1)]
Q, K = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(Q)]

G = nx.Graph()

for a, b, c in ABC:
    G.add_edge(a - 1, b - 1, weight=c)

pred, dist = nx.dijkstra_predecessor_and_distance(G, K - 1)

for x, y in XY:
    print(dist[x - 1] + dist[y - 1])



Fixed submission:
import networkx as nx

N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N - 1)]
Q, K = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(Q)]

G = nx.Graph()

for a, b, c in ABC:
    G.add_edge(a - 1, b - 1, weight=c)

pred, dist = nx.dijkstra_predecessor_and_distance(G, K - 1)

for x, y in XY:
    print(dist[x - 1] + dist[y - 1])
Buggy python submission (verdict: Time Limit Exceeded):
import math
import sys


N, M = map(int, input().split())

nn = max(N, M)
mm = min(N, M)

if mm != nn and mm + 1 < nn:
    print(0)
    sys.exit()

if N == M:
    print(math.factorial(N)*math.factorial(M) * 2)
else:
    print(math.factorial(N)*math.factorial(M))


Fixed submission:
import math
import sys


N, M = map(int, input().split())
C = 10**9 + 7

nn = max(N, M)
mm = min(N, M)

if mm != nn and mm + 1 < nn:
    print(0)
    sys.exit()

if N == M:
    pN = math.factorial(N) % C
    pM = math.factorial(M) % C
    p = pN * pM * (2 % C)
    print(p % C)

else:
    pN = math.factorial(N) % C
    pM = math.factorial(M) % C
    print((pN * pM) % C)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    rA = list(map(lambda x: -1 * x, A[::-1]))

    MM = A[:N]
    mm = rA[:N]
    heapq.heapify(MM)
    heapq.heapify(mm)
    M = [sum(MM)]
    m = [sum(mm)]

    for i in range(N, 2*N):
        heapq.heappushpop(MM, A[i])
        heapq.heappushpop(mm, rA[i])
        M.append(sum(MM))
        m.append(sum(mm))

    Dm = -10**14
    for i, j in zip(M, m[::-1]):
        if i + j > Dm:
            Dm = i + j

    print(Dm)


main()


Fixed submission:
import heapq
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    rA = list(map(lambda x: -1 * x, A[::-1]))

    MM = A[:N]
    mm = rA[:N]
    heapq.heapify(MM)
    heapq.heapify(mm)
    M = [sum(MM)]
    m = [sum(mm)]

    for i in range(N, 2*N):
        pM = heapq.heappushpop(MM, A[i])
        pm = heapq.heappushpop(mm, rA[i])
        M.append(M[-1] - pM + A[i])
        m.append(m[-1] - pm + rA[i])

    Dm = -10**14
    for i, j in zip(M, m[::-1]):
        if i + j > Dm:
            Dm = i + j

    print(Dm)


main()