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

Fixed submission:
from collections import deque

def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    
    ans = [0]*n
    for _ in range(q):
        p, x = map(int, input().split())
        ans[p-1] += x
    
    not_yet = deque([0])
    already = [False]*n
    already[0] = True
    
    while not_yet:
        key = not_yet.popleft()
        for value in tree[key]:
            if already[value]:
                continue
            already[value] = True
            not_yet.append(value)
            ans[value] += ans[key]
    
    print(*ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
tree = {}
for i in range(n-1):
    a, b = map(int, input().split())
    tree[b] = a
ans = [0]*n
for i in range(q):
    a, b = map(int, input().split())
    ans[a-1] += b
for i in range(2, n+1):
    ans[i-1] += ans[tree[i]-1]
for i in ans:
    print(i, end=" ")

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
tree = {}
for i in range(n-1):
    a, b = map(int, input().split())
    tree[b] = a
ans = [0]*n
for i in range(q):
    a, b = map(int, input().split())
    ans[a-1] += b
for i in range(2, n+1):
    ans[i-1] += ans[tree[i]-1]
print(ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input())
tree = {}
for i in range(n):
    a, b = map(int, input())
    tree[b] = a
ans = [0]*n
for i in range(q):
    a, b = map(int)
    ans[a-1] += b
for i in tree:
    ans[i] += ans[tree[i]]
print(ans)
Problem p02939 (python)

Fixed submission:
s = input()
n = len(s)

bef = s[0]
key = 0
ans = 1
for i in range(1, n):
  if key == i:
    continue
  if bef == s[i]:
    if i == n-1:
      continue
    bef = s[i:i+2]
    ans += 1
    key = i+1
  else:
    bef = s[i]
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
ans = 1
key = 0
for i in range(len(s)-1):
	if s[i] != s[i+1] or key == 1:
        ans += 1
        key = 0
    else:
        key = 1
print(ans)
Problem p02947 (python)

Fixed submission:
N = int(input())
s = [0]*N
p = {}
ans = 0
for i in range(N):
    s[i] = "".join(sorted(input()))
    if s[i] in p:
        ans += p[s[i]]
        p[s[i]] += 1
    else:
        p[s[i]] = 1

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input)
ans = {}
for i in range(n):
    s = input()
    t = []
    for i in range(len(s)):
        t.append(s[i])
    t.sort()
    k = "".join(t)
    if k in ans:
        ans[k] = ans[k]+1
    else:
        ans[k] = 0
answer = 0
for i in ans:
    j = ans[i]
    answer += j*(j+1)//2
print(answer)
Problem p02953 (python)

Fixed submission:
N = int(input())
H = [int(x) for x in input().split()]
j = 0
Ans = True

for i in range(N-1):
    if H[i] > H[i+1] + 1:
        Ans = False
    elif H[i] == H[i+1] + 1:
        if H[i-j] == H[i]:
            j = 1
            continue
        else:
            Ans = False
            break
    elif H[i] == H[i+1]:
        j += 1
    else:
        j = 0
        continue

if Ans:
    print("Yes")
else:
    print("No")

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

judge = 0
for i in range(n):
  if a[i] < judge:
    print("No")
    break
  judge = max(judge, a[i]-1)
else:
  print("Yes")
Problem p02972 (python)

Fixed submission:
N = int(input())
a = [int(x) for x in input().split()]


answer = []
B = 0

for i in range(N):
    U = N - i
    L = N // (U)
    if L == 1:
        answer.append(a[U-1])
    else:
        for j in range(L-1):
            t = U * (j+1)
            B += answer[-t]
        B = B % 2
        if B == a[U-1]:
            answer.append(0)
        else:
            answer.append(1)
    B = 0


k = 0



answer.reverse()
print(answer.count(1))
J = len(answer)
for i in range(J):
    if answer[i] == 1:
        print(i+1, end=' ')

Buggy submission (verdict: Runtime Error):
N = int(input())
a = [int(x) for x in input().split()]


answer = []
B = 0

for i in range(N):
    L = N // (N-i)
    if L == 1:
        answer.append(a[N-i-1])
    else:
        for j in range(L-1):
            t = (N-i) * j
            B += answer[t]
        B = B % 2
        if B == a[N-i-1]:
            answer.append(0)
        else:
            answer.append(1)
    B = 0


k = 0
P = len(answer)
while (answer[0] == 0) and (k < P-1):
    answer.remove(0)
    k += 1

if answer == [0]:
    print(0)
else:
    answer.reverse()
    print(answer.count(1))
    for i in range(J):
        if answer[i] == 1:
            print(i+1, end=' ')
Problem p02991 (python)

Fixed submission:
import sys
from collections import deque

def main():
  input = sys.stdin.readline
  n, m = map(int, input().split())
  roads0 = [[] for _ in range(n)]
  roads1 = [[] for _ in range(n)]
  roads2 = [[] for _ in range(n)]
  
  for i in range(m):
    u, v = map(int, input().split())
    roads0[u-1].append(v-1)
    roads1[u-1].append(v-1)
    roads2[u-1].append(v-1)
  

  s, t = map(int, input().split())
  not_yet = deque([])
  already = [[False]*3 for _ in range(n)]
  dist = [[0]*3 for _ in range(n)]
  for v in roads0[s-1]:
    not_yet.append((v, 1))
    dist[v][1] = 1
    already[v][1] = True
  already[s-1][0] = True
  while not_yet:
    key = not_yet.popleft()
    k1, k2 = key[0], key[1]
    if k2 == 0:
      for v in roads1[k1]:
        if already[v][1]:
          continue
        dist[v][1] = dist[k1][0]+1
        already[v][1] = True
        not_yet.append((v, 1))
    elif k2 == 1:
      for v in roads2[k1]:
        if already[v][2]:
          continue
        dist[v][2] = dist[k1][1]+1
        already[v][2] = True
        not_yet.append((v, 2))
    else:
      for v in roads0[k1]:
        if already[v][0]:
          continue
        dist[v][0] = dist[k1][2]+1
        already[v][0] = True
        not_yet.append((v, 0))
  if already[t-1][0]:
    print(dist[t-1][0]//3)
  else:
    print(-1)
  
    
if __name__ == "__main__":
  main()

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

def main():
  input = sys.stdin.readline
  n, m = map(int, input().split())
  roads0 = [[] for _ in range(n)]
  roads1 = [[] for _ in range(n)]
  roads2 = [[] for _ in range(n)]
  
  for i in range(m):
    u, v = map(int, input().split())
    roads0[u-1].append(v-1)
    roads1[u-1].append(v-1)
    roads2[u-1].append(v-1)
    
  ans = 100000000
  
  s, t = map(int, input().split())
  not_yet = deque([])
  already = [[False]*3 for _ in range(n)]
  dist = [[0]*3 for _ in range(n)]
  for v in roads0[s-1]:
    not_yet.append((v, 1))
    dist[v][1] = 1
    already[v][1] = True
  already[s-1][0] = True
  while not_yet:
    key = not_yet.pop()
    k1, k2 = key[0], key[1]
    if k2 == 0:
      for v in roads1[k2]:
        if already[v][1]:
          continue
        dist[v][1] = dist[k1][0]+1
        already[v][1] = True
        not_yet.append((v, 1))
    elif k2 == 1:
      for v in roads2[k2]:
        if already[v][2]:
          continue
        dist[v][2] = dist[k1][1]+1
        already[v][2] = True
        not_yet.append((v, 2))
    else:
      for v in roads3[k2]:
        if already[v][0]:
          continue
        dist[v][0] = dist[k1][2]+1
        already[v][0] = True
        not_yet.append((v, 0))
  if already[t-1][0]:
    print(dist[t-1][0])
  else:
    print(-1)

    
if __name__ == "__main__":
  main()
Problem p03006 (python)

Fixed submission:
import sys
from collections import deque
input = sys.stdin.readline

def main():
  n = int(input())
  ball = [tuple(map(int, input().split())) for i in range(n)]

  ans = n
  for i in range(n):
    for j in range(n):
      p, q = ball[i][0] - ball[j][0], ball[i][1] - ball[j][1]
      par = [-1]*n
      node = [[] for i in range(n)]
      for s in range(n):
        for t in range(n):
          x, y = ball[s][0] - ball[t][0], ball[s][1] - ball[t][1]
          if x == p and y == q:
            node[s].append(t)
            node[t].append(s)
      already = [False]*n
      count = 0
      for k in range(n):
        if already[k]:
          continue
        count += 1
        already[k] = True
        not_yet = deque([k])
        while not_yet:
          key = not_yet.pop()
          for v in node[key]:
            if already[v]:
              continue
            already[v] = True
            not_yet.append(v)
      if ans > count:
        ans = count
  print(ans)

  
main()

Buggy submission (verdict: Runtime Error):
n = int(input())

def find(x, par):
  if par[x] < 0:
    return x
  
  par[x] = find(par[x], par)
  return par[x]

def union(x, y, par):
  if find(x, par) == find(y, par):
    return
  
  if par[par[x]] > par[par[y]]:
    x, y = y, x
  par[x] += par[par[y]]
  par[y] = x


def main():
  ball = [tuple(map(int, input().split())) for i in range(n)]

  ans = n
  for i in range(n):
    for j in range(n):
      p, q = ball[i][0] - ball[j][0], ball[i][1] - ball[j][1]
      par = [-1]*n
      for s in range(n):
        for t in range(n):
          x, y = ball[s][0] - ball[t][0], ball[s][1] - ball[t][1]
          if x == p and y == q:
            union(s, t, par)
      judge = set()
      for k in range(n):
        judge.add(find(k, par))
      if ans > len(judge):
        ans = len(judge)
  print(ans)

  
main()
Problem p03014 (python)

Fixed submission:
h, w = map(int, input().split())
grid = [0]*h
for i in range(h):
  grid[i] = input()

counter = [[[0, 0] for j in range(w)] for i in range(h)]
ans = 0
for i in range(h):
  for j in range(w):
    if grid[i][j] == "#":
      continue
    if i == 0 or (i != 0 and grid[i-1][j] == "#"):
      k = 0
      while i+k < h and grid[i+k][j] == ".":
        k += 1
      counter[i][j][0] = k-1
    else:
      counter[i][j][0] = counter[i-1][j][0]
    if j == 0 or (j != 0 and grid[i][j-1] == "#"):
      k = 0
      while j+k < w and grid[i][j+k] == ".":
        k += 1
      counter[i][j][1] = k-1
    else:
      counter[i][j][1] = counter[i][j-1][1]
    if ans < counter[i][j][0]+counter[i][j][1]:
      ans = counter[i][j][0]+counter[i][j][1]
print(ans+1)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
grid = [0]*n
for i in range(h):
  grid[i] = input()

counter = [[[0, 0]]*w for i in range(h)]
ans = 0
for i in range(h):
  for j in range(w):
    if grid[i][j] == "#":
      continue
    if i == 0 or grid[i-1][j] == "#":
      k = 0
      while grid[i+k][j] == "." and i+k < n:
        k += 1
      counter[i][j][0] = k
    else:
      counter[i][j][0] = counter[i-1][j][0]
    if j == 0 or grid[i][j-1] == "#":
      k = 0
      while grid[i][j+k] == "." and j+k < n:
        k += 1
      counter[i][j][1] = k
    else:
      counter[i][j][1] = counter[i][j-1][1]
    if ans < counter[i][j][0]+counter[i][j][1]:
      ans = counter[i][j][0]+counter[i][j][1]
print(ans+1)
Problem p03029 (python)

Fixed submission:
a, p = map(int, input().split())

k = 3 * a + p
print(k // 2)

Buggy submission (verdict: Runtime Error):
import sys
from heapq import *
input = sys.stdin.readline

def main():
    n, q = map(int, input().split())
    
    key = []
    for i in range(n):
        s, t, x = map(int, input().split())
        l, r = max(0, s-x), max(0, t-x)
        key.append((l, 1, x))
        key.append((r, -1, x))

    key.sort()
    judge = []
    now = set()
    already = dict()
    
    index = 0
    ans = -1
    for i in range(q):
        d = int(input())
        for i in range(index, 2*n):
            s, j, x = key[i]
            if s > d:
                index = i
                break
            if j == 1:
                heappush(judge, x)
                now.add(x)
            else:
                now.remove(x)
                if x in already:
                    already[x] += 1
                else:
                    already[x] = 1
                    
                    
        while judge:
            if judge[0] in already and already[judge[0]] > 0:
                already[judge[0]] -= 1
                heappop(judge)
            else:
                break
        if judge:
            ans = judge[0]
        else:
            ans = -1      
            
        print(ans)

    
if __name__ == "__main__":
    main()
Problem p03030 (python)

Fixed submission:
n = int(input())
key = []
for i in range(n):
  flag = True
  s, p = map(str, input().split())
  for j in range(len(key)):
    if key[j][0] == s:
      key[j][1].append([int(p), i+1])
      flag = False
      break
  if flag:
    key.append([s, [[int(p), i+1]]])
key.sort()
for i in range(len(key)):
  key[i][1].sort(reverse=True)
  for j in range(len(key[i][1])):
    print(key[i][1][j][1])

Buggy submission (verdict: Runtime Error):
N = int(input())
s = {}
for _, i in zip(range(N), range(N)):
    p, q = map(str, input().split)
    s[q] = i
    
s.sort()

for i in range(N):
    print(s[i])aN = int(input())
j = []
for _, i in zip(range(N), range(N)):
    p, q = map(str, input().split())
    j.append(int(q))

for i in range(N):
    p = max(j)
    q = j.index(p)
    print(q+1)
    j[q] = 0
Problem p03046 (python)

Fixed submission:
m, k = map(int, input().split())
if k >= 2**m:
  print(-1)
elif m == 0:
  print(0, 0)
elif m == 1:
  if k == 0:
    print(0, 0, 1, 1)
  else:
    print(-1)
else:
  for i in range(2**m):
    if i == k:
      continue
    print(i, end=" ")
  print(k, end=" ")
  for i in range(2**m-1, -1, -1):
    if i == k:
      continue
    print(i, end=" ")
  print(k)

Buggy submission (verdict: Runtime Error):
m, k = map(int, input().split())
if k >= 2**m:
  print(-1)
elif m == 0:
  print(0, 0)
elif m == 0:
  if k == 0:
    print(0, 0, 1, 1)
  else:
    print(-1)
else:
  for i in range(2**n):
    if i == k:
      continue
    print(i, end=" ")
  print(k, end=" ")
  for i in range(2**n-1, -1, -1):
    if i == k:
      continue
    print(i, end=" ")
  print(k)
Problem p03061 (python)

Fixed submission:
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p

n = int(input())
a = [int(x) for x in input().split()]

if n == 2:
    print(max(a))
else:
    x, y, z = a[0], a[1], a[2]
    p, q, r = gcd(x, y), gcd(y, z), gcd(z, x)
    gcd_max = max(p, q, r)
    if gcd_max == p:
        without = z
    elif gcd_max == q:
        without = x
    else:
        without = y

    k = gcd(gcd_max, without)   
    for i in range(3, n):
        a_i = a[i]
        if a_i % gcd_max != 0:
            l = gcd(gcd_max, a_i)
            if l >= k:
                gcd_max = l
            else:
                gcd_max = k
                without = a_i
        k = gcd(k, a_i)
    print(gcd_max)

Buggy submission (verdict: Runtime Error):
from math import gcd
n = int(input())
a = [int(x) for x in input().split()]

if n == 2:
    print(max(a))
else:
    x, y, z = a[0], a[1], a[2]
    p, q, r = gcd(x, y), gcd(y, z), gcd(z, x)
    gcd_max = max(p, q, r)
    if gcd_max == p:
        without = z
    elif gcd_max == q:
        without = x
    else:
        without = y

    k = gcd(gcd_max, without)   
    for i in range(3, n):
        a_i = a[i]
        if a_i % gcd_max != 0:
            l = gcd(gcd_max, a_i)
            if l >= k:
                gcd_max = l
            else:
                gcd_max = k
                without = a_i
        k = gcd(k, a_i)
    print(gcd_max)

Buggy submission (verdict: Runtime Error):
from math import gcd
n = int(input())
a = [int(x) for x in input().split()]

if n == 2:
    print(max(a))
else:
    
    x, y, z = a[0], a[1], a[2]
    p, q, r = gcd(x, y), gcd(y, z), gcd(z, x)
    gcd_max = max(p, q, r)
    if gcd_max == p:
        without = z
    elif gcd_max == q:
        without = x
    else:
        without = y

    k = gcd(gcd_max, without)   
    for i in range(3, n):
        a_i = a[i]
        if a_i % gcd_max != 0:
            l = gcd(gcd_max, a_i)
            if l >= k:
                gcd_max = l
            else:
                gcd_max = k
                without = a_i
        k = gcd(k, a_i)
print(gcd_max)
Problem p03074 (python)

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

key = [0]
for i in range(1, n):
  if s[i] != s[i-1]:
    key.append(i)
key.append(n)

m, r = 0, len(key)-1
ans = 0
while True:
  if m == 0 and s[0] == "0":
    p = m+2*k
    if ans < key[min([p, r])] - key[m]:
      ans = key[min([p, r])] - key[m]
    m += 1
  else:
    p = m+2*k+1
    if ans < key[min([p, r])] - key[m]:
      ans = key[min([p, r])] - key[m]
    m += 2
  if p >= r:
    break

print(ans)

Buggy submission (verdict: Runtime Error):
def main():
    n, k = map(int, input().split())
    s = input()
    sub = []
    count = 1
    for i in range(1, n):
        if s[i] == s[i-1]:
            count += 1
        else:
            sub.append(count)
            count = 1
    sub.append(count)
    
    key = s[0]
    
    if key:
        if 2*k+1 >= len(sub):
            a = n
        else:
            a = sum(sub[:(2*k+1)])
            key = 0
    else:
        if 2*k >= len(sub):
            a = n
        else:
            a = sum(sub[:(2*k)])
            key = 0
    ans = a
    flag = False
    for i in range(len(sub)):
        a -= sub[i]
        if key:
            if i+2*k == len(sub)-1:
                a += sub[i+2*k]
                flag = True
            key = 0
        else:
            if i+2*k == len(sub)-1:
                a += sub[i+2*k]
                flag = True
            else:
                a += sub[i+2*k]+sub[i+1+2*k]
            if i+2*k == len(sub)-2:
                flag = True
            key = 1
            
        if a > ans:
            ans = a
        if flag:
            break
    
    print(ans)

if __name__ == "__main__":
    main()

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

if s[0] == "1":
    s_first = 1
else:
    s_first = 0

S_count = []
t = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        t += 1
    else:
        S_count.append(t)
        t = 1
S_count.append(t)

t = len(S_count)
ans_list = []
if n-2*k <= 0:
    print(len(s))
else:    
    if s_first == 1:
        for i in range(t-2*k):
            ans_list.append(sum(S_count[i:i+2*k+1]))
    else:
        for i in range(t-2*k):
            ans_list.append(sum(S_count[i:i+2*k]))
    print(S_count)
    print(ans_list)
    ans = max(ans_list)
    print(ans)
Problem p03078 (python)

Fixed submission:
x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
p = []
for i in a:
    for j in b:
        p.append(i+j)
p.sort(reverse=True)
p = p[:min(3001, x*y*z+1)]
q = []
for j in c:
    for i in p:
        q.append(i+j)
q.sort(reverse=True)
for i in range(k):
    print(q[i])

Buggy submission (verdict: Runtime Error):
import heapq
x, y, z,  k = map(int, input().split())
a = list(int(x) for x in input().split())
b = list(int(x) for x in input().split())
c = list(int(x) for x in input().split())

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

ab_1 = []
for i in a:
    for j in b:
        heapq.heappush(ab_1, -i-j)
key = min([k, x*y*z])
ab_2 = []
for i in range(key):
    value = heapq.heappop(ab_1)
    ab_2.append(-value)

abc = []
for p in ab_2:
    for q in c:
        heapq.heappush(abc, -p-q)
for i in range(key):
    value = heapq.heappop(abc)
    print(-value)

Buggy submission (verdict: Runtime Error):
x, y, z,  k = map(int, input().split())
a = list(int(x) for x in input().split())
b = list(int(x) for x in input().split())
c = list(int(x) for x in input().split())

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

ab_1 = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]*b[j])
ab_1.sort()   
key = min([k, x*y*z])
ab_2 = ab_1[:key]

abc = []
for i in range(key):
    for j in range(z):
        abc.append(ab_2[i]*c[j])
abc.sort()
for i in range(key):
    print(abc[i])
Problem p03086 (python)

Fixed submission:
s = input()
k = 0
ans = 0
for i in range(len(s)):
    if s[i] == "A" or s[i] == "G" or s[i] == "C" or s[i] == "T":
        k += 1
    else:
        ans = max(ans, k)
        k = 0

ans = max(ans, k)
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
k = 0
ans = 0
for i in range(s):
    if s[i] == "A" or "G" or "C" or "T":
        k += 1
    else:
        ans = max(ans, k)
        k = 0
ans = max(ans, k)
print(ans)
Problem p03110 (python)

Fixed submission:
N = int(input())
x = [0]*N
u = [0]*N
answer = 0
for i in range(N):
    x[i], u[i] = map(str, input().split())
    if u[i] == "JPY":
        answer += int(x[i])
    else:
        answer += (float(x[i]) * 380000)



print(answer)

Buggy submission (verdict: Runtime Error):
n = int(input())
otoshidama = {"JPY":0, "BTC":0}
for i in range(n):
    a, b = map(str, input().split())
    a = float(a)
    if b == "JPY":
        otoshidama["JPY"] = otoshidama["JPY"] + a
    else:
        otoshidama["BTC"] = otoshidama["BTC"] + a
ans = otochidama["JPY"] + otochidama["BTC"] * 380000.0
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
otoshidama = {JPY:0, BTC:0}
for i in range(n):
    a, b = map(str, input().split())
    a = float(a)
    if b == JPY:
        otoshidama[JPY] = otoshidama[JPY] + a
    else:
        otoshidama[BTC] = otoshidama[BTC] + a
ans = otochidama[JPY] + otochidama[BTC] * 380000.0
print(ans)
Problem p03112 (python)

Fixed submission:
import sys

def main():
  input = sys.stdin.readline
  a, b, q = map(int, input().split())
  s = [0]*a
  for i in range(a):
    s[i] = int(input())
  
  t = [0]*b
  for i in range(b):
    t[i] = int(input())

  s_near = [0]*a
  k = 0
  for i in range(a):
    if k == b-1:
      s_near[i] = abs(s[i]-t[b-1])
      continue
    while True:
      sub = min([abs(s[i]-t[k]), abs(s[i]-t[k+1])])
      if sub == abs(s[i]-t[k+1]):
        k += 1
        if k == b-1:
          s_near[i] = sub
          break
        continue
      else:
        s_near[i] = sub
        break
  
  t_near = [0]*b
  k = 0
  for i in range(b):
    if k == a-1:
      t_near[i] = abs(t[i]-s[a-1])
      continue
    while True:
      sub = min([abs(t[i]-s[k]), abs(t[i]-s[k+1])])
      if sub == abs(t[i]-s[k+1]):
        k += 1
        if k == a-1:
          t_near[i] = sub
          break
        continue
      else:
        t_near[i] = sub
        break
  
  
  for i in range(q):
    x = int(input())
    if x <= s[0]:
      sub1 = s[0]-x+s_near[0]
    elif x >= s[a-1]:
      sub1 = x-s[a-1]+s_near[a-1]
    else:
      l, r = 0, a
      while r-l > 1:
        k = (r+l)//2
        if x <= s[k]:
          r = k
        else:
          l = k
      sub1 = min([s[r]-x+s_near[r], x-s[l]+s_near[l]])
    
    
    if x <= t[0]:
      sub2 = t[0]-x+t_near[0]
    elif x >= t[b-1]:
      sub2 = x-t[b-1]+t_near[b-1]
    else:
      l, r = 0, b
      while r-l > 1:
        k = (r+l)//2
        if x <= t[k]:
          r = k
        else:
          l = k
      sub2 = min([t[r]-x+t_near[r], x-t[l]+t_near[l]])
    ans = min([sub1, sub2])
    print(ans)


if __name__ == "__main__":
  main()

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

def main():
  input = sys.stdin.readline
  inf = pow(10, 18)
  a, b, q = map(int, input().split())
  s = [-inf]+[int(input()) for _ in range(a)]+[inf]
  t = [-inf]+[int(input()) for _ in range(b)]+[inf]
  
  for _ in range(q):
    x = int(input())
    i = bisect.bisect_left(s, x)
    j = bisect.bisect_left(t, x)
    ans = inf
    for l in range(2):
      for m in range(2):
        sub = min([abs(x-s[i+l]), abs(x-t[j+m])])+abs(s[i+l]-t[j+m])
        if ans > sub:
          ans = sub
    print(ans)

if __name__ == "__main__":
  main()