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

Fixed submission:
N, S = map(int, input().split())
x = list(map(int, input().split()))
x.append(S)
x.sort()
diff = []
for i in range(N):
    diff.append(x[i + 1] - x[i])

# 最小公倍数を求める
def lcm(a, b):
    while b != 0:
        a, b = b, a % b
    return a

ans = diff[0]
for d in diff[1:]:
    ans = lcm(ans, d)
print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left
N, S = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
i = bisect_left(x, S)
if x[i] != S:
    x.insert(i, S)
    N += 1
diff = []
for i in range(N - 1):
    diff.append(x[i + 1] - x[i])

# 最小公倍数を求める
def lcm(a, b):
    while b != 0:
        a, b = b, a % b
    return a
ans = diff[0]
for d in diff[1:]:
    ans = lcm(ans, d)
print(ans)
Problem p03305 (python)

Fixed submission:
from heapq import heappop, heappush
n, m, s, t = map(int, input().split())
edge_a = [set() for _ in range(n + 1)]
edge_b = [set() for _ in range(n + 1)]
for _ in range(m):
    u, v, a, b = map(int, input().split())
    edge_a[u].add((v, a))
    edge_a[v].add((u, a))
    edge_b[u].add((v, b))
    edge_b[v].add((u, b))

def dijkstra(edge, s):
    sp = [10 ** 19] * (n + 1)
    sp[s] = 0
    q = [(0, s)]
    while q:
        dist, s = heappop(q)
        for t, w in edge[s]:
            if dist + w < sp[t]:
                sp[t] = dist + w
                heappush(q, (sp[t], t))
    return sp

sp_a = dijkstra(edge_a, s)
sp_b = dijkstra(edge_b, t)
dist = [int(sp_a[-1] + sp_b[-1])]
for i in range(n - 1, 0, -1):
    dist.append(min(dist[-1], int(sp_a[i] + sp_b[i])))

snuk = 10 ** 15
print(*(snuk - d for d in reversed(dist)), sep='\n')

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse import csr_matrix, csgraph
n, m, s, t = map(int, input().split())
src = [list(map(int, input().split())) for _ in range(m)]
u, v, a, b = zip(*src)
pa = csgraph.dijkstra(csr_matrix((a, (u, v)), (n + 1, n + 1), dtype=np.int32),
                      False, s)
pb = csgraph.dijkstra(csr_matrix((b, (u, v)), (n + 1, n + 1), dtype=np.int32),
                      False, t)

dist = [int(pa[-1] + pb[-1])]
for i in range(n - 1, 0, -1):
    dist.append(min(dist[-1], int(pa[i] + pb[i])))

snuk = 10 ** 15
print(*(snuk - d for d in reversed(dist)), sep='\n')
Problem p03339 (python)

Fixed submission:
N = int(input())
S = input()
sum_s = [0] * N
sum_s[0] = 1 if S[0] == 'W' else 0
for i in range(1, N):
    if S[i] == 'W':
        sum_s[i] = sum_s[i - 1] + 1
    else:
        sum_s[i] = sum_s[i - 1]

lc = 0
rc = (N - 1) - (sum_s[N - 1] - sum_s[0])
ans = lc + rc

for i in range(1, N):
    lc = sum_s[i - 1]
    rc = (N - (i + 1)) - (sum_s[N - 1] - sum_s[i])

    ans = min(ans, lc + rc)

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
sum_s = [0] * N
# [0, i) のWの数
for i in range(1, N + 1):
    if S[i - 1] == 'W':
        sum_s[i] = sum_s[i - 1] + 1
    else:
        sum_s[i] = sum_s[i - 1]
        
ans = sum_s[N + 1]
for i in range(0, N):
    lc = sum_s[i]
    rc = sum_s[N + 1] - sum_s[i + 1] 
    min(ans, lc + rc)

print(ans)
Problem p03447 (python)

Fixed submission:
x, a, b = [int(input()) for _ in range(3)]
print((x - a) % b)

Buggy submission (verdict: Runtime Error):
print((x - a) % b)
Problem p03490 (python)

Fixed submission:
s = input()
x, y = map(int, input().split())
m = list(map(len, s.split('T')))
# 最初は右に行くしかない
dp_x = {m[0]}
for i in m[2::2]:
    new_dp = set()
    for v in dp_x:
        new_dp |= {v + i, v - i}
    dp_x = new_dp

dp_y = {0}
for i in m[1::2]:
    new_dp = set()
    for v in dp_y:
        new_dp |= {v + i, v - i}
    dp_y = new_dp

print('Yes' if x in dp_x and y in dp_y else 'No')

Buggy submission (verdict: Runtime Error):
s = input()
x, y = map(int, input().split())
m = list(map(len, s.split('T')))
# 最初は右に行くしかない
dp_x = {m[0]}
for i in m[2::2]:
    new_dp = set()
    for v in dp_x:
        new_dp += {v + i, v - i}
    dp_x = new_dp

dp_y = {0}
for i in m[1::2]:
    new_dp = set()
    for v in dp_y:
        new_dp += {v + i, v - i}
    dp_y = new_dp

print('Yes' if x in dp_x and y in dp_y else 'No')

print(dp_x)
print(dp_y)
Problem p03501 (python)

Fixed submission:
N, A, B = map(int, input().split())
print(min(A * N, B))

Buggy submission (verdict: Runtime Error):
N, C = map(int, input().split())
r = [[0] * C for _ in range(10 * 5)]
for _ in range(N):
    s, t, c = map(int, input().split())
    for i in range(s - 1, t):
        r[i][c - 1] = 1
ans = 0
for t in r:
    sum_t = sum(t)
    if sum_t > ans:
        ans = sum_t

print(ans)
Problem p03504 (python)

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

r = [[0 for i in range(c)] for j in range(100000)]  # テレビ局の番組表
for dummy in range(n):  # 入力
    s, t, c = map(int, input().split())
    for j in range(s - 1, t):
        r[j][c - 1] = 1  # 放送中なら1

ans = 0
for i in range(100000):
    if sum(r[i]) > ans:
        ans = sum(r[i])  # 同時に放送されている番組の最大数

print(ans)

Buggy submission (verdict: Runtime Error):
N, C = map(int, input().split())
r = [[0 for _ in range(C)] for _ in range(10 * 5)]

for _ in range(N):
    s, t, c = map(int, input().split())
    for i in range(s - 1, t):
        r[i][c - 1] = 1
ans = 0
for t in r:
    sum_t = sum(t)
    if sum_t > ans:
        ans = sum_t

print(ans)

Buggy submission (verdict: Runtime Error):
N, C = map(int, input().split())
r = [[0] * C for _ in range(10 * 5)]
for _ in range(N):
    s, t, c = map(int, input().split())
    for i in range(s - 1, t):
        r[i][c - 1] = 1
ans = 0
for t in r:
    sum_t = sum(t)
    if sum_t > ans:
        ans = sum_t

print(ans)

Buggy submission (verdict: Runtime Error):
N, C = map(int, input().split())
r = [[0] * C for _ in range(10 * 5)]
for _ in range(N):
    s, t, c = map(int, input().split())
    for i in range(s - 1, t):
        r[j][c - 1] = 1
ans = 0
for t in r:
    sum_t = sum(t)
    if sum_t > ans:
        ans = sum_t

print(ans)
Problem p03569 (python)

Fixed submission:
import re
s = input()
pa = s.replace('x', '')
if pa != pa[::-1]:
    print(-1)
else:
    ans = 0
    sp = re.split(r'[a-wyz]', s)
    for r, l in zip(sp, sp[::-1]):
        ans += abs(len(r) - len(l))
    print(ans // 2)

Buggy submission (verdict: Runtime Error):
import re
s = input()
pa = s.replace('x', '')
if pa != pa[::-1]:
    print(-1)
else:
    ans = 0
    sp = s.split(pa[len(pa) // 2])
    for r, l in zip(re.split(r'[a-wyz]', sp[0]),
                    re.split(r'[a-wyz]', sp[-1])[::-1]):
        ans += abs(len(r) - len(l))
    print(ans)
Problem p03711 (python)

Fixed submission:
a, b = map(int, input().split())
g1, g2, g3 = {1, 3, 5, 7, 8, 10, 12}, {4, 6, 9, 11}, {2}
print('Yes' if not ({a, b} - g1) or not ({a, b} - g2) else 'No')

Buggy submission (verdict: Runtime Error):
g1, g2, g3 = {1, 3, 5, 7, 8, 10, 12}, {4, 6, 9, 11}, {2}
print('Yes' if {a, b} in g1 or {a, b} in g2 else 'No')
Problem p03777 (python)

Fixed submission:
a, b = input().split()
if a == 'H':
    print(b)
else:
    if b == 'H':
        print('D')
    else:
        print('H')

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())
if a == 'H':
    print(b)
else:
    if b == 'H':
        print('D')
    else:
        print('H')
Problem p03782 (python)

Fixed submission:
# https://beta.atcoder.jp/contests/arc070/submissions/2980516
from bisect import bisect_left

N, K = map(int, input().split())
A = sorted(map(int, input().split()))
A = A[:bisect_left(A, K)]
dp = [False] * K
dp[0] = True
curr_max = 0
ans = len(A)
for i, a in reversed(list(enumerate(A))):
    # a[i - 1]を使ってa[i]が必要ならa[i - 1]も必要
    # a[i]より大きな値を使って判定すればいい
    if curr_max + a >= K:
        ans = i

    f = True
    for j in range(min(curr_max, K - a - 1), -1, -1):
        if dp[j]:
            dp[j + a] = True
            if f:
                curr_max = max(curr_max, j + a)
                f = False
print(ans)

Buggy submission (verdict: Runtime Error):
# https://beta.atcoder.jp/contests/arc070/submissions/2980516
from bisect import bisect_left

N, K = map(int, input().split())
A = sorted(map(int, input().split()))
A = A[:bisect_left(A, K)]
dp = [False] * K
dp[0] = True
curr_max = 0
ans = len(A)
for i, a in reversed(enumerate(A)):
    # a[i - 1]を使ってa[i]が必要ならa[i - 1]も必要
    # a[i]より大きな値を使って判定すればいい
    if curr_max + a >= K:
        ans = i

    f = True
    for j in range(min(curr_max, K - a - 1), -1, -1):
        if dp[j]:
            dp[j + a] = True
            if f:
                curr_max = max(curr_max, j + a)
                f = False
print(ans)
Problem p03864 (python)

Fixed submission:
n, x = map(int, input().split())
a = list(map(int, input().split()))
b = [v if v < x else x for v in a]
for i in range(1, n):
    if b[i - 1] + b[i] > x:
        b[i] = x - b[i - 1]
print(sum(i - j for i, j in zip(a, b)))

Buggy submission (verdict: Runtime Error):
from heapq import heappop, heappush

INF = 10 ** 20

xs, ys, xt, yt = map(int, input().split())
N = int(input())
circle = [list(map(int, input().split())) for _ in range(N)]
circle = [[xs, ys, 0]] + circle + [[xt, yt, 0]]
edges_lst = [[] for _ in range(N + 2)]
for i, (x1, y1, r1) in enumerate(circle):
    for j, (x2, y2, r2) in enumerate(circle[i + 1:]):
        j += i + 1
        d = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** .5
        dist = max(0, d - r1 - r2)
        edges_lst[i].append((j, dist))
        edges_lst[j].append((i, dist))

def dijkstra():
    que = []
    heappush(que, (0, 0))
    dist_lst = [INF] * (N + 2)
    dist_lst[0] = 0
    while que:
        dist, node = heappop(que)
        for to, cost in edges_lst[node]:
            if dist_lst[to] > cost + dist:
                dist_lst[to] = cost + dist
                heappush(que, (cost + dist, to))
    return dist_lst[-1]

print('{:.10f}'.format(dijkstra()))
Problem p03921 (python)

Fixed submission:
N, M = map(int, input().split())
parent = [i for i in range(N + M)]
rank = [1] * (N + M)
def find(x):
    if x == parent[x]:
        return x
    parent[x] = find(parent[x])
    return parent[x]

def unite(x, y):
    x, y = find(x), find(y)
    if x == y:
        return
    if rank[x] == rank[y]:
        rank[x] += 1
    elif rank[x] < rank[y]:
        x, y = y, x
    parent[y] = x
for i in range(N):
    for l in list(map(int, input().split()))[1:]:
        # 言語はparentの後半
        unite(i, N + l - 1)
if all(find(0) == find(i) for i in range(N)):
    print('YES')
else:
    print('NO')

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
parent = [i for i in range(N + M)]
def find(x):
    if x == parent[x]:
        return x
    parent[x] = find(parent[x])
    return parent[x]

def unite(x, y):
    x, y = find(x), find(y)
    if x == y:
        return
    parent[y] = x
for i in range(N):
    for l in list(map(int, input().split()))[1:]:
        # 言語はparentの後半
        unite(i, N + l - 1)
if all(find(0) == find(i) for i in range(N)):
    print('YES')
else:
    print('NO')
Problem p03957 (python)

Fixed submission:
s = input()
if 'C' in s and 'F' in s and s.index('C') < s.rindex('F'):
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
s = input()
if s.index('C') < s.rindex('F'):
    print('Yes')
else:
    print('No')
Problem p03970 (python)

Fixed submission:
print(sum(a != b for a, b in zip(input(), 'CODEFESTIVAL2016')))

Buggy submission (verdict: Runtime Error):
x, y = map(int, input().split())
print(sum(a != b for a, b in zip(input(), 'CODEFESTIVAL2016')))
Problem p04035 (python)

Fixed submission:
N, L = map(int, input().split())
a = list(map(int, input().split()))
end = 0
for i in range(N - 1):
    if a[i] + a[i + 1] >= L:
        end = i + 1
        break
else:
    print('Impossible')
    exit()

ans = list(range(1, end)) + list(range(end, N))[::-1]
print('Possible')
print(*ans, sep='\n')

Buggy submission (verdict: Runtime Error):
N, L = map(int, input().split())
a = list(map(int, input().split()))
end = 0
for i in range(N - 1):
    if a[i] + a[i + 1] >= L:
        end = i + 1
        break
else:
    print('Impossible')
    exit()

print('Possible')
print(*range(1, end), *reversed(range(end, N)), sep='\n')
Problem p03095 (python)

Fixed submission:
for t in range(1):
	n=input()
	s=input()
	freq=[0]*(26)
	ct=0
	for i in range(len(s)):
		freq[ord(s[i])-ord('a')]+=1
		if freq[ord(s[i])-ord('a')]==1:
			ct+=1
	ans=1
	for i in range(26):
		if freq[i]>0:
			ans=(ans*(freq[i]+1))
	print((ans-1)%1000000007)

Buggy submission (verdict: Runtime Error):
for t in range(int(input())):
	s=input()
	freq=[0]*(26)
	ct=0
	for i in range(len(s)):
		freq[ord(s[i])-ord('a')]+=1
		if freq[ord(s[i])-ord('a')]==1:
			ct+=1
	ans=1
	for i in range(26):
		if freq[i]>0:
			ans=(ans*(freq[i]+1))
	print((ans-1)%1000000007)
Problem p03106 (python)

Fixed submission:
n,m,k=map(int,input().split())
for i in range(min(n,m),0,-1):
	if n%i==0 and m%i==0:
		k-=1
		if k==0:
			print(i)
			exit()

Buggy submission (verdict: Runtime Error):
import math
n,m,k=map(int,input().split())
print(math.gcd(n,m))

Buggy submission (verdict: Runtime Error):
import math
n,m,k=map(int,input().split())
g=math.gcd(n,m)
for i in range(1,101):
	if g%i==0:
		k-=1
		if k==0:
			print(i)
			exit()

Buggy submission (verdict: Runtime Error):
import math
n,m,k=map(int,input().split())
g=math.gcd(n,m)
for i in range(1,101):
	if g%i==0:
		k-=1
		if k==0:
			print(i)
			exit()

Buggy submission (verdict: Runtime Error):
import math
n,m,k=map(int,input().split())
g=math.gcd(n,m)
for i in range(1,101):
	if g%i==0:
		k-=1
		if k==0:
			print(i)
			exit()
Problem p03160 (python)

Fixed submission:
n=int(input())
arr=list(map(int,input().split()))
dp=[0]*(n)
dp[1]=abs(arr[1]-arr[0])
for i in range(2,n):
	dp[i]=min(dp[i-1]+abs(arr[i]-arr[i-1]),dp[i-2]+abs(arr[i]-arr[i-2]))
print(dp[n-1])

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
dp=[0]*(n)
for i in range(n):
	s=input()
	dp[i]=[-1]*m
	for j in range(m):
		if s[j]=="#":
			dp[i][j]=0
fill=1
for i in range(m):
	if dp[0][i]<0:
		dp[0][i]=fill
	else:
		fill=0
fill=1
for i in range(1,n):
	if dp[i][0]<0:
		dp[i][0]=fill
	else:
		fill=0
for i in range(1,n):
	for j in range(1,m):
		if dp[i][j]<0:
			dp[i][j]=(dp[i-1][j]+dp[i][j-1])%1000000007
print(dp[n-1][m-1])
Problem p04029 (python)

Fixed submission:
n = input()
n = int(n)
total = 0
for candy in range(1, n + 1):
    total += candy

print(total)

Buggy submission (verdict: Runtime Error):
n = input()
total = 0
for candy in range(1, n+1):
  total += candy
 
print(candy)
Problem p04043 (python)

Fixed submission:
# -*- coding: utf-8 -*-
# 整数の入力
a, b, c = map(int, input().split())
# カウンタのクリア
five_count = 0
seven_count = 0
# 5,7,5を判定
def setsu_check(x):
	global five_count, seven_count
	if x == 5:
		five_count+=1
	elif x == 7:
		seven_count+=1
# 判定関数のCALL
setsu_check(a)
setsu_check(b)
setsu_check(c)
if five_count == 2 and seven_count == 1:
	print("YES")
else:
	print("NO")

Buggy submission (verdict: Runtime Error):
user_input = map(int, input().split())
five = 0
seven = 0
for i in len(user_input):
    if user_input[i] == 5:
        five += 1
    elif user_input[i] == 7:
        seven += 1

if five == 2 and seven == 1:
    print('YES')
else:
    print('NO')

Buggy submission (verdict: Runtime Error):
user_input = int(input().split())
five = 0
seven = 0
for i in len(user_input):
    if user_input[i] == 5:
        five += 1
    elif user_input[i] == 7:
        seven += 1

if five == 2 and seven == 1:
    print('YES')
else:
    print('NO')

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

five = 0;
seven = 0;

if A == 5:
    five +=1
elif A == 7:
        seven +=1

if B == 5:
    five +=1
elif B == 7:
        seven +=1

if C == 5:
    five +=1
elif C == 7:
        seven +=1

if five == 2 and seven == 1:
    print 'YES'
else: print 'NO'

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

five = 0;
seven = 0;

if A == 5:
    five +=1
elif A == 7:
        seven +=1

if B == 5:
    five +=1
elif B == 7:
        seven +=1

if C == 5:
    five +=1
elif C == 7:
        seven +=1

if five == 2 and seven == 1:
    print 'YES'
else: print 'NO'

Buggy submission (verdict: Runtime Error):
five = 0;
seven = 0;

if A == 5:
    five +=1
elif A == 7:
        seven +=1

if B == 5:
    five +=1
elif B == 7:
        seven +=1

if C == 5:
    five +=1
elif C == 7:
        seven +=1

if five == 2 and seven == 1:
    print 'YES'
else: print 'NO'

Buggy submission (verdict: Runtime Error):
int five = 0;
int seven = 0;

if A == 5:
    five ++
elif A == 7:
        sevent ++

if B == 5:
    five ++
elif B == 7:
        sevent ++

if C == 5:
    five ++
elif C == 7:
        sevent ++

if five == 2 and seven == 1:
    print 'YES'
    else print 'NO'

Buggy submission (verdict: Runtime Error):
int five = 0;
int seven = 0;

if A == 5
    five ++;

    else if A == 7
        sevent ++;

if B == 5
    five ++;

    else if B == 7
        sevent ++;

if C == 5
    five ++;

    else if C == 7
        sevent ++;

if five == 2 and seven == 1
    print 'YES';
    else print 'NO';
Problem p03086 (python)

Fixed submission:
ACGT_filter = ['A', 'C', 'G', 'T']

ans = 0
now = 0
for s in input():
    if s in ACGT_filter:
        now += 1
    else:
        now = 0
    ans = max(now, ans)

print(ans)

Buggy submission (verdict: Runtime Error):
S = list(input())
ACGT_filter = ['A', 'C', 'G', 'T']

str_length = 0
str_lengths = []
for s in S:
    if s in ACGT_filter:
        str_length += 1
    else:
        str_lengths.append(str_length)
        str_length = 0

print(max(str_lengths))

Buggy submission (verdict: Runtime Error):
S = list(input())
ACGT_filter = ['A', 'C', 'G', 'T']

str_length = 0
str_lengths = []
for s in S:
    if s in ACGT_filter:
        str_length += 1
    else:
        str_lengths.append(str_length)
        str_length = 0

print(max(str_lengths))

Buggy submission (verdict: Runtime Error):
S = list(input())
ACGT_filter = ['A', 'C', 'G', 'T']

str_length = 0
str_lengths = []
for s in S:
    if s in ACGT_filter:
        print(s)
        str_length += 1
    else:
        str_lengths.append(str_length)
        str_length = 0

print(max(str_lengths))
Problem p03085 (python)

Fixed submission:
b = input()
 
c = {"A":"T","T":"A","C":"G","G":"C"}
 
print(c[b])

Buggy submission (verdict: Runtime Error):
b = input()

c = {A:T,T:A,C:G,G:C}

print(c[b])
Problem p03105 (python)

Fixed submission:
A, B, C = [int(e) for e in input().split(" ")]

# N = A*C
# if N <= B:
#     print(C)

p, _ = divmod(B, A)
if p > C:
    print(C)
else:
    print(p)

Buggy submission (verdict: Runtime Error):
N, M = [int(e) for e in input().split(" ")]
d_node2setindex = {}
ABs = []
for _ in range(M):
    ABs.append([int(e) for e in input().split(" ")])
maxValue = N * (N-1) // 2
ans = [maxValue]
buf_sets = []
flagSkip = False
for i in range(M-1):
    a, b = ABs[M-1-i]
    # print("a,b", a, b)
    if flagSkip is True:
        ans.append(0)
        continue
    
    idx_a = d_node2setindex.get(a, None)
    idx_b = d_node2setindex.get(b, None)
    # if len(buf_sets) > 0:
    #     for j, s in enumerate(buf_sets):
    #         if idx_a is None:
    #             if a in s:
    #                 idx_a = j 
    #         if idx_b is None:
    #             if b in s:
    #                 idx_b = j
    #         if idx_a is not None and idx_b is not None:
    #             break
    delta = 0
    if idx_a is None and idx_b is None:
        s_new = set([a, b])
        idx_set = len(buf_sets)
        d_node2setindex[a] = idx_set
        d_node2setindex[b] = idx_set
        buf_sets.append(s_new)
        delta = 1
    elif idx_a is not None and idx_b is None:
        delta = len(buf_sets[idx_a])
        buf_sets[idx_a].add(b)
        d_node2setindex[b] = idx_a
    elif idx_a is None and idx_b is not None:
        delta = len(buf_sets[idx_b])
        buf_sets[idx_b].add(a)
        d_node2setindex[a] = idx_b
    else:
        if idx_a != idx_b:
            s_a = buf_sets[idx_a]
            s_b = buf_sets[idx_b]
            delta = len(s_a) * len(s_b)
            [d_node2setindex.__setitem__(e, idx_a) for e in s_b]
            # for e in s_b:
            #     d_node2setindex[e] = idx_a
            buf_sets[idx_a] = s_a | s_b
            buf_sets[idx_b] = None
            # buf_sets.pop(idx_b)
    
    d = ans[-1] - delta
    ans.append(d)
    if d == 0:
        flagSkip = True

txt = "\n".join(map(str, reversed(ans)))
print(txt)
Problem p03112 (python)

Fixed submission:
import bisect

A, B, Q = [int(e) for e in input().split()]
ss = [int(input()) for _ in range(A)]
tt = [int(input()) for _ in range(B)]
xx = [int(input()) for _ in range(Q)]
INF = 10 ** 18
for x in xx:
    idx_s_right = min(bisect.bisect_left(ss, x), A-1)
    idx_s_left = max(0, idx_s_right - 1)
    stmps = [ss[idx_s_right], ss[idx_s_left]]

    ttmps = [] 
    idx_t_right = min(bisect.bisect_left(tt, x), B-1)
    idx_t_left = max(0, idx_t_right - 1)
    ttmps = [tt[idx_t_right], tt[idx_t_left]]

    mem = INF
    for s in stmps:
        for t in ttmps:
            d_s = abs(s - x) + abs(t - s)
            d_t = abs(t - x) + abs(s - t)
            mem = min(mem, d_s, d_t)
    print(mem)

Buggy submission (verdict: Runtime Error):
import bisect

A, B, Q = [int(e) for e in input().split()]
ss = [int(input()) for _ in range(A)]
tt = [int(input()) for _ in range(B)]
xx = [int(input()) for _ in range(Q)]

for x in xx:
    buf = []
    # s right, t
    idx_s_right = min(bisect.bisect_left(ss, x), A-1)
    s_right = ss[idx_s_right]

    idx_st_right = min(bisect.bisect_left(tt, s_right), B-1)
    idx_st_left = max(0, idx_st_right - 1)
    t_right = tt[idx_st_right]
    t_left = tt[idx_st_left]

    buf.append(abs(s_right - x) + abs(t_right - s_right))
    buf.append(abs(s_right - x) + abs(t_left - s_right))
    s_right = None 
    t_right = None
    s_left = None
    t_left = None

    # s left, t
    idx_s_left = max(0, idx_s_right - 1)
    if idx_s_left != idx_s_right:
        s_left = ss[idx_s_left]

        idx_st_right = min(bisect.bisect_left(tt, s_left), B-1)
        idx_st_left = max(0, idx_st_right - 1)
        t_right = tt[idx_st_right]
        t_left = tt[idx_st_left]

        buf.append(abs(s_left - x) + abs(t_right - s_left))
        buf.append(abs(s_left - x) + abs(t_left - s_left))
    
        s_right = None 
        t_right = None
        s_left = None
        t_left = None

    # t right, r 
    idx_t_right = min(bisect.bisect_left(tt, x), B-1)
    t_right = tt[idx_t_right]

    idx_ts_right = min(bisect.bisect_left(ss, t_right), A-1)
    idx_ts_left = max(0, idx_ts_right - 1)
    s_right = ss[idx_ts_right]
    s_left = ss[idx_ts_left]

    buf.append(abs(t_right - x) + abs(s_right - t_right))
    buf.append(abs(t_right - x) + abs(s_left - t_right))
    
    s_right = None 
    t_right = None
    s_left = None
    t_left = None

    # s left, t
    idx_t_left = max(0, idx_t_right - 1)
    if idx_t_left != idx_t_right:
        t_left = ss[idx_t_left]

        idx_ts_right = min(bisect.bisect_left(ss, t_left), A-1)
        idx_ts_left = max(0, idx_ts_right - 1)
        s_right = tt[idx_ts_right]
        s_left = tt[idx_ts_left]

        buf.append(abs(t_left - x) + abs(s_right - t_left))
        buf.append(abs(t_left - x) + abs(s_left - t_left))

    print(min(buf))