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

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def gcd(a,b):
    while b:a,b=b,a%b
    return a

def main():
    a,b,c=MI()
    g=gcd(a,b)
    if c%g:print("NO")
    else:print("YES")

main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
import numpy as np

def main():
    a,b,c=MI()
    g=np.gcd(a,b)
    if c%g:print("NO")
    else:print("YES")

main()
Problem p03732 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n, lim = MI()
    ww = []
    vv = []
    for _ in range(n):
        w, v = MI()
        ww.append(w)
        vv.append(v)
    w0=ww[0]
    # dp[i][j]…荷物の個数がiで、端数の合計がjのときの価値の最大値
    dp=[[0]*(3*n+1) for _ in range(n+1)]
    for w,v in zip(ww,vv):
        for i in range(n,0,-1):
            sj=min(lim-i*w0,3*n)
            for j in range(sj,-1,-1):
                nj=j-(w-w0)
                if nj<0:break
                dp[i][j]=max(dp[i][j],dp[i-1][nj]+v)
    #p2D(dp)
    print(max(max(row) for row in dp))

main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n, lim = MI()
    ww = []
    vv = []
    for _ in range(n):
        w, v = MI()
        ww.append(w)
        vv.append(v)
    w0=ww[0]
    # dp[i][j]…荷物の個数がiで、端数の合計がjのときの価値の最大値
    dp=[[0]*(3*n+1) for _ in range(n+1)]
    for w,v in zip(ww,vv):
        for i in range(n,0,-1):
            for j in range(lim-i*w0,-1,-1):
                nj=j-(w-w0)
                if nj<0:continue
                dp[i][j]=max(dp[i][j],dp[i-1][nj]+v)
    #p2D(dp)
    print(max(max(row) for row in dp))

main()
Problem p03740 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
def MI(): return map(int, sys.stdin.readline().split())

def main():
    x,y=MI()
    if x>y:x,y=y,x
    if y>x+1:print("Alice")
    else:print("Brown")

main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    memo={}
    def win(a,b):
        if a>b:a,b=b,a
        if (a,b)==(0,0) or (a,b)==(0,1) or (a,b)==(1,1):return False
        if (a,b) in memo:return memo[a,b]
        for i in range(1,a):
            r=a-i*2
            if r<0:break
            if not win(r,b+i):
                memo[a,b]=True
                return True
        for i in range(1,b):
            r=b-i*2
            if r<0:break
            if not win(r,a+i):
                memo[a,b]=True
                return True
        memo[a, b] = False
        return False

    x,y=MI()
    if win(x,y):print("Alice")
    else:print("Brown")

main()
Problem p03780 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def need(m):
        s=1
        for i in range(n):
            if i==m:continue
            s|=s<<aa[i]
            s&=mask
        #print(m,bin(s),bin(s>>k-aa[m]))
        if s>>k-aa[m]:return True
        return False

    n,k=MI()
    mask=(1<<k)-1
    #print(bin(mask))
    aa=LI()
    aa.sort()
    for i in range(n):
        if aa[i]>k:
            aa=aa[:i]
            break
    n=len(aa)
    #print(aa)
    l=-1
    r=n
    while l+1<r:
        m=(l+r)//2
        #while aa[m-1]==aa[m]:m-=1
        #print(m,aa[m],need(m))
        if need(m):r=m
        else:l=m
    print(l+1)

main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def need(m):
        s=1
        for i in range(m+1,n):
            s|=s<<aa[i]
            s&=mask
        #print(m,bin(s),bin(s>>k-aa[m]))
        if s>>k-aa[m]:return True
        return False

    n,k=MI()
    mask=(1<<k)-1
    #print(bin(mask))
    aa=LI()
    aa.sort()
    for i in range(n):
        if aa[i]>k:
            aa=aa[:i]
            break
    n=len(aa)
    #print(aa)
    l=-1
    r=n
    while l+1<r:
        m=(l+r)//2
        while aa[m-1]==aa[m]:m-=1
        #print(m,aa[m],need(m))
        if need(m):r=m
        else:l=m
    print(l+1)

main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def need(m):
        s=1
        for i in range(m+1,n):
            a=aa[i]
            s|=s<<a
            s&=mask
        #print(m,bin(s))
        for i in range(max(0,k-aa[m]),k):
            if s>>i&1:return True
        return False

    n,k=MI()
    mask=(1<<k+1)-1
    #print(bin(mask))
    aa=LI()
    aa.sort()
    #print(aa)
    l=-1
    r=n
    while l+1<r:
        m=(l+r)//2
        while aa[m-1]==aa[m]:m-=1
        #print(m,aa[m],need(m))
        if need(m):r=m
        else:l=m
    print(l+1)

main()
Problem p03963 (python)

Fixed submission:
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    n,k=MI()
    ans=k*pow(k-1,n-1)
    print(ans)

main()

Buggy submission (verdict: Runtime Error):
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *
from math import gcd

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    n,k=MI()
    ans=k*pow(k-1,n-1)
    print(ans)

main()
Problem p02642 (python)

Fixed submission:
N = int(input())
A = sorted(list(map(int, input().split())))
if N == 1:
    print(1)
elif A[0] == 1 and A[1] == 1:
    print(0)
elif A[0] == 1:
    print(1)
else:
    Eratos = [False]*1000001
    ans = 0
    for i in range(N):
        if Eratos[A[i]]:
            ans += 1
        else:
            if i < N-1 and A[i] == A[i+1]:
                ans += 1
            for j in range(A[i], 1000001, A[i]):
                Eratos[j] = True
    print(N-ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = sorted(list(map(int, input().split())))
if N == 1:
    print(1)
elif A[0] == 1 and A[1] == 1:
    print(0)
elif A[0] == 1:
    print(1)
else:
    Eratos = [False]*1000001
    ans = 0
    for i in range(N):
        if Eratos[A[i]]:
            ans += 1
        else:
            for j in range(A[i], 1000001, A[i]):
                Eratos[j] = True
print(N-ans)
Problem p02665 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
if N == 0:
    if A[0] == 1:
        print(1)
    else:
        print(-1)
elif A[0] != 0:
    print(-1)
else:
    rest = [1]
    for i in range(1, N+1):
        rest.append(min(rest[i-1]*2 - A[i], 10**13))
    ans, cnodes = A[N], A[N]
    for i in range(N-1, -1, -1):
        if cnodes//2 + cnodes%2 > rest[i]:
            print(-1)
            exit()
        else:
            cnodes = min(cnodes, rest[i]) + A[i]
            ans += cnodes
    print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
if N == 0:
    if A[0] == 1:
        print(1)
    else:
        print(-1)
elif A[0] != 0:
    print(-1)
else:
    rest = [1]
    for i in range(1, N+1):
        rest.append(min(rest[i-1]*2 - A[i], 10**13))
    ans, cnodes = A[N], A[N]
    for i in range(N-1, -1, -1):
        if cnodes//2 + cnodes%2 > rest[i]:
            print(-1)
            exit()
        else:
            cnodes = min(cnodes, rest[i]) + A[i]
            ans += cnodes
print(ans)
Problem p02714 (python)

Fixed submission:
import collections

N = int(input())
S = input()

c = collections.Counter(S)
if 'R' in c.keys() and 'G' in c.keys() and 'B' in c.keys():
    ans = c['R'] * c['G'] * c['B']

    for i in range(N-2):
        for x in range(((N-i)-1)//2):
            if S[i] != S[i+x+1] and S[i] != S[i+2*(x+1)] and S[i+x+1] != S[i+2*(x+1)]:
                ans -= 1
else:
    ans = 0
print(ans)

Buggy submission (verdict: Runtime Error):
import collections

N = int(input())
S = input()

c = collections.Counter(S)
if 'R' in c.keys() and 'G' in c.keys() and 'B' in c.keys():
    ans = c['R'] * c['G'] * c['B']

    for x in range(N//2 - 1):
        for i in range(N - 2*(x+1)):
            if S[i] != S[i+x+1] and S[i] != S[i+2*(x+1)] and S[i+x+1] != S[i+2*(x+1)]:
                ans -= 1
print(ans)
Problem p02804 (python)

Fixed submission:
N, K = map(int, input().split())
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print((MAX - MIN) % MOD)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
Ar = A[::-1]
Ai = [Ar[i] - A[i] for i in range(N)]
ans = 0

if K > N//2:
    n, r = N-1, K-1 
    C = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    for i in range(N-K+1):
        ans += Ai[i] * C
        C *= N-i-K
        if N-i-1 != 0:
            C //= N-i-1
else:
    C = 1
    for i in range(N-K, -1, -1):
        ans += Ai[i] * C
        C *= N - i
        if N-K+1 - i != 0:
            C //= N-K+1 - i

print(ans % 1000000007)

Buggy submission (verdict: Runtime Error):
import math

def C(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
ans = 0

C = C(N-1, K-1)
for i in range(N-K+1):
    ans += (A[-i-1] - A[i]) * C
    C *= N-i-K
    if N-i-1 != 0:
        C /= N-i-1

print(int(ans % 1000000007))
Problem p02900 (python)

Fixed submission:
A, B = map(int, input().split())
if A > B:
    A, B = B, A

length = int(A ** 0.5) + 2
Ax = A
s = [1] * length
ans, i, lim, t = 1, 2, A ** 0.5, set()
while i < lim:
    if s[i] == 1:
        for j in range(i*2, length, i):
            s[j] = 0
        if A % i == 0:
            if B % i == 0:
                ans += 1
            while Ax % i == 0:
                Ax //= i
    i += 1

if Ax >= length and B % Ax == 0:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
A, B = map(int, input().split())
if A > B:
    A, B = B, A

s = [1] * (A+1)
ans, t = 1, set()
i, lim = 2, A ** 0.5
while i < lim:
    if s[i] == 1:
        for j in range(i*2, A+1, i):
            s[j] = 0
        if A % i == 0 and B % i == 0:
            ans += 1
    i += 1

print(ans)
Problem p02924 (python)

Fixed submission:
N = int(input())
ans = N*(N-1)//2
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
print(sum(list(range(N))))
Problem p02936 (python)

Fixed submission:
N,Q = map(int,input().split())
Ki_p = [0 for i in range(N+1)]
Ki_c = [[] for i in range(N+1)]
Ki_co = [0 for i in range(N+1)]
 
for i in range(N-1):
    a,b = map(int,input().split())
    Ki_p[b] = a
    Ki_c[a] += [b]
 
for i in range(Q):
    p,x = map(int,input().split())
    Ki_co[p] += x
 
ans = [0 for i in range(N+1)]
q = {1}
for i in range(N):
    vi = q.pop()
    ans[vi] = Ki_co[vi] + ans[Ki_p[vi]]
    q |= set(Ki_c[vi])
    
print(' '.join(map(str,ans[1:N+1])))

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
Ki_p = [0 for i in range(N+1)]
Ki_c = [[] for i in range(N+1)]
Ki_co = [0 for i in range(N+1)]

def answer(vi,ans):
    ans[vi] = Ki_co[vi] + ans[Ki_p[vi]]
    for v in Ki_c[vi]:
        answer(v,ans)    

for i in range(N-1):
    a,b = map(int,input().split())
    Ki_p[b] = a
    Ki_c[a].append(b)

for i in range(Q):
    p,x = map(int,input().split())
    Ki_co[p] += x

ans = [0 for i in range(N+1)]
answer(1,ans)

print(' '.join(map(str,ans[1:N+1])))
Problem p03044 (python)

Fixed submission:
from collections import deque

N = int(input())
tree = [dict() for _ in range(N+1)]
color = [0]*(N+1)
parent = [0]*(N+1)
for _ in range(N-1):
    u, v, w = map(int, input().split())
    w %= 2
    tree[u][v] = w
    tree[v][u] = w

que = deque([1])
while len(que) != 0:
    u = que.pop()
    Rel, c, p = tree[u], color[u], parent[u]
    for v in Rel.keys():
        if v == p:
            continue
        elif Rel[v] == 0:
            color[v] = c
        else:
            color[v] = c^1
        parent[v] = u
        que.append(v)

for c in color[1:]:
    print(c)

Buggy submission (verdict: Runtime Error):
from collections import deque

N = int(input())
color = [0]*(N+1)
color_val = -1
finded = set()
codebook = {0:0, 1:1}

for _ in range(N-1):
    u, v, w = map(int, input().split())
    if u in finded and v in finded:
        if color[u] in codebook:
            codebook[color[v]] = (codebook[color[u]] + (w % 2)) % 2
            if color[v] % 2 == 0:
                codebook[color[v] + 1] = (codebook[color[v]] + 1) % 2
            else:
                codebook[color[v] - 1] = (codebook[color[v]] + 1) % 2
        else:
            codebook[color[u]] = (codebook[color[v]] + (w % 2)) % 2
            if color[u] % 2 == 0:
                codebook[color[u] + 1] = (codebook[color[u]] + 1) % 2
            else:
                codebook[color[u] - 1] = (codebook[color[u]] + 1) % 2
    elif u in finded:
        finded.add(v)
        color[v] = (color[u] + (w % 2)) % 2 + color[u]//2 * 2
    elif v in finded:
        finded.add(u)
        color[u] = (color[v] + (w % 2)) % 2 + color[v]//2 * 2
    else:
        finded.add(u)
        finded.add(v)
        color_val += 1
        if w % 2 == 0:
            color[u], color[v] = 2 * color_val, 2 * color_val
        if w % 2 != 0:
            color[u], color[v] = 2 * color_val, 2 * color_val + 1

for i in range(1, N+1):
    print(codebook[color[i]])
Problem p03060 (python)

Fixed submission:
N = int(input())
V = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]
P = [V[x]-C[x] for x in range(N)]

P = sorted(P)[::-1]
i,ans = 0,0

while P[i] > 0:
    ans += P[i]
    i += 1
    if i == N:
        break
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
V = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]
P = [V[x]-C[x] for x in range(N)]

P = sorted(P)[::-1]
i,ans = 0,0

while P[i] > 0:
    ans += P[i]
    i += 1
print(ans)
Problem p03090 (python)

Fixed submission:
N = int(input())
X = list(range(1,N+1))
Nsum = sum(X)

n = 2 
while Nsum % n != 0:
    n += 1

group = [[] for i in range(n)]
value = int(Nsum/n)

for i in range(n-1):
    for j in range(N):
        if sum(X[-j-1:]) > value:
            break
    group[i] = X[-j:]
    X = X[:-j]
    ex = value - sum(group[i])
    if ex != 0:
        group[i].append(X.pop(X.index(ex)))

group[-1] = X

LEN = []
LENpr = 0
for i in range(n):
    LEN.append(len(group[i]))
for i in range(n-1):
    LENpr += LEN[i] * sum(LEN[i+1:])

print(LENpr)

for i in range(n-1):
    for j in range(i+1,n):
        for k in range(LEN[i]):
            for l in range(LEN[j]):
                print(group[i][k],group[j][l])

Buggy submission (verdict: Runtime Error):
N = int(input())
X = list(range(1,N+1))
Nsum = sum(X)

n = 2 
while Nsum % n != 0:
    n += 1

group = [[] for i in range(n)]
value = int(Nsum/n)

for i in range(n-1):
    for j in range(N):
        if sum(X[-j-1:]) > value:
            break
    group[i] = X[-j:]
    X = X[:-j]
    ex = value - sum(group[i])
    if ex != 0:
        group[i].append(X.pop(ex))

group[-1] = X

LEN = []
LENpr = 0
for i in range(n):
    LEN.append(len(group[i]))
for i in range(n-1):
    LENpr += LEN[i] * sum(LEN[i+1:])

print(LENpr)

for i in range(n-1):
    for j in range(i+1,n):
        for k in range(LEN[i]):
            for l in range(LEN[j]):
                print(group[i][k],group[j][l])
Problem p03096 (python)

Fixed submission:
N = int(input())
C = dict()
i, cp, hist, ans = 0, 0, [0]*N, 0
for _ in range(N):
    c = int(input())
    if c != cp:
        if c in C.keys():
            temp = hist[C[c]] + 1
            hist[i] = hist[i-1] + temp
            ans += temp
        else:
            hist[i] += hist[i-1]
        C[c], cp = i, c
        i += 1
print((ans+1) % (10**9+7))

Buggy submission (verdict: Runtime Error):
N = int(input())
C = dict()
i, cp, lr = 0, 0, []
for _ in range(N):
    c = int(input())
    if c != cp:
        if c in C.keys():
            lr.append([C[c], i])
        C[c] = i
        cp = c
        i += 1
length = i
lr.sort()
hist, add, i, ans = [0] * length, [0] * length, 0, 0
for l, r in lr:
    while i <= l:
        hist[i] = hist[i-1] + add[i]
        ans += add[i]
        i += 1
    add[r] += hist[l] + 1
for i in range(l+1, length):
    ans += add[i]
print((ans+1) % (10**9+7))

Buggy submission (verdict: Runtime Error):
N = int(input())
C = dict()
i, cp, lr = 0, 0, []
for _ in range(N):
    c = int(input())
    if c != cp:
        if c in C.keys():
            lr.append([C[c], i])
        C[c] = i
        cp = c
        i += 1
lr.sort()
hist, add, i, ans = [0] * N, [0] * N, 0, 0
for l, r in lr:
    while i <= l:
        hist[i] = hist[i-1] + add[i]
        ans += add[i]
        i += 1
    add[r] += hist[l] + 1
for i in range(l+1, N):
    ans += add[i]
print(ans+1)
Problem p03112 (python)

Fixed submission:
A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
query, X = [], {}

for i in range(Q):
    q = int(input())
    query.append(q)
    X[q] = i
query.sort()

near_s = [0 for _ in range(Q)]
near_t = [0 for _ in range(Q)]
j = 0
while j < Q and query[j] < S[0]:
    near_s[j] = [0, S[0]]
    j += 1    
for i in range(A-1):
    while j < Q and S[i] < query[j] < S[i+1]:
        near_s[j] = [S[i], S[i+1]]
        j += 1
near_s = near_s[:j] + [[S[A-1],0] for _ in range(Q-j)]

j = 0
while j < Q and query[j] < T[0]:
    near_t[j] = [0, T[0]]
    j += 1
for i in range(B-1):
    while j < Q and T[i] < query[j] < T[i+1]:
        near_t[j] = [T[i], T[i+1]]
        j += 1
near_t = near_t[:j] + [[T[B-1],0] for _ in range(Q-j)]

ans = [0]*Q
for i in range(Q):
    q, s0, s1, t0, t1 = query[i], near_s[i][0], near_s[i][1], near_t[i][0], near_t[i][1]
    l1 = q - min(s0, t0)
    l2 = max(s1, t1) - q
    l3 = s1 - t0 + min(s1 - q, q - t0)
    l4 = t1 - s0 + min(t1 - q, q - s0)
    if s0 == 0:
        l1, l4 = float('inf'), float('inf')
    if s1 == 0:
        l2, l3 = float('inf'), float('inf')
    if t0 == 0:
        l1, l3 = float('inf'), float('inf')
    if t1 == 0:
        l2, l4 = float('inf'), float('inf')
    ans[X[q]] = min(l1, l2, l3, l4)

for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
query, X = [], {}

for i in range(Q):
    q = int(input())
    query.append(q)
    X[q] = i
query.sort()

near_s = [0 for _ in range(Q)]
near_t = [0 for _ in range(Q)]
j = 0
while j < Q and query[j] < S[0]:
    near_s[j] = [0, S[0]]
    j += 1    
for i in range(A-1):
    while j < Q and S[i] < query[j] < S[i+1]:
        near_s[j] = [S[i], S[i+1]]
        j += 1
near_s = near_s[:j] + [[S[A-1],0] for _ in range(Q-j)]

j = 0
while j < Q and query[j] < T[0]:
    near_t[j] = [0, T[0]]
    j += 1
for i in range(B-1):
    while j < Q and T[i] < query[j] < T[i+1]:
        near_t[j] = [T[i], T[i+1]]
        j += 1
near_t = near_t[:j] + [[T[B-1],0] for _ in range(Q-j)]

ans = [0]*Q
for i in range(Q):
    q, s0, s1, t0, t1 = query[i], *near_s[i], *near_t[i]
    l1 = q - min(s0, t0)
    l2 = max(s1, t1) - q
    l3 = s1 - t0 + min(s1 - q, q - t0)
    l4 = t1 - s0 + min(t1 - q, q - s0)
    if s0 == 0:
        l1, l4 = float('inf'), float('inf')
    if s1 == 0:
        l2, l3 = float('inf'), float('inf')
    if t0 == 0:
        l1, l3 = float('inf'), float('inf')
    if t1 == 0:
        l2, l4 = float('inf'), float('inf')
    ans[X[q]] = min(l1, l2, l3, l4)

for a in ans:
    print(a)