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

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort()
b.sort()
c.sort()

def isok(na , index , key):
    if na[index] >= key:
        return True
    else:
        return False
    
def bs(nar,key):
    left = -1
    right = len(nar)
    
    while (right - left) > 1:
        mid = left + (right - left) // 2
        
        if isok(nar , mid , key):
            right = mid
        else:
            left = mid
            
    return right

ans = 0
for i in b:
    q = bs(a,i)
    r = bs(c,i+1)
    
    ans += q*(n-r)
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ab = [0] * (max(a) + 1)
bc = [0] * (max(b) + 1)

def isok(na , index , key):
    if na[index] >= key:
        return True
    else:
        return False
    
def bs(nar,key):
    left = -1
    right = len(nar)
    
    while (right - left) > 1:
        mid = left + (right - left) // 2
        
        if isok(nar , mid , key):
            right = mid
        else:
            left = mid
            
    return right

ans = 0
for i in b:
    q = bs(a,i)
    r = bs(c,i+1)
    
    ans += q*(n-r)
print(ans)
Problem p03628 (python)

Fixed submission:
n = int(input())
s = input()
s = s + "0"
s2 = input()
MOD = 10**9 + 7

al = []
a0 = 1
for i in range(n):
    if s[i] != s[i+1]:
        al.append(a0)
        a0 = 1
    else:
        a0 += 1

if al[0] == 1:
    ans = 3
else:
    ans = 6

for i in range(len(al)-1):
    if al[i] == 1:
        ans = ans * 2 % MOD
    else:
        if al[i+1] == 2:
            ans = ans * 3 % MOD
            
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
s = s + "0"
s2 = input()
MOD = 10**9 + 7

al = []
for i in range(n):
    if s[i] != s[i+1]:
        al.append(a0)
        a0 = 1
    else:
        a0 += 1

if al[0] == 1:
    ans = 3
else:
    ans = 6

for i in range(len(al)-1):
    if al[i] == 1:
        ans = ans * 2 % MOD
    else:
        if al[i+1] == 2:
            ans = ans * 3 % MOD
            
print(ans)
Problem p03633 (python)

Fixed submission:
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

n = int(input())
t = [0]*n
for i in range(n):
    t[i] = int(input())
    
ans = t[0]
    
for i in range(1,n):
    ans = ans * t[i] // gcd(ans , t[i])
    
print(ans)

Buggy submission (verdict: Runtime Error):
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

n = int(input())
t = [0]*n
for i in range(n):
    t[i] = int(input())
    
ans = t[0]
    
for i in range(1,n):
    ans = int(ans * t[i] / gcd(ans , t[i]))
    
print(ans)

Buggy submission (verdict: Runtime Error):
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

n = int(input())
t = [0]*n
for i in range(n):
    t[i] = int(input())
    
ans = t[0]
    
for i in range(n-1):
    ans = int(ans * t[i+1] / gcd(ans , t[i+1]))
    
print(int(ans))
Problem p03721 (python)

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

for _ in range(n):
    a , b = map(int,input().split())
    dic.setdefault(a , 0)
    dic[a] += b
    
sdic = sorted(dic.items(), key = lambda x : x[0])

for i in range(n):
    k -= sdic[i][1]
    
    if k <= 0:
        print(sdic[i][0])
        break

Buggy submission (verdict: Runtime Error):
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

n = int(input())
t = [0]*n
for i in range(n):
    t[i] = int(input())
    
ans = t[0]
    
for i in range(1,n):
    ans = ans * t[i] // gcd(ans , t[i])
    
print(ans)
Problem p03818 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
a = a + [0]

a.sort(reverse = True)

b = 1
ichi = 0
r = []
for i in range(n):
    if a[i] != a[i+1]:
        if b != 1:
            r.append(b)
        else:
            ichi += 1
        b = 1
    else:
        b += 1

for i in range(len(r)-1):
    if r[i] % 2 == 0:
        r[i] = 1
        r[i+1] -= 1
    else:
        r[i] = 1
        
if r != []:
    if r[-1] % 2 == 0:
        print(ichi + len(r) - 1)
    else:
        print(ichi + len(r))
        
else:
    print(ichi)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
a = a + [0]

a.sort(reverse = True)

b = 1
ichi = 0
r = []
for i in range(n):
    if a[i] != a[i+1]:
        if b != 1:
            r.append(b)
        else:
            ichi += 1
        b = 1
    else:
        b += 1

for i in range(len(r)-1):
    if r[i] % 2 == 0:
        r[i] = 1
        r[i+1] -= 1
    else:
        r[i] = 1
        
if r[-1] % 2 == 0:
    print(ichi + len(r) - 1)
else:
    print(ichi + len(r))
Problem p02553 (python)

Fixed submission:
a, b, c, d = [int(x) for x in input().split(" ")]
print(max([a*c, a*d, b*c, b*d]))

Buggy submission (verdict: Runtime Error):
a, b, c, d = [int(x) for x in input().split(" ")]
print(max([a*c a*d, b*c, b*d]))

Buggy submission (verdict: Runtime Error):
a, b, c, d = [int(x) for x in input().split(" ")]
print(max(a*c a*d, b*c, b*d))

Buggy submission (verdict: Runtime Error):
a, b, c, d = [int(x) for x input().split(" ")]
print(max(a*c a*d, b*c, b*d))
Problem p02817 (python)

Fixed submission:
print("".join(input().split(" ")[::-1]))

Buggy submission (verdict: Runtime Error):
import math
 
def main():
  N = int(input())
  
  print(search(N))
 
def search(x):
  
  ints = list(range(2,x))
  
  # 順に確認する
  for i in ints:
    if x%i==0:
      seartch(x+1)
  	elif x < i**2:
      break
    
  return x

Buggy submission (verdict: Runtime Error):
import math
 
def main():
  N = int(input())
  
  if N==2:
    print(2)
  else:
    print(search(N))
 
# xは奇数を前提
def search(x):
  
  # 2は確認不要
  ints = list(range(3,x))
  
  # 順に確認する
  for i in ints:
    
    
    if x%i==0:
      seartch(x+2)
  	elif x < i**2:
      break
    
  return x

main()
Problem p02850 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
 
def colorize(current_node, adjacents, colors, from_color, k):
    
    # そのノードから繋がるエッジを辿っていく
    count = 0
    for edge, node in adjacents[current_node]:
 
        # 既に塗られていたら無視
        if colors[edge]!=None:
            continue
        
        count += 1
        colors[edge] = (from_color+count)%k
        colorize(node, adjacents, colors, colors[edge], k)
     
N = int(input())
adjacents = {i+1:[] for i in range(N)}
 
for i in range(N-1):
    a,b = [int(e) for e in input().split(" ")]
    
    adjacents[a].append( [i+1,b] )
    adjacents[b].append( [i+1,a] )
 
# 一番隣接エッジ数が多いノード
most_edge =sorted(adjacents, key=lambda d:len(adjacents[d]))[-1]
k = len(adjacents[most_edge])
 
colors = {i+1:None for i in range(N-1)}
colorize(1, adjacents, colors, -1, k)
 
print(k)
for color in colors.values():
    print(color+1)

Buggy submission (verdict: Runtime Error):
def colorize(current_node, adjacents, colors, from_color, k):
    
    # そのノードから繋がるエッジを辿っていく
    count = 0
    for edge, node in adjacents[current_node]:

        # 既に塗られていたら無視
        if colors[edge]!=None:
            continue
        
        count += 1
        colors[edge] = (from_color+count)%k
        colorize(node, adjacents, colors, colors[edge], k)
        
N = int(input())
adjacents = {i+1:[] for i in range(N)}

for i in range(N-1):
    a,b = [int(e) for e in input().split(" ")]
    
    adjacents[a].append( [i+1,b] )
    adjacents[b].append( [i+1,a] )

# 一番隣接エッジ数が多いノード
most_edge =sorted(adjacents, key=lambda d:len(adjacents[d]))[-1]
k = len(adjacents[most_edge])

colors = {i+1:None for i in range(N-1)}

colorize(1, adjacents, colors, -1, k)

print(k)

for color in colors.values():
    print(color+1)

Buggy submission (verdict: Runtime Error):
def colorize(current_node, adjacents, colors, from_color, k):
    
    # そのノードから繋がるエッジを辿っていく
    count = 0
    for edge, node in adjacents[current_node]:

        # 既に塗られていたら無視
        if colors[edge]!=None:
            continue
        
        count += 1
        colors[edge] = (from_color+count)%k
        colorize(node, adjacents, colors, colors[edge], k)
        
N = int(input())
adjacents = {i:[] for i in range(N)}

for i in range(N-1):
    a,b = [int(e) for e in input().split(" ")]
    
    adjacents[a].append( [i+1,b] )
    adjacents[b].append( [i+1,a] )

# 一番隣接エッジ数が多いノード
most_edge =sorted(adjacents, key=lambda d:len(adjacents[d]))[-1]
k = len(adjacents[most_edge])

colors = {i+1:None for i in range(N-1)}

colorize(1, adjacents, colors, -1, k)

print(k)

for color in colors.values():
    print(color+1)
Problem p02573 (python)

Fixed submission:
from collections import defaultdict
# your code goes here
from heapq import heappop ,heappush
def bfs(k):
    
    q=[k]
    vis[k]=1
    #p.append(k+1)
    while len(q):
        
        
        curr=heappop(q)
        p.append(curr+1)
        #print(vis, v[curr+1])
        for c in v[curr+1]:
            #print(c)
            if vis[c-1]==0:
                a.append(c)
                
                heappush(q,c-1)
                vis[c-1]=1
            
    
n,m=map(int,input().split())
v=defaultdict(list)
vis=[0]*n
p=[]

for i in range(m):
    a,b=map(int,input().split())
    v[a].append(b)
    v[b].append(a)
ans=0      
for i in range(n):
    if vis[i]==0:
        a=[i+1]
        bfs(i)
        ans=max(ans,len(a))
        #print(a)
    

print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
# your code goes here


v=defaultdict(list)
n,m=map(int,input().split())
for i in range(m):
    a,b=map(int,input().split())
    v[a-1].append(b-1)
    v[b-1].append(a-1)
    

	
	
	
    
    
vis=[0]*n

def dfs(node):
    vis[node]=1
    for j in v[node]:
        #print(j,v[node])
        if vis[j]==0:
            a.append(j+1)
            dfs(j)
ans=0      
for i in range(n):
    if vis[i]==0:
        a=[i]
        dfs(i)
        ans=max(ans,len(a))
    

print(ans)
Problem p02630 (python)

Fixed submission:
n = int(input())
m=list(map(int,input().split()))
d={}
k=0
for i in range(n):
    k+=m[i]
    if m[i] in d:
        d[m[i]]+=1
    else:
        d[m[i]]=1
for x in range(int(input())):
    
    b,c=list(map(int,input().split()))

    
    
    if b in d:
        k+=d[b]*(c-b)
        if c in d:
            d[c]+=d[b]
        else:
            d[c] = d[b]
        del d[b] 
    print(k)

Buggy submission (verdict: Runtime Error):
n = int(input())
m=list(map(int,input().split()))
d={}
k=0
for i in range(n):
    k+=m[i]
    if m[i] in d:
        d[m[i]]+=1
    else:
        d[m[i]]=1
for x in range(int(input())):
    
    b,c=list(map(int,input().split()))

    k+=d[b]*(c-b)
    
    if c in d:
        d[c]+=d[b]
    else:
        d[c] = d[b]
    del d[b] 
    print(k)
Problem p02642 (python)

Fixed submission:

n=int(input())
m=list(map(int,input().split()))
k=10**6+1
p=[0]*k
m.sort()
for i in range(1,n):
    if m[i]==m[i-1]:
        p[m[i]]=1
res=0
for i in range(n):
    if p[m[i]]==0:
        res+=1
    
    if i!=n-1 and m[i]!=m[i+1]:
        for j in range(m[i]*2,k,m[i]):
            p[j]=1
print(res)

Buggy submission (verdict: Runtime Error):

n=int(input())
m=list(map(int,input().split()))
k=10**6
p=[0]*k
m.sort()
for i in range(1,n):
    if m[i]==m[i-1]:
        p[m[i]]=1
res=0
for i in range(n):
    if p[m[i]]==0:
        res+=1
    
    for j in range(m[i]*2,k,m[i]):
        p[j]=1
print(res)
Problem p03356 (python)

Fixed submission:
n,m=list(map(int,input().split()))
p=list(map(int,input().split()))
from collections import defaultdict
from collections import deque
c=0
for i in range(n):
    if p[i]==i+1:
        c+=1
vis=[0]*n
def dfs(x):
    vis[x]=1
    a.add(x+1)
    
    di = deque()
    di.append(x)
    while di:
        now = di.popleft()
        for j in d[now]:
            if not vis[j]:
                vis[j] = 1
                a.add(j+1)
                di.append(j)
    
    
    for u in d[x]:
        if vis[u]==0:
            dfs(u)
d=defaultdict(list)
for i in range(m):
    a,b=list(map(int,input().split()))
    d[a-1].append(b-1)
    d[b-1].append(a-1)
ans=0
for i in range(n):
    if vis[i]==0:
        a=set()
        dfs(i)
        l=0
        z=0
        for j in a:
            if p[j-1] in a:
                z+=1
            if p[j-1]==j:
                l+=1
        ans=max(ans,c+z-l)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
p=list(map(int,input().split()))
from collections import defaultdict
c=0
for i in range(n):
    if p[i]==i+1:
        c+=1
vis=[0]*n
def dfs(x):
    vis[x]=1
    a.add(x+1)
    for u in d[x]:
        if vis[u]==0:
            dfs(u)
d=defaultdict(list)
for i in range(m):
    a,b=list(map(int,input().split()))
    d[a-1].append(b-1)
    d[b-1].append(a-1)
ans=0
for i in range(n):
    if vis[i]==0:
        a=set()
        dfs(i)
        l=0
        z=0
        for j in a:
            if p[j-1] in a:
                z+=1
            if p[j-1]==j:
                l+=1
        ans=max(ans,c+z-l)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
p=list(map(int,input().split()))
from collections import defaultdict
c=0
for i in range(n):
    if p[i]==i+1:
        c+=1
vis=[0]*n
def dfs(x):
    vis[x]=1
    a.append(x+1)
    for u in d[x]:
        if vis[u]==0:
            dfs(u)
d=defaultdict(list)
for i in range(m):
    a,b=list(map(int,input().split()))
    d[a-1].append(b-1)
    d[b-1].append(a-1)
ans=0
for i in range(n):
    if vis[i]==0:
        a=[]
        dfs(i)
        l=0
        z=0
        for j in a:
            if p[j-1] in a:
                z+=1
            if p[j-1]==j:
                l+=1
        ans=max(ans,c+z-l)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
p=list(map(int,input().split()))
from collections import defaultdict
c=0
for i in range(n):
    if p[i]==i+1:
        c+=1
vis=[0]*n
def dfs(x):
    vis[x]=1
    a.append(x+1)
    for i in d[x]:
        if vis[i]==0:
            dfs(i)
d=defaultdict(list)
for i in range(m):
    a,b=list(map(int,input().split()))
    d[a-1].append(b-1)
    d[b-1].append(a-1)
ans=0
for i in range(n):
    if vis[i]==0:
        a=[]
        dfs(i)
        l=0
        z=0
        for i in a:
            if p[i-1] in a:
                z+=1
            if p[i-1]==i:
                l+=1
        ans=max(ans,c+z-l)
print(ans)
Problem p03285 (python)

Fixed submission:
n = int(input())
i = 0
flag = False
while(7*i<=n):
  if (n-7*i)%4 == 0:
    flag = True
    break
  i+=1
if flag:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
n = int(input())
i = 0
flag = false
while(7*i<=n):
  if (n-7*i)%4 == 0:
    flag = true
    break
  i+=1
if flag:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
n = int(input())
i = 0
flag = false
while(7*i<=n):
  if (n-7*i)%4 == 0:
    flag = true
    break
if flag:
  print("Yes")
else:
  print("No")
Problem p03415 (python)

Fixed submission:
arr = []
for i in range(3):
    arr.append(input())
print(arr[0][0] + arr[1][1] + arr[2][2])

Buggy submission (verdict: Runtime Error):
for i in range(3):
    arr[i] = input()
print(arr[0][0] + arr[1][1] + arr[2][2])

Buggy submission (verdict: Runtime Error):
arr = (list(input().split('\r\n')))
print(arr[0][0] + arr[1][1] + arr[2][2])

Buggy submission (verdict: Runtime Error):
arr = (list(input().split('\r\n')))
print('{}',arr[0][0] + arr[1][1] + arr[2][2])
Problem p03039 (python)

Fixed submission:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int

def cmb(n, r, mod):
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

N = 2*10**5
g1 = [1, 1]
g2 = [1, 1]
inv = [0, 1]

for i in range(2, N+1):
    g1.append((g1[-1] * i) % MOD)
    inv.append((-inv[MOD % i] * (MOD//i)) % MOD)
    g2.append((g2[-1] * inv[-1]) % MOD)

def solve(N: int, M: int, K: int):
    ans = 0
    for i in range(1, M):
        ans += i * N**2 * (M-i)
        ans %= MOD
    for i in range(1, N):
        ans += i * M**2 * (N-i)
        ans %= MOD
    ans *= cmb(N*M-2, K-2, MOD)
    ans %= MOD
    print(ans)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, M, K)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int

def cmb(n, r, mod):
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

N = 10**5
g1 = [1, 1]
g2 = [1, 1]
inv = [0, 1]

for i in range(2, N+1):
    g1.append((g1[-1] * i) % MOD)
    inv.append((-inv[MOD % i] * (MOD//i)) % MOD)
    g2.append((g2[-1] * inv[-1]) % MOD)

def solve(N: int, M: int, K: int):
    ans = 0
    for i in range(1, M):
        ans += i * N**2 * (M-i)
        ans %= MOD
    for i in range(1, N):
        ans += i * M**2 * (N-i)
        ans %= MOD
    ans *= cmb(N*M-2, K-2, MOD)
    ans %= MOD
    print(ans)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, M, K)

if __name__ == '__main__':
    main()
Problem p03044 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(1000000)

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    col = [-1 for _ in range(N+1)]
    G = [[] for _ in range(N+1)]
    for i in range(N-1):
        G[u[i]].append((v[i], w[i]))
        G[v[i]].append((u[i], w[i]))

    def dfs(p, c, cnt):
        if cnt == N:
            return
        for np, dis in G[p]:
            if col[np] == -1:
                cnt += 1
                if dis%2 == 0:
                    col[np] = c
                    dfs(np, c, cnt)
                else:
                    nc = 1 - c
                    col[np] = nc
                    dfs(np, nc, cnt)

    col[1] = 0
    dfs(1, 0, 0)
    for elem in col[1:]:
        print(elem)


    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N-1)  # type: "List[int]" 
    v = [int()] * (N-1)  # type: "List[int]" 
    w = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(1000000)

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    col = [-1 for _ in range(N+1)]
    G = [[] for _ in range(N+1)]
    for i in range(N-1):
        G[u[i]].append((v[i], w[i]))
        G[v[i]].append((u[i], w[i]))

    def dfs(p, c, cnt):
        if cnt == N:
            return
        for np, dis in G[p]:
            if col[np] == -1:
                cnt += 1
                if dis%2 == 0:
                    col[np] = c
                else:
                    nc = 1 - c
                    col[np] = nc
                dfs(np, nc, cnt)

    col[1] = 0
    dfs(1, 0, 0)
    for elem in col[1:]:
        print(elem)


    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N-1)  # type: "List[int]" 
    v = [int()] * (N-1)  # type: "List[int]" 
    w = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()
Problem p03112 (python)

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
S = []
for _ in range(A):
    S.append(int(input()))
T = []
for _ in range(B):
    T.append(int(input()))

for _ in range(Q):
    x = int(input())
    sr = bisect.bisect_right(S, x)
    tr = bisect.bisect_right(T, x)
    tmpS = []
    tmpT = []
    if sr == A:
        tmpS.append(S[A-1])
    elif sr == 0:
        tmpS.append(S[sr])
    else:
        tmpS.append(S[sr])
        tmpS.append(S[sr-1])
    if tr == B:
        tmpT.append(T[B-1])
    elif tr == 0:
        tmpT.append(T[tr])
    else:
        tmpT.append(T[tr])
        tmpT.append(T[tr-1])
    ans = 10**16
    for s in tmpS:
        tmp = abs(x-s)
        tmp1 = 10**16
        for elemT in tmpT:
            tmp1 = min(tmp1, abs(elemT-s))
        ans = min(ans, tmp+tmp1)
    for t in tmpT:
        tmp = abs(x-t)
        tmp1 = 10**16
        for elemS in tmpS:
            tmp1 = min(tmp1, abs(elemS-t))
        ans = min(ans, tmp+tmp1)
    print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
from itertools import permutations
A, B, Q = map(int, input().split())
S = []
for _ in range(A):
    S.append(int(input()))
T = []
for _ in range(B):
    T.append(int(input()))

for _ in range(Q):
    x = int(input())
    sr = bisect.bisect_right(S, x)
    tr = bisect.bisect_right(T, x)
    c = []
    if sr == A:
        c.append([S[A-1], 0])
    elif sr == 0:
        c.append([S[sr], 0])
    else:
        c.append([S[sr], 0])
        c.append([S[sr-1], 0])
    if tr == B:
        c.append([T[B-1], 1])
    elif tr == 0:
        c.append([T[tr], 1])
    else:
        c.append([T[tr], 1])
        c.append([T[tl-1], 1])
    ans = 10**16
    for elem in permutations(c):
        tmp = 0
        flag = [False, False]
        now_pos = x
        flag_calc = True
        for pos, kind in elem:
            if flag[kind]:
                flag_calc = False
                break
            tmp += abs(pos-now_pos)
            now_pos = pos
            flag[kind] = True
            if flag[0] and flag[1]:
                break
        if flag_calc:
            ans = min(ans, tmp)
    print(ans)