Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
n = int(input())
cs = 'ATGC'
ng = [
  'AGC',
  'GAC',
  'ACG'
]
mod = 10**9 + 7
d = defaultdict(int)
for c1 in cs:
  for c2 in cs:
    for c3 in cs:
      s = c1 + c2 + c3
      if s not in ng:
        d[s] += 1
for i in range(3, n):
  nd = defaultdict(int)
  for key in d.keys:
    for c in cs:
      cn = key[1:2] + c
      if cn not in ng:
        nd[cn] = (nd[cn] + d[key]) % mod
  d = nd
print(sum([d[key] for key in d]) % mod)
      

Fixed submission:
from collections import defaultdict
n = int(input())
cs = 'ATGC'
ng = [
  'AGC',
  'GAC',
  'ACG',
  'AGGC',
  'ATGC',
  'ACGC',
  'AGAC',
  'AGTC'
]

mod = 10**9 + 7
d = defaultdict(int)
for c1 in cs:
  for c2 in cs:
    for c3 in cs:
      s = c1 + c2 + c3
      if s not in ng:
        d[s] += 1
for i in range(3, n):
  nd = defaultdict(int)
  for key in d.keys():
    for c in cs:
      cn = key[1:3] + c
      cn4 = key + c
      if cn not in ng and cn4 not in ng:
        nd[cn] = (nd[cn] + d[key]) % mod
  d = nd
print(sum([d[key] for key in d]) % mod)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
bs = list(map(int, input().split()))

ans = []  
 
def check(bs, pre):
  tmp = pre[:]
  if len(bs) == 0:
    ans.append(tmp)
    return
  cand = []
  for i, b in enumerate(bs):
    if i + 1 == b:
      cand.append(i)
  if len(cand) == 0:
    return
  for i in cand:
    check(bs[:i] + bs[i+1:], [i + 1] + tmp)

check(bs, [])

if len(ans):
  for ai in ans[0]:
    print(ai)
else:
  print(-1)


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

ans = []  
 
def check(bs, pre):
  tmp = pre[:]
  if len(bs) == 0:
    ans.append(tmp)
    return
  cand = []
  for i, b in enumerate(bs):
    if i + 1 == b:
      cand.append(i)
  if len(cand) == 0:
    return
  i = cand[-1]
  check(bs[:i] + bs[i+1:], [i + 1] + tmp)

check(bs, [])

if len(ans):
  for ai in ans[0]:
    print(ai)
else:
  print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
bs = list(map(int, input().split()))

ans = []  
 
def check(bs, pre):
  tmp = pre[:]
  if len(bs) == 0:
    ans.append(tmp)
    return
  cand = []
  for i, b in enumerate(bs):
    if i + 1 == b:
      cand.append(i)
  if len(cand) == 0:
    return
  for i in cand:
    check(bs[:i] + bs[i+1:], [i + 1] + tmp)

check(bs, [])

if len(ans):
  for ai in ans[0]:
    print(ai)
else:
  print(-1)

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

ans = []  
 
def check(bs, pre):
  tmp = pre[:]
  if len(bs) == 0:
    ans.append(tmp)
    return
  cand = []
  for i, b in enumerate(bs):
    if i + 1 == b:
      cand.append(i)
  if len(cand) == 0:
    return
  i = cand[-1]
  check(bs[:i] + bs[i+1:], [i + 1] + tmp)

check(bs, [])

if len(ans):
  for ai in ans[0]:
    print(ai)
else:
  print(-1)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)].sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    pm += item[1]
    if pm > m_minus_1:
        print(s + item[0] * (m - pm + item[1]))
        break
    else:
        s += item[0] * item[1]


Fixed submission:
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
for item in a:
    if pm + item[1] >= m:
        print(s + item[0] * (m - pm))
        break
    else:
        pm += item[1]
        s += item[0] * item[1]

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    pm += item[1]
    if tmp > m_minus_1:
        print(s + item[0] * (m - pm + item[1]))
        break
    else:
        s += item[0] * item[1]


Fixed submission:
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
for item in a:
    if pm + item[1] >= m:
        print(s + item[0] * (m - pm))
        break
    else:
        pm += item[1]
        s += item[0] * item[1]

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    pm += item[1]
    if tmp > m_minus_1:
        print(s + item[0] * (m - pm + item[1]))
        break
    else:
        s += item[0] * item[1]


Fixed submission:
n, m = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
a.sort()
pm = s = 0
for item in a:
    if pm + item[1] >= m:
        print(s + item[0] * (m - pm))
        break
    else:
        pm += item[1]
        s += item[0] * item[1]
Buggy python submission (verdict: Time Limit Exceeded):
k = input()
ol = pl = len(k)
l = 0
while l != pl:
  pl = l
  k = k.replace("01", "").replace("10", "")
  l = len(k)
print(ol - l)

Fixed submission:
k = input()
print(min(k.count("0"),k.count("1"))*2)
Buggy python submission (verdict: Runtime Error):
hs = map(int, input().split())
hs.sort()
print(hs[0] * hs[1] // 2)

Fixed submission:
hs = list(map(int, input().split()))
hs.sort()
print(hs[0] * hs[1] // 2)
Buggy python submission (verdict: Runtime Error):
s = [int(x) for in list(input())]
cand = []
for c1, c2, c3 in zip(s, s[1:], s[2:]):
  res = abs(c1 * 100 + c2 * 10 + c3 - 753)
  cand.append(res)
print(min(cand))
  


Fixed submission:
s = [int(x) for x in list(input())]
cand = []
for c1, c2, c3 in zip(s, s[1:], s[2:]):
  res = abs(c1 * 100 + c2 * 10 + c3 - 753)
  cand.append(res)
print(min(cand))

Buggy python submission (verdict: Runtime Error):
s = [int(x) for in list(input())]
cand = []
for c1, c2, c3 in zip(s, s[1:], s[2:]):
  res = abs(c1 * 100 + c2 * 10 + c3 - 754)
  cand.append(res)
print(min(cand))
  

Fixed submission:
s = [int(x) for x in list(input())]
cand = []
for c1, c2, c3 in zip(s, s[1:], s[2:]):
  res = abs(c1 * 100 + c2 * 10 + c3 - 753)
  cand.append(res)
print(min(cand))
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cs = []
ps = {}
for _ in range(m):
  p, y = map(int, input().split())
  cs.append([y, p])
css = sorted(cs)
for c in css:
  if c[1] in ps:
    ps[c[1]].append(c[0])
  else:
    ps[c[1]] = [c[0]]
[print('{:06}{:06}'.format(c[1], ps[c[1]].index(c[0])+1))for c in cs]

 
  
  


Fixed submission:
n, m = map(int, input().split())
cs = []
cd = {}
ps = {}
for _ in range(m):
  p, y = map(int, input().split())
  cs.append([p, y])
  if p in ps:
    ps[p].append(y)
  else:
    ps[p] = [y]
    
for p in ps:
  v = sorted(ps[p])
  for i, y in enumerate(v):
    cd[y] = i + 1
[print('{:06}{:06}'.format(c[0], cd[c[1]])) for c in cs]

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
cs = []
ps = {}
for _ in range(m):
  p, y = map(int, input().split())
  cs.appenyd([y, p])
css = sorted(cs)
for c in css:
  if c[1] in ps:
    ps[c[1]].append(c[0])
  else:
    ps[c[1]] = [c[0]]
[print('{:06}{:06}'.format(c[1], ps[c[1]].index(c[0])+1))for c in cs]

 
  
  


Fixed submission:
n, m = map(int, input().split())
cs = []
cd = {}
ps = {}
for _ in range(m):
  p, y = map(int, input().split())
  cs.append([p, y])
  if p in ps:
    ps[p].append(y)
  else:
    ps[p] = [y]
    
for p in ps:
  v = sorted(ps[p])
  for i, y in enumerate(v):
    cd[y] = i + 1
[print('{:06}{:06}'.format(c[0], cd[c[1]])) for c in cs]
Buggy python submission (verdict: Runtime Error):
from scipy.special import comb as cmb
n, m = map(int, input().split())

mod = 10 ** 9 + 7
N = 100005

# #逆元テーブル
# inv_t = [0]+[1]
# for i in range(2, N):
#   inv_t += [inv_t[mod % i] * (mod - int(mod / i)) % mod]

# #階乗計算
# kai = [1, 1]
# rev_kai = [1, inv_t[1]]
# for i in range(2, N):
# 	kai.append(kai[-1] * i % mod)
# 	rev_kai.append(rev_kai[-1] * inv_t[i] % mod)

# # コンビネーション計算
# def cmb(n, r):
# 	return kai[n] * rev_kai[r] * rev_kai[n-r] % mod

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

ans = 1

for _, cnt in factorization(m):
    ans = ans * int(cmb(cnt + n - 1, n - 1, exact=True)) % mod

print(ans)

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

mod = 10 ** 9 + 7
N = 200000

#逆元テーブル
inv_t = [0]+[1]
for i in range(2, N):
  inv_t += [inv_t[mod % i] * (mod - int(mod / i)) % mod]

#階乗計算
kai = [1, 1]
rev_kai = [1, inv_t[1]]
for i in range(2, N):
	kai.append(kai[-1] * i % mod)
	rev_kai.append(rev_kai[-1] * inv_t[i] % mod)

# コンビネーション計算
def cmb(n, r):
	return kai[n] * rev_kai[r] * rev_kai[n-r] % mod

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

ans = 1

for _, cnt in factorization(m):
    if _ == 1:
        break
    ans = ans * cmb(cnt + n - 1, n - 1) % mod

print(ans)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input())
if a * b % 2:
  print('Yes')
else:
  print('No')

Fixed submission:
a, b = map(int, input().split())
if a * b % 2:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
n = int(input())
p = ''
d = []
for _ in range(n):
  w = input()
  if p and p[-1] == w[0]
    print('No')
    exit()
  if w in d:
    print('No')
    exit()
  p = w[-1]
  d[w] = 1
print('Yes')
  

Fixed submission:
n = int(input())
p = ''
d = {}
for _ in range(n):
  w = input()
  if p and p[-1] != w[0]:
    print('No')
    exit()
  if w in d:
    print('No')
    exit()
  p = w[-1]
  d[w] = 1
print('Yes')
Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
a = []
m = []
for _ in range(h):
    a.append(list(map(int, input().split())))
for j in range(h):
    f = 0
    for i in range(w):
        if not f:
            if a[j][i] % 2:
                f = a[i][j]
        else:
            print(j+1, i, j+1, i+1)
            if a[j][i] % 2:
                f = 0
    m.append(f)

for j in m:
    f = 0
    if not f:
        if m[j] % 2:
            f = m[j]
    else:
        print(w, j, w, j+1)
        if m[j] % 2:
            f = 0


Fixed submission:
h, w = map(int, input().split())
a = []
m = []
ans = []
for _ in range(h):
    a.append(list(map(int, input().split())))
for j in range(h):
    f = 0
    for i in range(w):
        if not f:
            if a[j][i] % 2:
                f = a[j][i]
        else:
            ans.append([j+1, i, j+1, i+1])
            if a[j][i] % 2:
                f = 0
    m.append(f)

f = 0
for j in range(h):
    if not f:
        if m[j] % 2:
            f = m[j]
    else:
        ans.append([j, w, j+1, w])
        if m[j] % 2:
            f = 0

print(len(ans))
for ansi in ans:
    print(" ".join([str(x) for x in ansi]))
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input.split())
if n % k:
  print(1)
else:
  print(0)

Fixed submission:
n, k = map(int, input().split())
if n % k:
  print(1)
else:
  print(0)
Buggy python submission (verdict: Runtime Error):
import sys
n = int(input())
for i in range(26):
  if (n - 4 * i ) % 7 or n - 4 * a >= 0:
    continue
  else:
    print("Yes")
    sys.exit()
print("No")


Fixed submission:
import sys
n = int(input())
for i in range(26):
  if (n - 4 * i ) % 7 or n - 4 * i < 0:
    continue
  else:
    print("Yes")
    sys.exit()
print("No")

Buggy python submission (verdict: Runtime Error):
import sys
n = int(input())
for i in range(26):
  if (n - 4 * i ) % 7 or n-4*a>=0:
    continue
  else:
    print("Yes")
    sys.exit()
print("No")


Fixed submission:
import sys
n = int(input())
for i in range(26):
  if (n - 4 * i ) % 7 or n - 4 * i < 0:
    continue
  else:
    print("Yes")
    sys.exit()
print("No")
Buggy python submission (verdict: Runtime Error):
d, g = map(int, input().split())
g = g // 100
tmp = 0
cands = []
for i in range(1, d + 1):
  p, c = map(int, input().split())
  cand = [i for _ in range(p)]
  cand[-1] += c
  tmp += sum(cands)
  cands.append(cand)
  
while True:
  if tmp <= g:
    print(sum([len(cand) for cand in cands]))
    break
  lasts = [cand[-1] for cand in cands]
  index = lasts.index(min(lasts))
  tmp -= lasts[index]
  cands[index] = cands[index][:-1]

Fixed submission:
import sys
input = sys.stdin.readline
d, g = map(int, input().split())
pc = []
for i in range(d):
    p, c = map(int, input().split())
    pc.append([p, c // 100])
g = g // 100
ans = 1000
for i in range(2 ** d):
    rem = g
    cost = 0
    for j in reversed(range(d)):
        if ((i >> j) & 1):
            p, c = pc[j]
            rem -= c + p * (j + 1)
            cost += p
    for j in reversed(range(d)):
        if  not ((i >> j) & 1):
            p, c = pc[j]
            p -= 1
            if rem >= p * (j + 1):
                rem -= p * (j + 1)
                cost += p
            else:
                while rem > 0:
                    rem -= j +1
                    cost += 1
                break
    if rem <= 0:
        ans = min(cost, ans)
print(ans)
Buggy python submission (verdict: Runtime Error):
a = list(map(int, input().split()))
print(min([abs(a[0]-a[1])+abs(a[1]-a[2]), abs(a[0]-a[2])+abs(a[1]-a[0])), abs(a[0]-a[2])+abs(a[1]-a[2])]))


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

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


Fixed submission:
a = list(map(int, input().split()))
print(
    min(
    [abs(a[0]-a[1])+abs(a[1]-a[2]), abs(a[0]-a[2])+abs(a[1]-a[0]), abs(a[0]-a[2])+abs(a[1]-a[2])]
    )
)
Buggy python submission (verdict: Runtime Error):
n, m, d = map(int, input().split())
print((n - d) * 2 * (1 - n) ** m / (1 - n) / n ** m)



Fixed submission:
n, m, d = map(int, input().split())
c = n
if d:
  c = 2 * (n - d)
print(c * (m -1)/ n ** 2)
Buggy python submission (verdict: Runtime Error):
a,b=map(int,input())
if a < 9 and b < 9:
  print('Yay!')
else:
  print(':(')

Fixed submission:
a,b=map(int,input().split())
if a < 9 and b < 9:
  print('Yay!')
else:
  print(':(')
Buggy python submission (verdict: Runtime Error):
import itertools

N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

memo = [[0 for i in range(C)]for j in range(3)]

for i in range(N):
    for j in range(N):
        cij = c[i][j]
        a = (i + j + 2) % 3
        for k in range(C):
            memo[a][k] += d[cij-1][k]

ans = 25000000


for c1, c2, c3 in itertools.permutations(range(C), 3):
    ans = min(ans, memo[c1][0] + memo[c2][1] + memo[c3][2])

print(ans)


Fixed submission:
import itertools

N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

memo = [[0 for i in range(C)]for j in range(3)]

for i in range(N):
    for j in range(N):
        cij = c[i][j]
        a = (i + j + 2) % 3
        for k in range(C):
            memo[a][k] += d[cij-1][k]

ans = float('inf')

for c1, c2, c3 in itertools.permutations(range(C), 3):
    ans = min(ans, memo[0][c1] + memo[1][c2] + memo[2][c3])

print(ans)

Buggy python submission (verdict: Runtime Error):
import itertools

N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

memo = [[0 for i in range(C)]for j in range(3)]

for i in range(N):
    for j in range(N):
        cij = c[i][j]
        a = (i + j + 2) % 3
        for k in range(C):
            memo[a][k] += d[cij-1][k]

ans = 25000000


for c1, c2, c3 in itertools.permutations(range(C), 3):
    ans = min(ans, memo[c1][0] + memo[c2][1] + memo[c3][2])

print(ans)

Fixed submission:
import itertools

N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

memo = [[0 for i in range(C)]for j in range(3)]

for i in range(N):
    for j in range(N):
        cij = c[i][j]
        a = (i + j + 2) % 3
        for k in range(C):
            memo[a][k] += d[cij-1][k]

ans = float('inf')

for c1, c2, c3 in itertools.permutations(range(C), 3):
    ans = min(ans, memo[0][c1] + memo[1][c2] + memo[2][c3])

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

bit_len = 20
memo = [[] for i in range(bit_len)]
ans = 0

for i, ai in enumerate(a):
    li = 0
    for j in range(bit_len):
        if ai & (1 << j):
            if len(memo[j]) >= 1:
                li = max(li, memo[j][-1] + 1)
            memo[j].append(i)
        else:
            if len(memo[j]) >= 2:
                li = max(li, memo[j][-2] + 1)
    ans += i - li + 1

print(ans)


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

bit_len = 20
memo = [[] for i in range(bit_len)]
ans = 0

for i, ai in enumerate(a):
    li = 0
    for j in range(bit_len):
        if ai & (1 << j):
            if len(memo[j]) >= 1:
                li = max(li, memo[j][-1] + 1)
            memo[j].append(i)
        else:
            if len(memo[j]) >= 2:
                li = max(li, memo[j][-2] + 1)
    ans += i - li + 1

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n = int(input())
memo = [0 for i in range(n)]
for i in range(n):
  memo[int(input())-1] = i
cand = 0
tmp = 0
for n1, n2 in range(n - 1):
  if n2 > n1:
    tmp += 1
    if tmp > cand:
      cand = tmp
  else:
    tmp = 0
print(n - cand - 1)

Fixed submission:
import sys
input = sys.stdin.readline
n = int(input())
memo = [0 for i in range(n)]
for i in range(n):
  memo[int(input())-1] = i
cand = 0
tmp = 0
for n1, n2 in zip(memo[:], memo[1:]):
  if n2 > n1:
    tmp += 1
    if tmp > cand:
      cand = tmp
  else:
    tmp = 0
print(n - cand - 1)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
k = int(input())

n = len(s)
strs = set([])

for i in range(n):
    for j in range(i+1, n+1):
        strs.add(s[i:j])
strs = list(strs)
strs.sort()
print(strs[k-1])


Fixed submission:
s = input()
k = int(input())

n = len(s)
strs = set([])

for i in range(n):
    for j in range(i+1, min(i+k+1,n+1)):
        strs.add(s[i:j])
strs = list(strs)
strs.sort()
print(strs[k-1])
Buggy python submission (verdict: Runtime Error):
a = map(int, input().split())
k = int(input())

a.sort()
a[-1] *= 2 ** k

print(sum(a))

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

a.sort()
a[-1] *= 2 ** k

print(sum(a))
Buggy python submission (verdict: Runtime Error):
a, b, x = map(int. input().split())
if a <= x and x <= a + b:
  print("YES")
else:
  print("NO")

Fixed submission:
a, b, x = map(int, input().split())
if a <= x and x <= a + b:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
x = list(map(int, input().split()))
x.sort()
d1 = s[2] - s[0]
d2 = s[1] - s[0]
ans += d1 // 2 + d2 // 2
y = d1 % 2 + d2 % 2
if y == 1:
  ans += 2
if y == 2:
  ans += 1
print(ans)

Fixed submission:
s = list(map(int, input().split()))
s.sort()
d1 = s[2] - s[0]
d2 = s[2] - s[1]
ans = d1 // 2 + d2 // 2
y = d1 % 2 + d2 % 2
if y == 1:
  ans += 2
if y == 2:
  ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
n, m = get(int, input().split())
print(n * m - min(2, n) * min(2, m))

Fixed submission:
n, m = map(int, input().split())
if n == 1:
    n += 2
if m == 1:
    m += 2
print((n-2) *(m-2))
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
h, w, d = map(int, input().split())
a = {}
l = list(range(1, h * w + 1))
for i in range(h):
  for j, x in enumerate(input().split()):
    a[int(x)] = (i, j)
memo = {}
def calc(s):
  tmp = 0
  memo[a[s]] = 0
  for Xc, Xn in zip(l[s::d], l[s+d::d]):
    xc, yc = a[Xc]
    xn, yn = a[Xn]
    tmp += abs(xc - xn) + abs(yc - yn)
    memo[a[Xn]] = tmp
for i in range(d):
  calc(i+1)
for _ in range(int(input())):
  l, r = map(int, input().split())
  print(memo[a[r]] - memo[a[l]])

Fixed submission:
import sys
input = sys.stdin.readline
h, w, d = map(int, input().split())
a = {}
l = list(range(1, h * w + 1))
for i in range(h):
  for j, x in enumerate(input().split()):
    a[int(x)] = (i, j)
memo = {}
def calc(s):
  tmp = 0
  memo[a[s+1]] = 0
  for Xc, Xn in zip(l[s::d], l[s+d::d]):
    xc, yc = a[Xc]
    xn, yn = a[Xn]
    tmp += abs(xc - xn) + abs(yc - yn)
    memo[a[Xn]] = tmp
for i in range(d):
  calc(i)
for _ in range(int(input())):
  l, r = map(int, input().split())
  print(memo[a[r]] - memo[a[l]])

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
h, w, d = map(int, input().split())
a = {}
l = list(range(h * w))
for i in range(h):
  for j, x in enumerate(input().split()):
    a[int(x)] = (i, j)
memo = {}
def calc(s):
  tmp = 0
  memo(a[s]) = 0
  for xc, xn in zip(l[s::d], l[s+d::d]):
    xc, yc = a[xc]
    xn, yn = a[xn]
    tmp += abs(xc - xn) + abs(yc - yn)
    memo(a[xn]) = tmp
for i in range(d):
  calc(i)
for _ in range(int(input())):
  l, r = map(int, input().split())
  print(memo[a[r]] - memo[a[l]])
    

Fixed submission:
import sys
input = sys.stdin.readline
h, w, d = map(int, input().split())
a = {}
l = list(range(1, h * w + 1))
for i in range(h):
  for j, x in enumerate(input().split()):
    a[int(x)] = (i, j)
memo = {}
def calc(s):
  tmp = 0
  memo[a[s+1]] = 0
  for Xc, Xn in zip(l[s::d], l[s+d::d]):
    xc, yc = a[Xc]
    xn, yn = a[Xn]
    tmp += abs(xc - xn) + abs(yc - yn)
    memo[a[Xn]] = tmp
for i in range(d):
  calc(i)
for _ in range(int(input())):
  l, r = map(int, input().split())
  print(memo[a[r]] - memo[a[l]])
Buggy python submission (verdict: Runtime Error):
c = []
for i in range(3):
  r = map(int, input().split())
  c.append(r)
  
d1 = set([])
d2 = set([])
d3 = set([])
d4 = set([])
for i in range(3):
  d1.append(c[i][1] - c[i][0])
  d2.append(c[i][2] - c[i][1])
  d3.append(c[1][i] - c[0][i])
  d4.append(c[2][i] - c[1][i])
if len(d1) == 1 and len(d2) == 1 and len(d3) == 1 and len(d4) == 1:
  print('Yes')
else:
  print('No')
  

Fixed submission:
c = []
for i in range(3):
  r = list(map(int, input().split()))
  c.append(r)
  
d1 = set([])
d2 = set([])
d3 = set([])
d4 = set([])
for i in range(3):
  d1.add(c[i][1] - c[i][0])
  d2.add(c[i][2] - c[i][1])
  d3.add(c[1][i] - c[0][i])
  d4.add(c[2][i] - c[1][i])
if len(d1) == 1 and len(d2) == 1 and len(d3) == 1 and len(d4) == 1:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

m = 0
im = 0
for i, ai in enumerate(a):
  if abs(ai) >= abs(m):
    m = ai
    im = i
print(2*n)

for i in range(n):
  print(im+1, i+1)

if im > 0:
  for i in range(0, n-1):
    print(i+1, i+2)
else:
  for i in reversed(1, n):
    print(i+1, i)
    

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

m = 0
im = 0
for i, ai in enumerate(a):
  if abs(ai) >= abs(m):
    m = ai
    im = i
print(2*n-1)

for i in range(n):
  print(im+1, i+1)

if m > 0:
  for i in range(0, n-1):
    print(i+1, i+2)
else:
  for i in reversed(range(1, n)):
    print(i+1, i)