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

Fixed submission:
k = int(input())
alist = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(alist[k-1])

Buggy submission (verdict: Runtime Error):
k = int(input())
alist = list(1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51)
print(alist[k-1])

Buggy submission (verdict: Runtime Error):
k = int(input())
alist =1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 . split(, )
print(alist[k-1])

Buggy submission (verdict: Runtime Error):
k = int(input())
alist =1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 . split(,)
alist[k-1]

Buggy submission (verdict: Runtime Error):
a = input()
alist = a. split(,)
alist[31]

Buggy submission (verdict: Runtime Error):
a = input()
a. split(,)
a[31]
Problem p02742 (python)

Fixed submission:
h,w = map(int,input(). split())
if (h == 1) or (w == 1):
  answer = 1
  print(answer)
elif (h%2 == 1) and (w%2 == 1):
 
  answer = ((h*w)+1)//2
  print(answer)
else:
  answer = h*w//2
  print(answer)

Buggy submission (verdict: Runtime Error):
h,w = map(int,input(). split())
if (h == 1) or (w == 1):
  answer = 1
  print(answer)
elif (h%2 = 1) and (w%2 = 1):
  a = (h//2)*(w//2)
  b = ((h//2)+1)*((w//2)+1)
  answer = a + b
  print(answer)
else:
  answer = h*w/2
  print(answer)

Buggy submission (verdict: Runtime Error):
h, w =map(int, input(). split())
if(h%2 = 0) and (w%2 = 0):
  answer = (h/2)*(w/2)
  print(answer)
elif(h%2 = 0) and (w%2 = 1):
  answer = (h/2)*w
  print(answer)
elif(h%2 = 1) and (w%2 = 0):
  answer = (w/2)*h
  print(answer)
else:
  answer =(h//2)*(w//2)+((h//2)+1)*((w//2)+1)
  print(answer)
Problem p02743 (python)

Fixed submission:
a, b, c = map(int, input(). split())
if (4*a*b<(c-a-b)*(c-a-b)) and c-a-b>0 :
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
a, b, c = map(int, input(). split())
if (4ab < c-a-b):
  print('Yes')
else:
  print('No')
Problem p02755 (python)

Fixed submission:
A, B = map(int, input(). split())
answer = None
flag = False
for i in range(1, 1009):
  
  if (A == (i*0.08)//1) and (B == (i*0.1)//1):
    answer = i
    print(answer)
    flag = True
   
    if flag:
        break
if answer is None:
  answer = -1
  print(answer)

Buggy submission (verdict: Runtime Error):
A, B = map(int, input(). split())
answer = None
flag = False
for i in range(1009):
  
  if (A == (i*0.08)//1) and (B == (i*0.1)//1):
    answer = i
    print(answer)
    flag = True
   
    if flag:
        break
if (answer = None):
  print(-1)

Buggy submission (verdict: Runtime Error):
A, B = map(int, input(). split())
answer = None
flag = False
for i in range(1009):
  
  if (A == i*0.08//1) and (B == i*0.1//1):
    Answer = i
    print(Answer)
    flag = True
   
    if flag:
        break
if (answer = None):
  print(-1)

Buggy submission (verdict: Runtime Error):
A, B = map(int, input(). split())
answer = None
flag = False
for i in range(1009):
  
  if (i*0.08//1 = A) and (i*0.1//1 = B):
    print(i)
    flag = True
   
    if flag:
        break
if (answer = None):
  print(-1)

Buggy submission (verdict: Runtime Error):
A, B = map(int, input(). split())
answer = None
flag = False
for i in range(1001):
  
  if (i*0.08//1 = A) and (i*0.1//1 = B):
    print(i)
    flag = True
    break
if (answer = None):
  print(-1)

Buggy submission (verdict: Runtime Error):
A, B = map(int, input(). split())
for i in range(1001):
  flag = True
  if (i*0.08//1 = A) and (i*0.1//1 = B):
    print(i)
    flag = False
    break
if (flag = True):
  print(-1)
Problem p02573 (python)

Fixed submission:
# union-find を使う

# unionするクエリが飛んでくる(クエリ:要求)
# find(どのグループ,集合かわかるようにする)
# findはある要素とある要素が同じグループに属するか,そのグループのサイズはどうか
# この二つを処理する

# 計算量 O(log N) か 工夫すると O(a(N)):アッカーマン計算量の逆関数... ならし計算量

##木構造を作っていく. グループのidはnodeのidにする. findするときは根の番号に辿るようにする

## 計算量を落とす.
### 木がどんどん深くなると処理が長くなる.

## 1. くっつける先を根に向かうようにする, グループとグループは根から根に繋げるようにする
## 小さい方を大きい方にくっつける
## -> 一回あたりO(log(N))
### 高さが高々 log(N)にしかならないから
#### 根から辿ると,一個辿ると頂点の個数が半分以下になっていくから
##### 証明できる: 辺が張られた後は そのグループはいじられない
##### さらに,最初につけたときに既に 残りのグループの方が大きいため

## 2. 経路縮約的な考え
# 計算結果をメモしておく
# 一回計算したところは直接つなぐ　-> メモ化で　ならしlog(N)

##3. 両方を採用すると ならし　アッカーマン関数の逆関数



##実装の工夫

# 根にマイナスのサイズを持つようにする。
# 葉には親のidを持つようにする
# メモ化するためにd[x]を親のfindと一致させるようにする

# MSTを考えてuniteでくっつけられるかどうかを考える

class UnionFind():
    def __init__(self, n):
        self.parents = [-1]*n #各親を格納する,自分が親ならマイナスサイズを持つ
    
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y) ##親を見た方が楽
        if x==y:
            return
        
        if self.parents[x] > self.parents[y]:
            x,y = y,x
        
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    def size(self,x):
        return -self.parents[self.find(x)]
    
def main():
    n,m = map(int,input().split())
    uf = UnionFind(n)
    for _ in range(m):
        a, b = map(int, input().split())
        uf.union(a-1,b-1)
    
    ans = 1
    for i in range(n):
        ans = max(ans, uf.size(i))
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
##この問題はグループの最大人数を求めれば良い

n,m = map(int,input().split())

a = [list(map(int,input().split())) for i in range(m)]

#print(a[0][1])

l = [n+1]*n
# lで何のグループに属しているか判定する
p = [1]*n
# lでどのグループがどのくらいのpowerを持っているか判定する

##最終的にlがn+1でない中で,pが一番大きいのを解答に出す
##ただ,これでは更新頻度がえぐいため工夫がいる.


for j in range(m):
    if(l[a[j][0]] == n+1 & l[a[j][1]] == n+1):
        if(a[j][0] > a[j][1]):
            l[a[j][0]] = a[j][1]
            l[a[j][1]] = a[j][1]
            p[a[j][1]] = 2
        else:
            l[a[j][0]] = a[j][0]
            l[a[j][1]] = a[j][0]
            p[a[j][0]] = 2

    elif(l[a[j][0]] != n+1 & l[a[j][1]] != n+1):
        if(l[a[j][0]] < l[a[j][1]]):

            ###give up
Problem p03160 (python)

Fixed submission:
N = int(input())
h = input().rstrip().split(' ')
h.insert(0,0)
h = list(map(int,h))

#ここで保存しておく
l = [-1]*(N-1)
l.insert(0,0)
l.insert(0,0)

def cost_min(n):
    #n番目までで最小のコストを考える
    if(l[n] != -1):
        return l[n]
    else:
        l[n] = min([cost_min(n-1) + abs(h[n]-h[n-1]),cost_min(n-2) + abs(h[n]-h[n-2])])
        return l[n]

def cost_min_bottomup(n):
    if(n<2):
        return 0
    t_0 = 0
    t_1 = abs(h[2] - h[1])
    ans_0 = 0
    ans_1 = t_1
    for i in range(n-2):
        t_0 = ans_0 + abs(h[i+3]-h[i+1])
        t_1 = ans_1 + abs(h[i+3]-h[i+2])
        ans_0 = ans_1
        ans_1 = min([t_0,t_1])
    return ans_1

#print(cost_min(N))
print(cost_min_bottomup(N))

Buggy submission (verdict: Runtime Error):
N = int(input())
h = input().rstrip().split(' ')
h.insert(0,0)
h = list(map(int,h))

#ここで保存しておく
l = [-1]*(N-1)
l.insert(0,0)
l.insert(0,0)

import sys
sys.setrecursionlimit(30000)

def cost_min(n):
    #n番目までで最小のコストを考える
    if(l[n] != -1):
        return l[n]
    else:
        l[n] = min([cost_min(n-1) + abs(h[n]-h[n-1]),cost_min(n-2) + abs(h[n]-h[n-2])])
        return l[n]

def cost_min_bottomup(n):
    if(n<2):
        return 0
    t_0 = 0
    t_1 = abs(h[2] - h[1])
    ans_0 = 0
    ans_1 = t_1
    for i in range(n-2):
        t_0 = ans_0 + abs(h[i+3]-h[i+1])
        t_1 = ans_1 + abs(h[i+3]-h[i+2])
        ans_0 = ans_1
        ans_1 = min([t_0,t_1])
    return ans_1

print(cost_min(N))
#print(cost_min_bottomup(N))

Buggy submission (verdict: Runtime Error):
N = int(input())
h = input().rstrip().split(' ')
h.insert(0,-1)
h = list(map(int,h))

l = [-1]*(N-1)
l.insert(0,0)
l.insert(0,0)

def cost_min(n):
    #n番目までで最小のコストを考える
    if(l[n] != -1):
        return l[n]
    else:
        l[n] = min([cost_min(n-1) + abs(h[n]-h[n-1]),cost_min(n-2) + abs(h[n]-h[n-2])])
        return l[n]

print(cost_min(N))
Problem p02553 (python)

Fixed submission:
a,b,c,d = map(int, input().split())
l = [a*c, a*d, b*c, b*d]
print(max(l))

Buggy submission (verdict: Runtime Error):
a,b,c,d = map(int, input().split())
l = [a*c, a*d, b*c, b*d]
print(max[l])
Problem p03037 (python)

Fixed submission:
N,M = map(int, input().split())
ar = [0,N]
for m in range(M):
    l,r = map(int, input().split())
    if l>ar[0]:
        ar[0] = l
    if r<ar[1]:
        ar[1]=r
print(max([0,ar[1]-ar[0]+1]))

Buggy submission (verdict: Runtime Error):
N,M = map(int, input().split())
ar = [0,N]
for m in range(M):
    l,r = map(int, input().split())
    if l>ar[0]:
        ar[0] = l
    if r<ar[1]:
        ar[1]=r
print(max([0,ar[1]-ar[0]+1])
Problem p02640 (python)

Fixed submission:
x, y = map(int, input().split())
def func(x,y):
    for i in range(x+1):
        for j in range(x+1):
            if i+j==x:
                if i*2 + j*4 == y:
                    print('Yes')
                    return
    print('No')
    return
func(x,y)

Buggy submission (verdict: Runtime Error):
x = list(map(int, input().split()))
def func(x):
    for i in range(x[0]+1):
        for j in range(x[0]+1):
            if i+j>x[0]:
                break
            elif i*2 + j*4 == x[2] or j*2 + i*4 == x[2]:
                print('Yes')
                return
    print('No')
    return
func(x)
Problem p02675 (python)

Fixed submission:
n=input()
if int(n[len(n)-1]) in [2,4,5,7,9]:
    print('hon') 
elif int(n[len(n)-1]) in [0,1,6,8]:
    print('pon')
else:
    print('bon')

Buggy submission (verdict: Runtime Error):
N, M = map(int,input().split()) 
P = [list(map(int,input().split())) for i in range(M)]
A = [ [0 for __ in range(N)] for _ in range(N)]
for path in P:
    A[path[1]-1][path[0]-1]=1
    A[path[0]-1][path[1]-1]=1
#print(A)
depth_list = [0 for _ in range(N)]
next_generation = set([0])
depth = 0
#old_generation = set()
direction = [0 for _ in range(N)]
used = [0 for _ in range(N)]
while len(next_generation)!=0:
    #print('next_gen',next_generation)
    current = next_generation
    for node in next_generation:
        used[node] = 1
    #[old_generation.add(node) for node in next_generation]
    next_generation = set()
    for index in current:
        depth_list[index]=depth
    for node in current:
        #print('node',node,':',A[node])
        
        #print('old',old_generation)
        for i,neighbor in enumerate(A[node]):
            #print("trm",neighbor)

            if neighbor == 1 and used[i]==0:
                #print("unseen",i)
                next_generation.add(i)
                direction[i]=node+1
    depth+=1
if used.count(1) != N:
    print('No')
else:
    print('Yes')
    for d in direction[1:]:
        print(d)
#print(depth_list)
Problem p02882 (python)

Fixed submission:
import math
a,b,x=map(int,input().split())
f=b*(a**2)/2
if x>=f:
  y=2*(b*(a**2)-x)/(a**2)
  if y==0:
    print(0)
    exit()
  print(90-math.degrees(math.atan(a/y)))
else:
  y=2*x/(a*b)
  if y==0:
    print(0)
    exit()
  print(math.degrees(math.atan(b/y)))

Buggy submission (verdict: Runtime Error):
import math
a,b,x=map(int,input().split())
f=b*(a**2)/2
if x>=f:
  y=2*(b*(a**2)-x)/(a**2)
  print(90-math.degrees(math.atan(a/y)))
else:
  y=2*x/(a*b)
  print(math.degrees(math.atan(b/y)))
Problem p02958 (python)

Fixed submission:
x = int(input())
a_list = list(map(int,input().split()))
a = sorted(a_list)
count = 0
for i in range(x):
    if a[i] == a_list[i]:
      count += 1
if count >= x-2:
  print('YES')
else:
  print('NO')

Buggy submission (verdict: Runtime Error):
x = int(input())
for i in range(x):
  exec("a_%d = int(input())" %(i))
a_list = [eval("a_%d" %(i)) for i in range(x)]
a_list.sort()
be_list = [eval("a_%d" %(i)) for i in range(x)]
count = 0
for i in range(x):
    if a_list[i] == be_list[i]:
      count += 1
if count >= x-2:
  print('YES')
else:
  print('NO')
Problem p03076 (python)

Fixed submission:
a = [int(input()) for i in range(5)]
ans = 0
cnt = [0]
for i in a:
  if i % 10 !=0:
    ans += i + (10-i%10)
    cnt.append(10-i%10)
  else:
    ans += i
print(ans-max(cnt))

Buggy submission (verdict: Runtime Error):
a = [int(input()) for i in range(5)]
ans = 0
cnt = []
for i in a:
  if i % 10 !=0:
    ans += i + (10-i%10)
    cnt.append(10-i%10)
  else:
    ans += i
print(ans-max(cnt))
Problem p03087 (python)

Fixed submission:
n,q = map(int,input().split())
s = input() + '0'
lr = [list(map(int,input().split())) for _ in range(q)]
cnt = [0] * (n+1)

for i in range(n):
  if s[i] == 'A' and s[i+1] == 'C':
    cnt[i+1] = cnt[i] + 1
  else:
    cnt[i+1] = cnt[i]
for i in lr:
  print(cnt[i[1]-1] - cnt[i[0]-1])

Buggy submission (verdict: Runtime Error):
n,q = map(int,input().split())
s = input()
lr = [list(map(int,input().split())) for _ in range(q)]
cnt = [0] * (n+1)

for i in range(n):
  if s[i] == 'A' and s[i+1] == 'C':
    cnt[i+1] = cnt[i] + 1
  else:
    cnt[i+1] = cnt[i]
for i in lr:
  print(cnt[i[1]-1] - cnt[i[0]-1])
Problem p03262 (python)

Fixed submission:
n,x = map(int,input().split())
x_list = list(map(int,input().split()))
x_list.append(x)
x_list = sorted(x_list)
def u(o,p):
  c = [a[1],a[0]]
  while c[0]%c[1] != 0:
    c.append(c[0]%c[1])
    c.pop(0)
  else:
    return c[1]
    
if 4 >= len(x_list) >= 3:
  a = []
  for i in range(1,n+1):
    a.append(x_list[i] - x_list[i-1])
  a = sorted(a)
  print(u(a[1],a[0]))

elif len(x_list) > 4:
  a = []
  for i in range(1,n+1):
    a.append(x_list[i] - x_list[i-1])
  a = sorted(a)
  u = u(a[1],a[0])
  for i in a:
    if i/u == int(i/u):
      pass
    else:
      u = i%u
  print(u)
else:
  print(x_list[1]-x_list[0])

Buggy submission (verdict: Runtime Error):
x_list = list(map(int,input().split()))
x_list.append(x)
x_list = sorted(x_list)
if len(x_list) >= 3:
  a = []
  for i in range(1,n+1):
    a.append(x_list[i] - x_list[i-1])
  a = sorted(a)
  c = [a[1],a[0]]
  while c[0]%c[1] != 0:
    c.append(c[0]%c[1])
    c.pop(0)
  else:
    print(c[1])
else:
  print(x_list[1]-x_list[0])

Buggy submission (verdict: Runtime Error):
n,x = map(int,input().split())
x_list = list(map(int,input().split()))
x_list.append(x)
x_list = sorted(x_list)

if len(x_list) >= 3:
  a = []
  for i in range(1,n):
    a.append(x_list[i] - x_list[i-1])
  a = sorted(a)
  c = [a[1],a[0]]
  while c[0]%c[1] != 0:
    c.append(c[0]%c[1])
    c.pop(0)
  else:
    print(c[1])
else:
  print(x_list[1]-x_list[0])
Problem p03353 (python)

Fixed submission:
s = input()
k = int(input())
s_list = []
for i in range(len(s)):
  for j in range(k):
    s_list.append(s[i:i+j+1])
s_list = sorted(list(set(s_list)))
print(s_list[k-1])

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())
s_list = []
for i in range(len(s)):
  s_list.append(s[i:i+k])
s_list = sorted(list(set(s_list)))
if len(s_list[0]) == k:
  print(s_list[0])
else:
  n = len(s_list[0]) + len(s_list[1])
  if n == k:
    print(s_list[2][0])
  elif n < k:
    print(s_list[2][0:k-n+1])
  else:
    print(s_list[1][0:n-k+1])
Problem p03502 (python)

Fixed submission:
x = input()
c = 0
for i in range(len(x)):
  c += int(x[i])
if int(x) % c == 0:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
x = input()
if int(x) % (int(x[0])+int(x[1])) == 0:
  print('Yes')
else:
  print('No')
Problem p03633 (python)

Fixed submission:
n = int(input())
t = sorted([int(input()) for _ in range(n)],reverse = True)
def c_mul(x,y):
  d = x
  e = y
  while y%x != 0:
    z = y%x
    y = x
    x = z
  else:
    return d*e//x
for i in range(1,n):
  a = c_mul(t[0],t[i])
  if a != t[0]:
    t[0] = a
print(t[0])

Buggy submission (verdict: Runtime Error):
n = int(input())
t = sorted([int(input()) for _ in range(n)],reverse = True)
def c_mul(x,y):
  d = x
  e = y
  while y%x != 0:
    z = y%x
    y = x
    x = z
  else:
    return d*e//x
for i in range(1,n):
  a = c_mul(t[0],t[i])
  if a != t[0]:
    t[0] = a
print(a)
Problem p03760 (python)

Fixed submission:
o = input()
e = input()
if len(o) > len(e):
  a = o[0]+e[0]
  for i in range(1,len(e)):
    a += o[i] + e[i]
  print(a+o[-1])
elif len(o) == len(e):
  a = o[0]+e[0]
  for i in range(1,len(e)):
    a += o[i] + e[i]
  print(a)
else:
  a = e[0]+o[0]
  for i in range(1,len(o)):
    a += e[i] + o[i]
  print(a+e[-1])

Buggy submission (verdict: Runtime Error):
o = input()
e = input()
if len(o) > len(e):
  a = o[0]+e[0]
  for i in range(1,len(e)):
    a += o[i] + e[i]
  print(a+o[-1])
if len(o) == len(e):
  a = o[0]+e[0]
  for i in range(1,len(e)):
    a += o[i] + e[i]
  print(a)
else:
  a = e[0]+o[0]
  for i in range(1,len(o)):
    a += e[i] + o[i]
  print(a+e[-1])
Problem p02585 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))


def dfs(i):
    if 0 <= visited[i]:
        return
    visited[i] = idx
    cycle.append(i)
    global csum
    csum += C[i]
    dfs(P[i])


visited = [-1]*n
idx = 0
cycles = []
for i in range(n):
    if 0 <= visited[i]:
        continue
    cycle = []
    csum = 0
    dfs(i)
    cycles.append((cycle, csum))
    idx += 1


INF = 10**18
ans = -INF
for i in range(n):
    cycle, csum = cycles[visited[i]]
    cmem = len(cycle)

    if 0 < csum:
        a, b = divmod(k, cmem)
        scoreA = csum*(a-1)
        b += cmem
        score = 0
        scoreB = 0
        for _ in range(b):
            i = P[i]
            score += C[i]
            if scoreB < score:
                scoreB = score
        X = scoreA+scoreB
    else:
        b = min(k, cmem)
        score = 0
        INF = 10**18
        X = -INF
        for _ in range(b):
            i = P[i]
            score += C[i]
            if X < score:
                X = score

    if ans < X:
        ans = X
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))


def dfs(i):
    if 0 <= visited[i]:
        return
    visited[i] = idx
    cycle.append(i)
    global csum
    csum += C[i]
    dfs(P[i])


visited = [-1]*n
idx = 0
cycles = []
for i in range(n):
    if 0 <= visited[i]:
        continue
    cycle = []
    csum = 0
    dfs(i)
    cycles.append((cycle, csum))
    idx += 1


INF = 10**18
ans = -INF
for i in range(n):
    cycle, csum = cycles[visited[i]]
    cmem = len(cycle)

    if 0 < csum:
        a, b = divmod(k, cmem)
        scoreA = csum*a
        score = 0
        INF = 10**18
        scoreB = -INF
        for _ in range(b):
            i = P[i]
            score += C[i]
            if scoreB < score:
                scoreB = score
        X = scoreA+scoreB
    else:
        b = min(k, cmem)
        score = 0
        INF = 10**18
        X = -INF
        for _ in range(b):
            i = P[i]
            score += C[i]
            if X < score:
                X = score

    if ans < X:
        ans = X
print(ans)
Problem p02670 (python)

Fixed submission:
from collections import deque
n = int(input())
Ps = list(map(lambda x: int(x)-1, input().split()))
cost = [min(i, n-1-i, j, n-1-j) for i in range(n) for j in range(n)]
exist = [1]*(n**2)


def to_idx(p):
    return divmod(p, n)


def to_v(i, j):
    return i*n+j


def bfs(init_v):
    next_v = deque([init_v])
    exist[init_v] = 0
    while next_v:
        v = next_v.popleft()
        i, j = to_idx(v)
        if 0 < i:
            v2 = v-n
            if cost[v2] > cost[v]+exist[v]:
                cost[v2] = cost[v]+exist[v]
                next_v.append(v2)

        if i < n-1:
            v2 = v+n
            if cost[v2] > cost[v]+exist[v]:
                cost[v2] = cost[v]+exist[v]
                next_v.append(v2)

        if 0 < j:
            v2 = v-1
            if cost[v2] > cost[v]+exist[v]:
                cost[v2] = cost[v]+exist[v]
                next_v.append(v2)

        if j < n-1:
            v2 = v+1
            if cost[v2] > cost[v]+exist[v]:
                cost[v2] = cost[v]+exist[v]
                next_v.append(v2)

ans = 0
for p in Ps:
    ans += cost[p]
    bfs(p)
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
n = int(input())
Ps = list(map(lambda x: int(x)-1, input().split()))
cost = [min(i, n-1-i, j, n-1-j) for i in range(n) for j in range(n)]
DIRC = [(-1, 0), (0, 1), (1, 0), (0, -1)]
exist = [1]*(n**2)


def to_idx(p):
    return divmod(p, n)


def to_v(i, j):
    return i*n+j


def bfs(init_v):
    next_v = deque([init_v])
    exist[init_v] = 0
    while next_v:
        v = next_v.popleft()
        i, j = to_idx(v)
        for di, dj in DIRC:
            x, y = i+di, j+dj
            if x < 0 or y < 0 or n <= x or n <= y:
                continue
            c = cost[v]+exist[v]
            if cost[to_v(x, y)] <= c:
                continue
            cost[to_v(x, y)] = c
            next_v.append((x, y))


ans = 0
for p in Ps:
    ans += cost[p]
    bfs(p)
print(ans)
Problem p02684 (python)

Fixed submission:
from collections import Counter
import sys
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))

# A[0]から出発して何回でループするか数える

visited = [0]*n


def dfs(v):
    if visited[v]+1 == 3:
        return 0
    visited[v] += 1
    dfs(A[v])


dfs(0)

c = Counter(visited)
loop_cnt = c[2]
init_cnt = c[1]
k_ = k-init_cnt
k_ %= loop_cnt

cnt = 0
visited = [0]*n


ans = 0
loop = k if k < init_cnt else k_+init_cnt
for _ in range(loop):
    ans = A[ans]
print(ans+1)

Buggy submission (verdict: Runtime Error):
from collections import Counter
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))


visited = [0]*n


def dfs(v):
    if visited[v]+1 == 3:
        return 0
    visited[v] += 1
    dfs(A[v])


dfs(0)

c = Counter(visited)
loop_cnt = c[2]
init_cnt = c[1]
k -= init_cnt
k %= loop_cnt

cnt = 0
visited = [0]*n


ans = 0
for _ in range(k+init_cnt):
    ans = A[ans]
print(ans+1)

Buggy submission (verdict: Runtime Error):
from collections import Counter
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))


visited = [0]*n


def dfs(v):
    if visited[v]+1 == 3:
        return 0
    visited[v] += 1
    dfs(A[v])


dfs(0)

c = Counter(visited)
loop_cnt = c[2]
init_cnt = c[1]
k -= init_cnt
k %= loop_cnt

cnt = 0
visited = [0]*n


ans = 0
for _ in range(k+init_cnt):
    ans = A[ans]
print(ans+1)