Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n = int(input())
b = [int(i) for i in input().split()]
flag = True
for i in range(n):
    if b[i] > i+1:
        flag = False
        break

if not flag:
    print(-1)
else:
    ans = []
    while len(b) > 0:
        for m in range(max(b), 0, -1):
            if b[m-1] == m:
                ans.append(b[m-1])
                del b[m-1]
                break
ans = ans[::-1]
print(*ans, sep="\n")


Fixed submission:
n = int(input())
b = [int(i) for i in input().split()]
flag = True
for i in range(n):
    if b[i] > i+1:
        flag = False
        break

if not flag:
    print(-1)
else:
    ans = []
    while len(b) > 0:
        for m in range(max(b), 0, -1):
            if b[m-1] == m:
                ans.append(b[m-1])
                del b[m-1]
                break
    ans = ans[::-1]
    print(*ans, sep="\n")
Buggy python submission (verdict: Runtime Error):
import math
A, B = [int(i) for i in input().split()]
top = int(math.log2(B))+1

def cnt1(a, i):
    return (a//(2**i)) * 2**(i-1) + max(0, a%(2**i) - 2**(i-1))

ans = ''
for i in range(1, top+1):
    cntB = cnt1(B+1, i)
    cntA = cnt1(A, i)
    ans += str((cntB-cntA)%2)

ans = ans[::-1]
print(int(ans, 2))


Fixed submission:
import math

def cnt1(a, i):
    return (a//(2**i)) * 2**(i-1) + max(0, a%(2**i) - 2**(i-1))

A, B = [int(i) for i in input().split()]
if B == 0:
    ans = '0'
else:
    top = int(math.log2(B))+1


    ans = ''
    for i in range(1, top+1):
        cntB = cnt1(B+1, i)
        cntA = cnt1(A, i)
        ans += str((cntB-cntA)%2)

    ans = ans[::-1]
print(int(ans, 2))

Buggy python submission (verdict: Time Limit Exceeded):
a, b = [int(i) for i in input().split()]
ans = 0
for i in range(b-a+1):
  ans = ans ^ (a + i)
print(ans)

Fixed submission:
import math

def cnt1(a, i):
    return (a//(2**i)) * 2**(i-1) + max(0, a%(2**i) - 2**(i-1))

A, B = [int(i) for i in input().split()]
if B == 0:
    ans = '0'
else:
    top = int(math.log2(B))+1


    ans = ''
    for i in range(1, top+1):
        cntB = cnt1(B+1, i)
        cntA = cnt1(A, i)
        ans += str((cntB-cntA)%2)

    ans = ans[::-1]
print(int(ans, 2))
Buggy python submission (verdict: Time Limit Exceeded):
#C-117
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
if n >= m:
  print(0)
elif n == 1:
  print(max(x) - min(x))
else:
  x.sort()
  x_diff = []
  for mi in range(m-1):
    x_diff.append(x[mi+1]-x[mi])
  #print(x_diff)
  split_list = []
  for ni in range(n-1):
    split_idx = x_diff.index(max(x_diff))
    split_list.append(split_idx)
    x_diff[split_idx] = 0
  split_list.sort()
  #print(split_list)
  #print(x)
  ans = 0
  ans += x[split_list[0]] - x[0]
  #print("ans",ans)
  for ni in range(n-2):
    l = split_list[ni]+1
    r = split_list[ni+1]
    ans += x[r] - x[l]
    #print("ans",ans)
  ans += x[-1] - x[split_list[-1]+1]
  print(ans)


Fixed submission:
#C-117
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
if n >= m:
  print(0)
elif n == 1:
  print(max(x) - min(x))
else:
  x.sort()
  x_diff = []
  for mi in range(m-1):
    x_diff.append([x[mi+1]-x[mi], mi])
  #print(x_diff)
  x_diff.sort(reverse = True)
  #print(x_diff)
  split_list = []
  for ni in range(n-1):
    split_idx = x_diff[ni][1]
    split_list.append(split_idx)
  split_list.sort()
  #print(split_list)
  #print(x)
  ans = 0
  ans += x[split_list[0]] - x[0]
  #print("ans",ans)
  for ni in range(n-2):
    l = split_list[ni]+1
    r = split_list[ni+1]
    ans += x[r] - x[l]
    #print("ans",ans)
  ans += x[-1] - x[split_list[-1]+1]
  print(ans)

Buggy python submission (verdict: Runtime Error):
#C-117
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
if n >= m:
  print(0)
else:
  x.sort()
  x_diff = []
  for mi in range(m-1):
    x_diff.append(x[mi+1]-x[mi])
  #print(x_diff)
  split_list = []
  for ni in range(n-1):
    split_idx = x_diff.index(max(x_diff))
    split_list.append(split_idx)
    x_diff[split_idx] = 0
  split_list.sort()
  #print(split_list)
  #print(x)
  ans = 0
  ans += x[split_list[0]] - x[0]
  #print("ans",ans)
  for ni in range(n-2):
    l = split_list[ni]+1
    r = split_list[ni+1]
    ans += x[r] - x[l]
    #print("ans",ans)
  ans += x[-1] - x[split_list[-1]+1]
  print(ans)


Fixed submission:
#C-117
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
if n >= m:
  print(0)
elif n == 1:
  print(max(x) - min(x))
else:
  x.sort()
  x_diff = []
  for mi in range(m-1):
    x_diff.append([x[mi+1]-x[mi], mi])
  #print(x_diff)
  x_diff.sort(reverse = True)
  #print(x_diff)
  split_list = []
  for ni in range(n-1):
    split_idx = x_diff[ni][1]
    split_list.append(split_idx)
  split_list.sort()
  #print(split_list)
  #print(x)
  ans = 0
  ans += x[split_list[0]] - x[0]
  #print("ans",ans)
  for ni in range(n-2):
    l = split_list[ni]+1
    r = split_list[ni+1]
    ans += x[r] - x[l]
    #print("ans",ans)
  ans += x[-1] - x[split_list[-1]+1]
  print(ans)
Buggy python submission (verdict: Runtime Error):
N, X = [int(i) for i in input().split()]
b = "P"
for i in range(N):
    b = "B" + b + "P" + b + "B"

b_eat = b[:X]
print(b_eat.count("P"))

Fixed submission:
N, X = [int(i) for i in input().split()]
p = [1]
l = 1
for i in range(N):
    p.append(p[-1] * 2 + 1)
    l = 2 * l + 3
ban_l = 1
ban_r = l
pate = l//2+1
cnt = 0
for i in range(N+1):
    if X == pate:
        if pate == ban_l:
            cnt += 1
            break
        else:
            cnt += p[N-(i+1)] + 1
            break
    elif X == ban_l:
        break
    elif X < pate:
        ban_l = ban_l + 1
        ban_r = pate - 1
        pate = (pate+ban_l)//2
    elif X < ban_r:
        cnt += p[N-(i+1)] + 1
        ban_l = pate + 1
        pate = (pate+ban_r)//2
        ban_r = ban_r - 1
    else:
        cnt += p[N-i]
        break
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
#C-113
n, m = [int(i) for i in input().split()]
py = []
py_dict = {}
for mi in range(m):
  p, y = [int(i) for i in input().split()]
  py.append([p,y])
  py_dict[mi]=[p,y]
py.sort()
for mi in range(m):
  pref = py_dict[mi][0]
  x = [py[i][1] for i in range(m) if py[i][0] == pref].index(py_dict[mi][1])+1
  print("{:06}{:06}".format(pref,x))
  



Fixed submission:
n, m = [int(i) for i in input().split()]
city = []
pref = [0 for i in range(n)]
for i in range(m):
    p, y = [int(_) for _ in input().split()]
    pref[p-1] += 1
    city.append([p,y,i])

city.sort(key = lambda x: (x[0], x[1]))
cnt = 1
for i in range(m):
    city[i].append(cnt)
    if cnt == pref[city[i][0]-1]:
        cnt = 1
    else:
        cnt += 1
city.sort(key = lambda x: x[2])
for i in range(m):
    print(str(city[i][0]).zfill(6)+str(city[i][3]).zfill(6))
Buggy python submission (verdict: Runtime Error):
N = int(input())
x = []
y = []
h = []
for _ in range(N):
    tmp = [int(i) for i in input().split()]
    if tmp[2] == 0:
        continue
    x.append(tmp[0])
    y.append(tmp[1])
    h.append(tmp[2])

for Cx in range(100 + 1):
    for Cy in range(100 + 1):
        correct_flag = True
        H = abs(x[0] - Cx) + abs(y[0] - Cy) + h[0]
        for i in range(1, N):
            if abs(x[i] - Cx) + abs(y[i] - Cy) + h[i] != H:
                correct_flag = False
                break
        if correct_flag:
            print(Cx, Cy, H)
            break


Fixed submission:
N = int(input())
x = []
y = []
h = []
key = 0
for i in range(N):
    tmp = [int(i) for i in input().split()]
    if tmp[2] != 0:
        key = i
    x.append(tmp[0])
    y.append(tmp[1])
    h.append(tmp[2])

for Cx in range(100 + 1):
    for Cy in range(100 + 1):
        correct_flag = True
        H = abs(x[key] - Cx) + abs(y[key] - Cy) + h[key]
        for i in range(N):
            if h[i] == 0:
                if abs(x[i] - Cx) + abs(y[i] - Cy) < H:
                    correct_flag = False
                    break
            else:
                if abs(x[i] - Cx) + abs(y[i] - Cy) + h[i] != H:
                    correct_flag = False
                    break
        if correct_flag:
            print(Cx, Cy, H)
            break

Buggy python submission (verdict: Runtime Error):
N = int(input())
x = []
y = []
h = []
for _ in range(N):
    tmp = [int(i) for i in input().split()]
    if tmp[2] == 0:
        continue
    x.append(tmp[0])
    y.append(tmp[1])
    h.append(tmp[2])

for Cx in range(100 + 1):
    for Cy in range(100 + 1):
        correct_flag = True
        H = abs(x[0] - Cx) + abs(y[0] - Cy) + h[0]
        for i in range(1, N):
            if abs(x[i] - Cx) + abs(y[i] - Cy) + h[i] != H:
                correct_flag = False
                break
        if correct_flag:
            print(Cx, Cy, H)
            break
    if correct_flag:
        break


Fixed submission:
N = int(input())
x = []
y = []
h = []
key = 0
for i in range(N):
    tmp = [int(i) for i in input().split()]
    if tmp[2] != 0:
        key = i
    x.append(tmp[0])
    y.append(tmp[1])
    h.append(tmp[2])

for Cx in range(100 + 1):
    for Cy in range(100 + 1):
        correct_flag = True
        H = abs(x[key] - Cx) + abs(y[key] - Cy) + h[key]
        for i in range(N):
            if h[i] == 0:
                if abs(x[i] - Cx) + abs(y[i] - Cy) < H:
                    correct_flag = False
                    break
            else:
                if abs(x[i] - Cx) + abs(y[i] - Cy) + h[i] != H:
                    correct_flag = False
                    break
        if correct_flag:
            print(Cx, Cy, H)
            break
Buggy python submission (verdict: Runtime Error):
for i in range(min(math.ceil(math.sqrt(M)), M//N), 0, -1):
  if M % i == 0:
    print(i)
    break


Fixed submission:
import math
N, M = [int(i) for i in input().split()]
for i in range(math.ceil(M/N), 0, -1):
  if M % i == 0:
    print(i)
    break
Buggy python submission (verdict: Runtime Error):
n = int(input())
v = [int(i) for i in input().split()]
MAX_V = 100000
even = [[i, 0] for i in range(MAX_V)]
odd = [[i, 0] for i in range(MAX_V)]
for ni in range(n//2):
    even[v[ni*2]][1] += 1
    odd[v[ni*2+1]][1] += 1
even.sort(key=lambda x: x[1], reverse=True)
odd.sort(key=lambda x: x[1], reverse=True)

if even[0][0] == odd[0][0]:
    if even[1][1] > odd[1][1]:
        print(n-even[1][1]-odd[0][1])
    else:
        print(n-even[0][1]-odd[1][1])
else:
    print(n - even[0][1] - odd[0][1])

Fixed submission:
n = int(input())
v = [int(i) for i in input().split()]
MAX_V = 100001
even = [[i, 0] for i in range(MAX_V)]
odd = [[i, 0] for i in range(MAX_V)]
for ni in range(n//2):
    even[v[ni*2]][1] += 1
    odd[v[ni*2+1]][1] += 1
even.sort(key=lambda x: x[1], reverse=True)
odd.sort(key=lambda x: x[1], reverse=True)

if even[0][0] == odd[0][0]:
    if even[1][1] > odd[1][1]:
        print(n-even[1][1]-odd[0][1])
    else:
        print(n-even[0][1]-odd[1][1])
else:
    print(n - even[0][1] - odd[0][1])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(i) for i in input().split()]
cnt = 0
for i in range(1,2*n//k+1):
    if i*k%2 ==1 and k%2 == 1:
        for j in range((i-1)//2):
            a = i*k//2 - (k+2*k*j-1)//2
            if i * k - a <= n:
                cnt += ((a+n)//k - a//k)*2
    elif i*k%2 == 0:
        if k%2 == 0:
            for j in range(i-1):
                a = i * k // 2 - (k + k * j) // 2
                if i*k-a <= n:
                    cnt += ((a + n) // k - a // k) * 2
        else:
            for j in range(1,i // 2):
                a = i * k // 2 - k * j
                if i * k - a <= n:
                    cnt += ((a + n) // k - a // k) * 2
        cnt += ((i*k//2 + n) // k - (i*k//2) // k)
print(cnt)


Fixed submission:
N, K = [int(i) for i in input().split()]
cnt = 0
if K % 2 == 0:
    for i in range(1, 2*N//K+1):
        cnt += 1 + (2*N//K-i)//2 * 6 + (2*N//K-i)//2 * ((2*N//K-i)//2 - 1) * 3
else:
    for i in range(1, N//K+1):
        cnt += 1 + (N//K-i) * 6 + (N//K-i) * ((N//K-i) - 1) * 3

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):

n, k = [int(i) for i in input().split()]

cnt = 0
for i in range(1,2*n//k+1):
    for j in range(max(1, k*i-n), min(k*i//2+1, n+1)):
        a = j
        b = k*i-a
        if a == b:
            d = 1
        else:
            d = 2
        if (b-a) % k == 0:
            cnt += ((a+n)//k - a//k)*d
print(cnt)


Fixed submission:
N, K = [int(i) for i in input().split()]
cnt = 0
if K % 2 == 0:
    for i in range(1, 2*N//K+1):
        cnt += 1 + (2*N//K-i)//2 * 6 + (2*N//K-i)//2 * ((2*N//K-i)//2 - 1) * 3
else:
    for i in range(1, N//K+1):
        cnt += 1 + (N//K-i) * 6 + (N//K-i) * ((N//K-i) - 1) * 3

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):

n, k = [int(i) for i in input().split()]

cnt = 0
for i in range(1,2*n//k+1):
    for j in range(1, min(k*i, n+1)):
        a = j
        b = k*i-a
        if b <= n and (b-a)%k == 0:
            cnt += (a+n)//k- a//k
print(cnt)


Fixed submission:
N, K = [int(i) for i in input().split()]
cnt = 0
if K % 2 == 0:
    for i in range(1, 2*N//K+1):
        cnt += 1 + (2*N//K-i)//2 * 6 + (2*N//K-i)//2 * ((2*N//K-i)//2 - 1) * 3
else:
    for i in range(1, N//K+1):
        cnt += 1 + (N//K-i) * 6 + (N//K-i) * ((N//K-i) - 1) * 3

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):

n, k = [int(i) for i in input().split()]

cnt = 0
for i in range(1,2*n//k+1):
    for j in range(1, min(k*i, n+1)):
        a = j
        b = k*i-a
        if b <= n:
            for m in range(a//k+1,(a+n)//k+1):
                c = m*k - a
                if (c + b)%k == 0:
                    cnt += 1
print(cnt)


Fixed submission:
N, K = [int(i) for i in input().split()]
cnt = 0
if K % 2 == 0:
    for i in range(1, 2*N//K+1):
        cnt += 1 + (2*N//K-i)//2 * 6 + (2*N//K-i)//2 * ((2*N//K-i)//2 - 1) * 3
else:
    for i in range(1, N//K+1):
        cnt += 1 + (N//K-i) * 6 + (N//K-i) * ((N//K-i) - 1) * 3

print(cnt)
Buggy python submission (verdict: Runtime Error):
h, w = [int(i) for i in input().split()]
a = []
d = []
h_new = 0
for i in range(h):
  b = input()
  if "#" in b:
    a.append(list(b))
    h_new += 1
for i in range(w):
  c = []
  for j in range(h_new):
    c.append(a[j][i])
  if "#" not in c:
    d.append(i)
e = ""
for i in d:
  for j in range(h_new):
    del a[j][i]
for i in range(h_new-1):
  e += "".join(a[i]) +"\n"
e += "".join(a[h_new-1])
print(e)

Fixed submission:
h, w = [int(i) for i in input().split()]
a = []
d = []
h_new = 0
for i in range(h):
  b = input()
  if "#" in b:
    a.append(list(b))
    h_new += 1
for i in range(w):
  c = []
  for j in range(h_new):
    c.append(a[j][i])
  if "#" not in c:
    d.append(i)
e = ""
for i in range(h_new):
  for j in range(w):
    if j not in d:
      print(a[i][j],end="")
  print("\n")
Buggy python submission (verdict: Runtime Error):
d, g = [int(i) for i in input().split()]
p = []
c = []
for i in range(d):
    x, y = [int(_) for _ in input().split()]
    p.append(x)
    c.append(y)

ans = 1
key = d-1
score_max = 0
cnt = 1
pat = [0 for i in range(d)]
while True:
    while pat[key] >= p[key]:
        key -= 1    
    score_max += (key+1) * 100
    pat[key] += 1

    if cnt in p:
        i = p.index(cnt)
        score = 100*(i+1)*p[i]+c[i]
        if score > score_max:
            score_max = score
            pat = [0 for i in range(d)]
            pat[i] = p[i]
            key = d -1
    if score_max >= g:
        break
    cnt += 1
print(cnt)
    


Fixed submission:
import math
# N = int(input())
D, G = [int(i) for i in input().split()]
problems = []
for i in range(D):
    p, c = [int(_) for _ in input().split()]
    bit = 0
    problems.append([p, c])

ans = 1001
for i in range(2**D):
    score = 0
    p_num = 0
    for j, p in enumerate(problems):
        if i >> j & 1:
            score += p[0] * 100 * (j + 1) + p[1]
            p_num += p[0]
    if score < G:
        rest = G - score
        for j in range(D-1, -1, -1):
            if i >> j & 1:
                continue
            n = math.ceil(rest / (100 * (j + 1)))
            if n <= problems[j][0] - 1:
                rest -= n * 100 * (j + 1)
                p_num += n
                break
            rest -= problems[j][0] * 100 * (j + 1)
            p_num += problems[j][0]
        if rest <= 0:
            ans = min(ans, p_num)
    else:
        ans = min(ans, p_num)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
d, g = [int(i) for i in input().split()]
p = []
c = []
for i in range(d):
    x, y = [int(_) for _ in input().split()]
    p.append(x)
    c.append(y)

ans = 100 * 10
for i in range(1, 100**d):
    score = 0
    cnt = 0
    pat = [0 for i in range(d)]
    for j in range(d):
        pat[j] = i % 100
        i = i//100
    for j in range(d):
        if pat[j] >= p[j]:
            score += p[j] * (j+1) * 100 + c[j]
            cnt += p[j]
        else:
            score += pat[j] * (j+1) * 100
            cnt += pat[j]
    if score >= g and cnt < ans:
        ans = cnt
print(ans)

Fixed submission:
import math
# N = int(input())
D, G = [int(i) for i in input().split()]
problems = []
for i in range(D):
    p, c = [int(_) for _ in input().split()]
    bit = 0
    problems.append([p, c])

ans = 1001
for i in range(2**D):
    score = 0
    p_num = 0
    for j, p in enumerate(problems):
        if i >> j & 1:
            score += p[0] * 100 * (j + 1) + p[1]
            p_num += p[0]
    if score < G:
        rest = G - score
        for j in range(D-1, -1, -1):
            if i >> j & 1:
                continue
            n = math.ceil(rest / (100 * (j + 1)))
            if n <= problems[j][0] - 1:
                rest -= n * 100 * (j + 1)
                p_num += n
                break
            rest -= problems[j][0] * 100 * (j + 1)
            p_num += problems[j][0]
        if rest <= 0:
            ans = min(ans, p_num)
    else:
        ans = min(ans, p_num)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
x = [int(i) for i in input().split()]

y = [[x[i], i] for i in range(n)]
y.sort()

z = [0 for i in range(n)]
for i in range(n):
    z[y[i][1]] = (y[:i]+y[i+1:])[(n+1)//2-1][0]

for i in range(n):
    print(z[i])

Fixed submission:
n = int(input())
x = [int(i) for i in input().split()]

y = sorted(x)
med1 = y[n//2-1]
med2 = y[n//2]

for i in range(n):
    if x[i] <= med1:
        print(med2)
    else:
        print(med1)
Buggy python submission (verdict: Runtime Error):
n = input()
l = len(n)

if set(list(n[1:])) == {"9"}:
    print(int(n[0])+9*(len(n)-1))
else:
    print(int(n[0])-1+9*(len(n)-1))n = input()
l = len(n)
if l == 1:
    print(n)
elif set(list(n[1:])) == {"9"}:
    print(int(n[0])+9*(l-1))
else:
    print(int(n[0])-1+9*(l-1))

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

if l == 1:
    print(n)
elif set(list(n[1:])) == {"9"}:
    print(int(n[0])+9*(l-1))
else:
    print(int(n[0])-1+9*(l-1))
Buggy python submission (verdict: Time Limit Exceeded):
x, y = [int(i) for i in input().split()]
if x % y == 0:
    print(-1)
else:
    i = 2
    while True:
        if i * x % y != 0:
            print(i * x)
            break


Fixed submission:
x, y = [int(i) for i in input().split()]
if x % y == 0:
    print(-1)
else:
    i = 2
    while True:
        if i * x % y != 0:
            print(i * x)
            break
        i += 1
Buggy python submission (verdict: Time Limit Exceeded):
import math
isPrime = [True for i in range(10**5+1)]
for i in range(3, 10**5+1):
    for j in range(2, math.floor(math.sqrt(i))+1):
        if i % j == 0:
            isPrime[i] = False

cntPrime = [0 for i in range(10**5+1)]
for i in range(2,10**5+1):
    if i % 2 == 1 and isPrime[i] and isPrime[(i+1)//2]:
        cntPrime[i] = cntPrime[i-1]+1
    else:
        cntPrime[i] = cntPrime[i-1]

Q = int(input())
for i in range(Q):
    l, r = [int(_) for _ in input().split()]
    print(cntPrime[r]-cntPrime[l-1])


Fixed submission:
import math
isPrime = [True for i in range(10**5+1)]
for i in range(3, 10**5+1):
    for j in range(2, math.floor(math.sqrt(i))+1):
        if i % j == 0:
            isPrime[i] = False

cntPrime = [0 for i in range(10**5+1)]
for i in range(2,10**5+1):
    if i % 2 == 1 and isPrime[i] and isPrime[(i+1)//2]:
        cntPrime[i] = cntPrime[i-1]+1
    else:
        cntPrime[i] = cntPrime[i-1]

Q = int(input())
for i in range(Q):
    l, r = [int(_) for _ in input().split()]
    print(cntPrime[r]-cntPrime[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
def calculate_mp(a, b, path, mp_sum):
    if a == b:
        return mp_sum
    cand = [k for k in range(10) if k not in path]
    mp_sum_list = []
    for c in cand:
        mp_sum_list.append(calculate_mp(c, b, path + [c], mp_sum + C[a][c]))
    return min(mp_sum_list)

H, W = [int(i) for i in input().split()]
C = []
for i in range(10):
    c = [int(i) for i in input().split()]
    C.append(c)
D = [0 for i in range(10)]
for i in range(10):
    D[i] = calculate_mp(i, 1, [i], 0)
cnt = 0
for i in range(H):
    A = [int(i) for i in input().split()]
    for a in A:
        if a != -1:
            cnt += D[a]
print(cnt)


Fixed submission:
def calculate_mp(a, b, path, mp_sum):
    if a == b:
        return mp_sum
    cand = [k for k in range(10) if k not in path]
    mp_sum_list = []
    for c in cand:
        mp_sum_list.append(calculate_mp(c, b, path + [c], mp_sum + C[a][c]))
    return min(mp_sum_list)

H, W = [int(i) for i in input().split()]
C = []
for i in range(10):
    c = [int(i) for i in input().split()]
    C.append(c)
D = [0 for i in range(10)]
for i in range(10):
    D[i] = calculate_mp(i, 1, [i], 0)
cnt = 0
for i in range(H):
    A = [int(i) for i in input().split()]
    for a in A:
        if a != -1:
            cnt += D[a]
print(cnt)
Buggy python submission (verdict: Runtime Error):
import sys
s = input()
# まず達成可能かどうかを判定する
s_without_x = [si for si in s if si != 'x']
if len(s_without_x) % 2 == 0:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2-1:-1]:
        print(-1)
        sys.exit(0)
else:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2:-1]:
        print(-1)
        sys.exit(0)

# 達成可能な場合
l = 0
r = len(s) - 1
cnt = 0
while l != r:
    if s[l] != s[r]:
        cnt += 1
        if s[l] == 'x':
            l += 1
        else:
            r -= 1
    else:
        l += 1
        r -= 1
print(cnt)

Fixed submission:
import sys
s = input()
# まず達成可能かどうかを判定する
s_without_x = [si for si in s if si != 'x']
if len(s_without_x) % 2 == 0:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2-1:-1]:
        print(-1)
        sys.exit(0)
else:
    if s_without_x[:len(s_without_x)//2] != s_without_x[len(s_without_x)-1:len(s_without_x)//2:-1]:
        print(-1)
        sys.exit(0)

# 達成可能な場合
l = 0
r = len(s) - 1
cnt = 0
while l < r:
    if s[l] != s[r]:
        cnt += 1
        if s[l] == 'x':
            l += 1
        else:
            r -= 1
    else:
        l += 1
        r -= 1
print(cnt)