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

Fixed submission:
import fractions
A, B, C, D = map(int, input().split())
E = (C * D) // fractions.gcd(C, D)
print(B - ((B//C - (A-1)//C) + (B//D - (A-1)//D)-(B//E - (A-1)//E))-A + 1)

Buggy submission (verdict: Runtime Error):
import math
A, B, C, D = map(int, input().split())
E = (C * D) // math.gcd(C, D)
print(B - ((B//C - (A-1)//C) + (B//D - (A-1)//D)-(B//E - (A-1)//E))-A + 1)

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


E = C * D // math.gcd(C, D)

X = (B//C - (A-1)//C)
Y = (B//D - (A-1)//D)
Z = (B//E - (A-1)//E)


print(B - (X+Y-Z)-A + 1)

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


E = C * D // math.gcd(C, D)

X = (B//C - (A-1)//C)
Y = (B//D - (A-1)//D)
Z = (B//E - (A-1)//E)


print(B - (X+Y-Z)-A + 1)

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


E = C * D // math.gcd(C, D)

X = (B//C - (A-1)//C)
Y = (B//D - (A-1)//D)
Z = (B//E - (A-1)//E)

print(E)
print(X)
print(Y)
print(Z)
print(B - (X+Y-Z)-A + 1)
Problem p02969 (python)

Fixed submission:
r=int(input())
print(3*r*r)

Buggy submission (verdict: Runtime Error):
a=input().split()
n=int(a[0])
d=int(a[1])


if n%(d*2+1)==0:
	print(int(n/(d*2+1)))
elif n%(d*2+1)!=0:
	print(int(n/(d*2+1))+1)
Problem p02969 (python)

Fixed submission:
a = input()
print (3*int(a)*int(a))

Buggy submission (verdict: Runtime Error):
a = input()
print (3*a*a)

Buggy submission (verdict: Runtime Error):
a = list(map(int, input().split()))
print (3*a*a)
Problem p02971 (python)

Fixed submission:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

a_sort = sorted(set(a))

max_val_1 = a_sort[-1]
max_total_1 = a.count(max_val_1)

if len(a_sort) >= 2:
    max_val_2 = a_sort[-2]

#print("max val:{}".format(max_val_1))
#print("max total:{}".format(max_total_1))

for i in range(n):
    if max_total_1 >=2:
        print(max_val_1)
    
    elif a[i] < max_val_1:
        print(max_val_1)

    elif a[i] == max_val_1 and max_total_1 == 1:
        print(max_val_2)
    else:
        print("err")

Buggy submission (verdict: Runtime Error):
array = list(map(int, input().strip().split()))
n = array[0]
max = 0
t = len(array)
t = t-1
backup = 0

for i in range(n):
    backup = array[i+1]
    array[i+1] = 0

    for j in range(t):
        if(array[j+1] > max):
            max = array[j+1]      

    print(array)
    print(max)
    max = 0
    array[i+1] = backup

Buggy submission (verdict: Runtime Error):
array = list(map(int, input().strip().split()))
n = array[0]
max = 0
t = len(array)
t = t-1
backup = 0
for i in range(n):
    backup = array[i+1]
    array[i+1] = 0
    for j in range(t):
        if(array[j+1] > max):
            max = array[j+1]      
    print(array)
    print(max)
    max = 0
    array[i+1] = backup

Buggy submission (verdict: Runtime Error):
array = list(map(int, input().strip().split()))
n = array[0]
backup = 0
for i in range(n):
    backup = array[i+1]
    array[i+1] = 0
    print(max(array))
    array[i+1] = backup

Buggy submission (verdict: Runtime Error):
array = list(map(int, input().strip().split()))
n = array[0]
backup = 0
for i in range(n):
    backup = array[i]
    array[i] = 0
    print(max(array))
    array[i] = backup
Problem p02981 (python)

Fixed submission:
N,A,B = list(map(int, input().split()))

if N*A < B:
    print(N*A)
else:
    print(B)

Buggy submission (verdict: Runtime Error):
import sys

args = sys.argv

n = args[1]
a = args[2]
b = args[3]

if n*a > b:
    print("b")
else:
    print("a")

Buggy submission (verdict: Runtime Error):
import sys
print('python-izm')

args = sys.argv

n = args[1]
a = args[2]
b = args[3]

if n*a > b:
    print("b")
else:
    print("a")

Buggy submission (verdict: Runtime Error):
import sys
print('python-izm')

args = sys.argv

n = args[0]
a = args[1]
b = args[2]

if n*a > b:
    print("b")
else:
    print("a")
Problem p02981 (python)

Fixed submission:
a=list(map(int,input().split()))
print(min(a[0]*a[1],a[2]))

Buggy submission (verdict: Runtime Error):
a=list(map(int,input().split()))
print(min(a[0]*a[1],a[2])

Buggy submission (verdict: Runtime Error):
a=list(map(int,input()))
if a[0]*a[1]>a[2]:
  print(a[2])
else:
  print(a[0]*a[1])
Problem p02983 (python)

Fixed submission:
l,r = map(int,input().split())
a = float("inf")
for i in range(l,min(r+1,l+2019)):
               for j in range(i+1,min(r+1,l+2019)):
                              if a > (i*j)%2019:
                                 a = (i*j)%2019
print(a)

Buggy submission (verdict: Runtime Error):
l=list(map(int,input().split()))
a=l[0]%2019
b=l[1]%2019
c=[(i*j)%2019 for i in range(a,b) for j in range(i+1,b+1)]
print(min(c))

Buggy submission (verdict: Runtime Error):
l=list(map(int,input().split()))
a=l[0]%2019
b=l[1]%2019
c=[(i*j)%2019 for i in range(a,b) for j in range(i+1,b+1)]
print(min(c))
Problem p02971 (python)

Fixed submission:
N = int(input())
A = list()
for _ in range(N):
    A.append(int(input()))

one, two = sorted(A, reverse=True)[:2]

for i in A:
    print(two if one == i else one)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list()
for _ in range(N):
    A.append(int(input()))

one, two = sorted(list(set(A)), reverse=True)[:2]

for i in A:
    print(two if one == i else one)
Problem p03000 (python)

Fixed submission:
N, X = map(int, input().split())
L = list(map(int, input().split()))

s = 0
for i in range(N):
    s += L[i]
    if s > X:
        print(i + 1)
        exit()

print(i + 2)

Buggy submission (verdict: Runtime Error):
N, X = map(int, input().split())
L = list(map(int, input().split()))

s = 0
for i in range(N + 1):
    s += L[i]
    if s > X:
        break

print(i + 1)

Buggy submission (verdict: Runtime Error):
N, X = map(int, input().split())
L = list(map(int, input().split()))

s = 0
for i in range(N + 1):
    s += L[i]
    if s >= X:
        break

print(i + 1)
Problem p02969 (python)

Fixed submission:
n=int(input()) 
a = n*n*3
print(a)

Buggy submission (verdict: Runtime Error):
a = r*r*3
print(a)

Buggy submission (verdict: Runtime Error):
3*r*r
Problem p03005 (python)

Fixed submission:
N, K = map(int, input().split())
if K == 1:
    print(0)
    exit()

print(N-K)

Buggy submission (verdict: Runtime Error):
N, K = map(int, raw_input().split())
if K == 1:
    print(0)
    exit()

print(N-K)
Problem p03844 (python)

Fixed submission:
array = map(str, raw_input().split())
# print array
if array[1]=="+":
    print int(array[0])+int(array[2])
else:
    print int(array[0])-int(array[2])

Buggy submission (verdict: Runtime Error):
a,op,b = map(raw_input().split())
if op=="+":
    print a+b
else:
    print a-b
Problem p03086 (python)

Fixed submission:
S = str(input())

count = 0
result = []

for i in range(len(S)):
  if (S[i] == "A") or (S[i] == "C") or (S[i] == "G") or (S[i] == "T"):
    count += 1
    if i == len(S) - 1:
      result.append(count)
  else:
    result.append(count)
    count = 0
    if i == len(S) - 1:
      result.append(count)

maxi = result[0]
    
for j in range(len(result)):
  if maxi < result[j]:
    maxi = result[j]

print(maxi)

Buggy submission (verdict: Runtime Error):
S = str(input())

count = 0
result = []

for i in range(len(S)):
  if (S[i] == "A") or (S[i] == "C") or (S[i] == "G") or (S[i] == "T"):
    count += 1
  elif i == len(S) - 1:
    result.append(count)
  else:
    result.append(count)
    count = 0

maxi = result[0]
    
for j in range(len(result)):
  if maxi < result[j]:
    maxi = result[j]

print(maxi)

Buggy submission (verdict: Runtime Error):
S = str(input())

count = 0
result = []

for i in range(len(S)):
  if (S[i] == "A") or (S[i] == "C") or (S[i] == "G") or (S[i] == "T"):
    count += 1
  elif (i == len(S)):
    result.append(count)
  else:
    result.append(count)
    count = 0

maxi = result[0]
    
for j in range(len(result)):
  if maxi < result[j]:
    maxi = result[j]

print(maxi)

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

count = 0
result = []

for i in range(len(S)):
  if (S[i] == "A") or (S[i] == "C") or (S[i] == "G") or (S[i] == "T"):
    count += 1
  else:
    result.append(count)
    count = 0

maxi = result[0]
    
for j in range(len(result)):
  if maxi < result[j]:
    maxi = result[j]

print(maxi)

Buggy submission (verdict: Runtime Error):
S = str(input())

count = 0
result = []

for i in range(len(S)):
  if (S[i] == "A") or (S[i] == "C") or (S[i] == "G") or (S[i] == "T"):
    count += 1
  else:
    result.append(count)
    count = 0

maxi = result[0]
    
for j in range(len(result)):
  if maxi < result[j]:
    maxi = result[j]

print(maxi)

Buggy submission (verdict: Runtime Error):
S = str(input())

count = 0
result = []

for i in range(len(S)):
  if (S[i] == "A") or (S[i] == "C") or (S[i] == "G") or (S[i] == "T"):
    count += 1
  else:
    result.append(count)
    count = 0

max = result[0]
    
for j in range(len(result)):
  if max < result[j]:
    max = result[j]

print(max)

Buggy submission (verdict: Runtime Error):
S = str(input())

count = 0
result = []

for i in range(len(S)):
  if S[i] == "A" or "C" or "G" or "T":
    count += 1
  else:
    result.append(count)
    count = 0

max = result[0]
    
for j in range(len(result)):
  if max < result[j]:
    max = result[j]

print(max)
Problem p02971 (python)

Fixed submission:
n = int(input())
l = []
for i in range(n):
  a = int(input())
  l.append(a)
x = max(l)
i = l.index(x)

l.sort()
for b in range(n):
  if i != b:
    print(x)
  else:
    print(l[n-2])

Buggy submission (verdict: Runtime Error):
n = int(input())
l = []
y = []
for i in range(n):
  a = int(input())
  l.append(a)
for i in range(n):
  x = l[i]
  print(max(l.remove(x))
Problem p02971 (python)

Fixed submission:
N = int(input())
A = [int(input()) for _ in range(N)]
max_A = max(A)

# 複数の最大値のインデックスを全て取得
max_index = [i for i, v in enumerate(A) if v == max_A]

# 最大値は1個か?
one_max = (len(max_index) == 1)
answer = [max_A] * N

if one_max:
    Second = A.copy()
    Second.remove(max_A)
    Second_A = max(Second)
    for i in max_index:
        answer[i] = Second_A
for i in range(N):
    print(answer[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [input() for _ in range(N)]

max_A = max(A)
Second = A.copy()
Second.remove(max(max_A))
Second_A = max(Second)


max_index = [i for i, v in enumerate(A) if v == max_A]

one_max = (len(max_index) == 1)

answer = [max_A] * N
if one_max:
    for i in max_index:
        answer[i] = Second_A
for i in range(N):
    print(answer[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [input() for _ in range(N)]

max_A = max(A)
Second = A.copy()
Second.remove(max(max_A))
second_A = max(Second)

max_index = [i for i, v in enumerate(A) if v == max_A]

answer = [max_A] * N
for i in max_index:
    answer[i] = second_A
for i in range(N):
    print(answer[i])
Problem p03000 (python)

Fixed submission:
N, X = map(int,input().split())
L = list(map(int, input().split()))

i = 1
D = 0

for l in L:
    D = D + l
    if X < D:
        break
    i = i +1
print(i)

Buggy submission (verdict: Runtime Error):
N, X = map(int,input().split())
L = list(map(int, input().split()))

i = 0
D = 0

while D <= X:
    D = D + L[i]
    i = i + 1
    if i == N+1:
        break

print(i)

Buggy submission (verdict: Runtime Error):
N, X = map(int,input().split())
L = list(map(int, input().split()))

i = 0
D = 0

while D <= X:
    D = D + L[i]
    i = i + 1

print(i)
Problem p03029 (python)

Fixed submission:
A, P = map(int,input().split())
import math

print(math.floor((3*A+P)/2))

Buggy submission (verdict: Runtime Error):
A, P = map(int,input().split())
import math

print(math.floor((3A+P)/2))
Problem p03043 (python)

Fixed submission:
N, K = map(int,input().split())
import math
Sum = 0

for i in range(N):
    n = i + 1
    if n < K:
        Sum += pow(0.5, math.ceil(math.log(K/n, 2)))
    else:
        Sum += 1
Sum = Sum / N
print(Sum)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
import math
Sum = 0

for i in range(N):
    n = i + 1
    if n < k:
        Sum += pow(0.5, math.ceil(math.log(K/n, 2)))
    else:
        Sum += 1
Sum = Sum / N
print(Sum)
Problem p03062 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    aj = Alist[cur_N+1]
    ret = max(ai + ddp(cur_N+1, aj), -1*ai + ddp(cur_N+1, -1*aj))
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))

Buggy submission (verdict: Runtime Error):
N = int(input())
Alist = list(map(float, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    aj = Alist[cur_N+1]
    ret = max([ai + ddp(cur_N+1, aj), -1*ai + ddp(cur_N+1, -1*aj)])
    memo[cur_N][ai] = ret
    return ret
print(int(ddp(0, Alist[0])))

Buggy submission (verdict: Runtime Error):
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    aj = Alist[cur_N+1]
    ret = max([ai + ddp(cur_N+1, aj), -1*ai + ddp(cur_N+1, -1*aj)])
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))

Buggy submission (verdict: Runtime Error):
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    aj = Alist[cur_N+1]
    ret = max((ai + ddp(cur_N+1, aj)), (-1*ai + ddp(cur_N+1, -1*aj)))
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))

Buggy submission (verdict: Runtime Error):
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    nx = Alist[cur_N+1]
    ret = max(ai + ddp(cur_N+1, nx), -1*ai + ddp(cur_N+1, -1*nx))
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))

Buggy submission (verdict: Runtime Error):
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    nx = Alist[cur_N+1]
    ret = max(ai + ddp(cur_N+1, nx), -ai + ddp(cur_N+1, -nx))
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))
Problem p03085 (python)

Fixed submission:
b = input()
if b == "A":
    print("T")
elif b == "T":
    print("A")
elif b == "C":
    print("G")
elif b == "G":
    print("C")

Buggy submission (verdict: Runtime Error):
import re
N, Q = map(int, input().split())
S = input()
l = []
r = []
for i in range(Q):
    li, ri = map(int, input().split())
    l += [li]
    r += [ri]

for ind in range(Q):
    substr = S[max(0, l[ind]-1) : min(r[ind], N)]
    #print(substr.count("AC"))
    print(len(re.findall("AC", S)))
Problem p03087 (python)

Fixed submission:
N, Q = map(int, input().split())
S = input()
l = []
r = []
for i in range(Q):
    li, ri = map(int, input().split())
    l += [li]
    r += [ri]

S_tf = [0]*(N+1)
for ind in range(N):
    S_tf[ind + 1] = S_tf[ind] + (1 if S[ind:ind+2] == "AC" else 0)
for ind in range(Q):
    print(S_tf[max(0, r[ind]-1)] - S_tf[min(l[ind]-1, N)])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
S = input()
l = []
r = []
for i in range(Q):
    li, ri = map(int, input().split())
    l += [li]
    r += [ri]

for ind in range(Q):
    substr = S[max(0, l[ind]-1) : min(r[ind], N)]
    print(len(re.findall("AC", S)))
Problem p02969 (python)

Fixed submission:
r = int(input())
print(3 * r ** 2)

Buggy submission (verdict: Runtime Error):
r = input(int())
print(3 * r ** 2)

Buggy submission (verdict: Runtime Error):
r = input(int())
print(3 * r ** 3)
Problem p02983 (python)

Fixed submission:
l,r = input().split()
l = int(l)
r = int(r)
N = int(2019)
  
ans = N
count = 0
right = min(l + N, r)
for i in range(l,right):
  for j in range(i+1,right+1):
    ans = min(ans, (i * j) % N)
    
print(ans)

Buggy submission (verdict: Runtime Error):
l,r = input().split()
l = int(l)
r = int(r)
N = int(2019)
  
ans = (l * (l + 1)) % N
count = 0
for i in range(l,r):
  for j in range(l+1,r+1):
    if(count >= (N - 1)):
      flag = True
      break
    else if(ans > (i * j) % N):
      ans = (i * j) % N
    count += 1
  if flag:
    break
    
print(ans)

Buggy submission (verdict: Runtime Error):
l,r = input().split()
l = int(l)
r = int(r)
N = int(2019)
  
ans = (l * (l + 1)) % N
count = 0
for i in range(l,r-1):
  for j in range(l+1,r):
    if(count >= (N - 1)):
      flag = True
      break
    if(ans > (i * j) % N):
      ans = (i * j) % N
    count += 1
  if flag:
    break
    
print(ans)

Buggy submission (verdict: Runtime Error):
l,r = input().split()
N = 2019
  
ans = (l * (l + 1)) % N;
count = 0;
for i in range(l,r-1):
  if(count >= (N - 1)) break
  for j in range(l+1,r):
    if(count >= (N - 1)) break
    if(ans > (i * j) % N) ans = (i * j) % N
    count++;
    
printf("%lu\n",ans);
return 0
Problem p03006 (python)

Fixed submission:
N = int(input())

balls = []
for i in range(N):
    x, y = map(int, input().split())
    balls.append((x, y))

if N == 1:
    print(1)
else:
    pq = {}
    for i in range(N-1):
        for j in range(i+1, N):
            p, q = balls[i][0] - balls[j][0], balls[i][1] - balls[j][1]
            if p < 0 or (p == 0 and q < 0):
                p, q = -p, -q
            if (p, q) in pq:
                pq[(p, q)] += 1
            else:
                pq[(p, q)] = 1

    ans = N - max(pq.values())
    print(ans)

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

balls = []
for i in range(N):
    x, y = map(int, input().split())
    balls.append((x, y))

pq = {}
for i in range(N-1):
    for j in range(i+1, N):
        p, q = balls[i][0] - balls[j][0], balls[i][1] - balls[j][1]
        if p < 0:
            p, q = -p, -q
        if (p, q) in pq:
            pq[(p, q)] += 1
        else:
            pq[(p, q)] = 1

ans = N - max(pq.values())
print(ans)
Problem p03074 (python)

Fixed submission:
N, K = map(int, input().split())
S = [int(i) for i in input()]

now = 1
cnt = 0
nums = []
for i in range(N):
    if S[i] == now:
        cnt += 1
    else:
        nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    nums.append(cnt)

if len(nums) % 2 == 0:
    nums.append(0)

add = 2 * K + 1

# Calculate cumulative sums
cum_sum = [0]
for i in range(0, len(nums)):
    cum_sum.append(cum_sum[i] + nums[i])

# Solve
ans = 0
for i in range(0, len(nums), 2):
    l = i
    r = min(i + add, len(nums))
    ans = max(ans, cum_sum[r] - cum_sum[l])

print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = [int(i) for i in input()]

cnt_arr = [0]
idx = 0
cnt_seq_0 = 1
cnt_seq_1 = 0
cnt_seq_0_arr = []
cnt_seq_1_arr = []
idx_seq_0 = []

prev = 1
for i, s_i in enumerate(S):
    if s_i == 0:
        if prev == 0:
            cnt_seq_0 += 1
        elif prev == 1:
            cnt_seq_1_arr.append(cnt_seq_1)
            cnt_seq_1 = 1
        if prev != 0:
            idx_seq_0.append(i)
        if i == len(S) - 1:
            cnt_seq_0_arr.append(cnt_seq_0)
            cnt_seq_1_arr.append(0)
    elif s_i == 1:
        if prev == 1:
            cnt_seq_1 += 1
        elif prev == 0:
            cnt_seq_0_arr.append(cnt_seq_0)
            cnt_seq_0 = 1
        if i == len(S) - 1:
            cnt_seq_1_arr.append(cnt_seq_1)
    prev = s_i

r_cnt = {}

import heapq
sum_arr = []

if len(cnt_seq_0_arr) == 0:
    print(len(cnt_seq_1_arr))
else:
    sum_0 = cnt_seq_1_arr[0]
    for i in range(K):
        sum_0 += cnt_seq_0_arr[i] + cnt_seq_1_arr[i + 1]

    heapq.heappush(sum_arr, -sum_0)
    sum_i = sum_0

    for i in range(0, len(cnt_seq_0_arr) - K):
        sum_i += -cnt_seq_0_arr[i] - cnt_seq_1_arr[i] + cnt_seq_0_arr[i + K] + cnt_seq_1_arr[i + K + 1]
        heapq.heappush(sum_arr, -sum_i)

    print(-heapq.heappop(sum_arr))
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)]
x = [int(input()) for _ in range(Q)]


def bsearch(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    if r < len(arr) and abs(arr[l] - n) > abs(arr[r] - n):
        return r
    else:
        return l


def bsearch_l(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    return l


def calc_distance(x, l, c, near_idx):
    ret = abs(x - l)
    if c == 's':
        next_l = t[near_idx]
    else:
        next_l = s[near_idx]
    ret += abs(next_l - l)
    return ret


st = []
for i, s_i in enumerate(s):
    t_i = bsearch(s_i, t)
    st.append((s_i, 's', t_i))

for i, t_i in enumerate(t):
    s_i = bsearch(t_i, s)
    st.append((t_i, 't', s_i))

st = sorted(st)
starr = sorted(s + t)

ans_list = []
for i, x_i in enumerate(x):
    st_i = bsearch_l(x_i, starr)
    l, c, near_idx = st[st_i]
    ans = calc_distance(x_i, l, c, near_idx)
    if st_i < len(st) - 1:
        l, c, near_idx = st[st_i + 1]
        ans = min(ans, calc_distance(x_i, l, c, near_idx))
    ans_list.append(ans)

for i, ans in enumerate(ans_list):
    print(ans)

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)]
x = [int(input()) for _ in range(Q)]
INF = 10 ** 9


def bsearch(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    return l


s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i, x_i in enumerate(x):
    sa = bsearch(x_i, s)
    ta = bsearch(x_i, t)
    lmin = INF
    for si in range(sa, sa + 2):
        for ti in range(ta, ta + 2):
            l = min(abs(x_i - s[si]) + abs(s[si] - t[ti]), abs(x_i - t[ti]) + abs(t[ti] - s[si]))
            lmin = min(l, lmin)
    print(lmin)
Problem p03128 (python)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
INF = 10 ** 9


def matchnum(n):
    table = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    return table[n - 1]


# dp[n] : n本使った時の桁数
dp = [0] + [-INF for _ in range(N)]
for i in range(N + 1):
    for j, a_j in enumerate(A):
        if i - matchnum(a_j) >= 0:
            dp[i] = max(dp[i], dp[i - matchnum(a_j)] + 1)

l = dp[N]
m = N
ans = []
A = sorted(A, reverse=True)
while m > 0:
    for i, a_i in enumerate(A):
        if m - matchnum(a_i) >= 0 and dp[m - matchnum(a_i)] == l - 1:
            ans.append(str(a_i))
            m -= matchnum(a_i)
            l -= 1
            break
print(''.join(ans))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
INF = 10 ** 9


def matchnum(n):
    table = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    return table[n - 1]


# dp[n] : n本使った時の桁数
dp = [0] + [-INF for _ in range(N)]
for i in range(N + 1):
    for j, a_j in enumerate(A):
        if i - matchnum(a_j) >= 0:
            dp[i] = max(dp[i], dp[i - matchnum(a_j)] + 1)

l = dp[N]
m = N
ans = []
A = sorted(A, reverse=True)
while m > 0:
    for i, a_i in enumerate(A):
        if dp[m - matchnum(a_i)] == l - 1:
            ans.append(str(a_i))
            m -= matchnum(a_i)
            l -= 1
            break
print(''.join(ans))