Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
a,b,c,d,e,f=map(int,input().split())
ans=0
s=0
t=0
for i in range(f//100+1):
    if i==0 and j==0:
        continue
    w=(f-100*a*i)//100
    for j in range(w+1):
        x=a*i+b*j
        for k in range(x*e//c+1):
            if k==0 and l==0:
                continue
            v=(x*e-c*k)//d
            for l in range(v+1):
                y=c*k+d*l
                if 100*x+y <=f:
                    p=100*y/(100*x+y)
                    if ans<p:
                        s=100*x+y
                        t=y
print(s,t)

Fixed submission:
a,b,c,d,e,f=map(int,input().split())
ans=0
s=0
t=0
for i in range(f//100+1):
    w=(f-100*a*i)//100
    for j in range(w+1):
        if i==0 and j==0:
            continue
        x=a*i+b*j
        for k in range(x*e//c+1):
            v=(x*e-c*k)//d
            for l in range(v+1):
                y=c*k+d*l
                if 100*x+y <=f:
                    p=100*y/(100*x+y)
                    if ans<=p:
                        s=100*x+y
                        t=y
                        ans=p
print(s,t)

Buggy python submission (verdict: Runtime Error):
import sys
a,b,c,d,e,f=map(int,input().split())
ans=0
s=0
t=0
for i in range(f//100+1):
    if i==0 and j==0:
        continue
    w=(f-100*a*i)//100
    for j in range(w+1):
        x=a*i+b*j
        for k in range(x*e//c+1):
            v=(x*e-c*k)//d
            for l in range(v+1):
                y=c*k+d*l
                if 100*x+y <=f:
                    p=100*y/(100*x+y)
                    if ans<p:
                        s=100*x+y
                        t=y
print(s,t)

Fixed submission:
a,b,c,d,e,f=map(int,input().split())
ans=0
s=0
t=0
for i in range(f//100+1):
    w=(f-100*a*i)//100
    for j in range(w+1):
        if i==0 and j==0:
            continue
        x=a*i+b*j
        for k in range(x*e//c+1):
            v=(x*e-c*k)//d
            for l in range(v+1):
                y=c*k+d*l
                if 100*x+y <=f:
                    p=100*y/(100*x+y)
                    if ans<=p:
                        s=100*x+y
                        t=y
                        ans=p
print(s,t)
Buggy python submission (verdict: Runtime Error):
import sys
import copy

N = int(input())
table = []
for i in range(N):
    A = [int(j) for j in input().split()]
    table.append(A)
tas = copy.deepcopy(table)
for k in range(N):
    for i in range(N):
        for j in range(N):
            tas[j][i] = min(tas[j][i], tas[j][k] + tas[k][i])

for y in range(N):
    for x in range(y+1, N):
        if A[y][x] != tas[y][x]:
            print(-1)
            sys.exit()
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        flag = True
        for k in range(N):
            if i == k or j == k:
                continue
            if table[j][i] >= table[j][k] + table[k][i]:
                flag = False
        if flag:
            ans += table[j][i]
print(ans)



Fixed submission:
import sys
import copy

N = int(input())
table = []
for i in range(N):
    A = [int(j) for j in input().split()]
    table.append(A)
tas = copy.deepcopy(table)
for k in range(N):
    for i in range(N):
        for j in range(N):
            tas[j][i] = min(tas[j][i], tas[j][k] + tas[k][i])

for y in range(N):
    for x in range(y+1, N):
        if table[y][x] != tas[y][x]:
            print(-1)
            sys.exit()
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        flag = True
        for k in range(N):
            if i == k or j == k:
                continue
            if table[j][i] >= table[j][k] + table[k][i]:
                flag = False
        if flag:
            ans += table[j][i]
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
import copy
N=int(input())
table=[]
for i in range(N):
    A=[int(j) for j in input().split() ]
    table.append(A)
tas=copy.deepcopy(table)
for k in range(N):
    for i in range(N):
        for j in range(N):
            tas[j][i]=min(tas[j][i],tas[j][k]+tas[k][i])
            
for y in range(N):
    for x in range(N):
        if A[y][x]!=tas[y][x]:
            print(-1)
            sys.exit()
ans=0
for i in range(N):
    for j in range(i+1,N):
        flag =True
        for k in range(N):
            if i==k or j==k:
                continue
            if table[j][i]==table[j][k] +table[k][i]:
                flag = False
        if flag:        
            ans+=table[j][i]
print(ans)

Fixed submission:
import sys
import copy

N = int(input())
table = []
for i in range(N):
    A = [int(j) for j in input().split()]
    table.append(A)
tas = copy.deepcopy(table)
for k in range(N):
    for i in range(N):
        for j in range(N):
            tas[j][i] = min(tas[j][i], tas[j][k] + tas[k][i])

for y in range(N):
    for x in range(y+1, N):
        if table[y][x] != tas[y][x]:
            print(-1)
            sys.exit()
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        flag = True
        for k in range(N):
            if i == k or j == k:
                continue
            if table[j][i] >= table[j][k] + table[k][i]:
                flag = False
        if flag:
            ans += table[j][i]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
N,M,R=map(int,input().split())
r = [int(i) for i in input().split()]
inf=10**10
#[k][j][i]
dp=[[[inf]*N for i in range(N)]for i in range(N+1)]
for i in range(M):
    a,b,c=map(int,input().split())
    dp[0][a-1][b-1]=c
    dp[0][b-1][a-1]=c
for k in range(1,N+1):
    for j in range(N):
        for i in range(N):
            dp[k][j][i]=min(dp[k-1][j][i],dp[k-1][j][k-1]+dp[k-1][k-1][i])

ans =inf
for elem in permutations(r, R):
    dis=0
    for i in range(R-1):
        dis+=dp[N][elem[i+1]-1][elem[i]-1]
    ans=min(ans,dis)
print(ans)


Fixed submission:
from itertools import permutations
N,M,R=map(int,input().split())
r = [int(i) for i in input().split()]
inf=10**10
#[k][j][i]
dp=[[[inf]*N for i in range(N)]for i in range(N+1)]
for i in range(M):
    a,b,c=map(int,input().split())
    dp[0][a-1][b-1]=c
    dp[0][b-1][a-1]=c
for k in range(1,N+1):
    for j in range(N):
        for i in range(N):
            dp[k][j][i]=min(dp[k-1][j][i],dp[k-1][j][k-1]+dp[k-1][k-1][i])

ans =inf
for elem in permutations(r, R):
    dis=0
    for i in range(R-1):
        dis+=dp[N][elem[i+1]-1][elem[i]-1]
    ans=min(ans,dis)
print(ans)
Buggy python submission (verdict: Runtime Error):
class Unionfindtree:
    def __init__(self,number):
        self.par = [i for i in range(number)]
        self.rank = [0]*(number)

    def find(self,x):#親を探す
        if self.par[x] ==x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self,x,y):#x,yを繋げる
        px = self.find(x)
        py = self.find(y)
        if px == py:
            return 
        if self.rank[px]<self.rank[py]:
            self.par[px]=py
        else:
            self.par[py]=px
        if self.rank[px]==self.rank[py]:
            self.rank[px] +=1
    
    def connect(self,x,y):#親が同じかみる
        return self.find(x)==self.find(y)
    
btree = Unionfindtree(N)
wtree = Unionfindtree(N)
N = int(input())
table=[]
for i in range(N-1):
    a,b=map(int,input().split())
    table.append([a-1,b-1])
for x,y in table:
    if x==N-1 or y==N-1:
        continue
    btree.union(x,y)
brack=0
for i in range(N):
    if btree.connect(0,i):
        brack+=1
for x,y in table:
    if x==0 or y==0:
        continue
    wtree.union(x,y)
white=0
for i in range(N):
    if btree.connect(N-1,i):
        white+=1

mid = (brack+white)-N
brack-= mid
white-=mid
if mid %2==0:
    brack+= mid//2
    white+=mid//2
else:
    brack+= (mid//2)+1
    white+=mid//2    
    
if brack>white:
    print('Fennec')
else:
    print('Snuke')

Fixed submission:
from collections import deque
N = int(input())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    table[a-1].append(b-1)
    table[b-1].append(a-1)
adis=[0]*N
avis=[False]*N
avis[0]=True
bdis=[0]*N
bvis=[False]*N
bvis[N-1]=True
akei=deque()
akei.append(0)
bkei=deque()
bkei.append(N-1)
while akei:
    x = akei.popleft()
    for s in table[x]:
        if not avis[s]:
            adis[s]=adis[x]+1
            avis[s]=True
            akei.append(s)
while bkei:
    x = bkei.popleft()
    for s in table[x]:
        if not bvis[s]:
            bdis[s]=bdis[x]+1
            bvis[s]=True
            bkei.append(s)
white=0
brack=0
for i in range(N):
    if adis[i]<=bdis[i]:
        brack+=1
    else:
        white+=1
if white<brack:
    print('Fennec')
else:
    print('Snuke')
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[i for i in input().split()]
s=A[0]
if N%2==1:
    for i in range(1,N):
        if i%2==1:
            s=s+' '+A[i]
        else:
            s=A[i]+' ' +s
else:
    for i in range(1,N):
        if i%2==0:
            s=s+' '+A[i]
        else:
            s=s=A[i]+' ' +s
print(s)

Fixed submission:
from collections import deque
N=int(input())
A=[i for i in input().split()]
s=deque()
s.append(A[0])
if N%2==1:
    for i in range(1,N):
        if i%2==1:
            s.append(A[i])
        else:
            s.appendleft(A[i])
else:
    for i in range(1,N):
        if i%2==0:
            s.append(A[i])
        else:
            s.appendleft(A[i])
print(' '.join(map(str,s)))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[i for i in input().split()]
s=[A[0]]
if N%2==1:
    for i in range(1,N):
        if i%2==1:
            s=s+[A[i]]
        else:
            s=[A[i]]+s
else:
    for i in range(1,N):
        if i%2==0:
            s=s+[A[i]]
        else:
            s=[A[i]]+s 
print(' '.join(map(str,s)))

Fixed submission:
from collections import deque
N=int(input())
A=[i for i in input().split()]
s=deque()
s.append(A[0])
if N%2==1:
    for i in range(1,N):
        if i%2==1:
            s.append(A[i])
        else:
            s.appendleft(A[i])
else:
    for i in range(1,N):
        if i%2==0:
            s.append(A[i])
        else:
            s.appendleft(A[i])
print(' '.join(map(str,s)))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[i for i in input().split()]
s=[A[0]]
if N%2==1:
    for i in range(1,N):
        if i%2==1:
            s=s+[A[i]]
        else:
            s=[A[i]]+s
else:
    for i in range(1,N):
        if i%2==0:
            s=s+[A[i]]
        else:
            s=[A[i]]+s 
print(' '.join(map(str,s)))

Fixed submission:
from collections import deque
N=int(input())
A=[i for i in input().split()]
s=deque()
s.append(A[0])
if N%2==1:
    for i in range(1,N):
        if i%2==1:
            s.append(A[i])
        else:
            s.appendleft(A[i])
else:
    for i in range(1,N):
        if i%2==0:
            s.append(A[i])
        else:
            s.appendleft(A[i])
print(' '.join(map(str,s)))
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=[int(i) for i in input().split()]
mod=10**9+7
deni=[1]*(N+1)#(k)!を格納
quot=[1]*(N+1)#(k)!の逆数を格納
for i in range(1,N+1):
    deni[i]=(deni[i-1]*(i+1))%mod
for i in range(1,N+1):
    t=pow(i+1,mod-2,mod)
    quot[i]=(quot[i-1]*t)%mod
deni=[1]+deni
quot=[1]+quot
box=[[0,0]]*(N+1)
for i in range(N):
    if box[A[i]-1][0]==1:
        k_1=box[A[i]-1][1]
        k_2=N-i
        break
    box[A[i]-1]=[1,i]
K=k_1+k_2
ans =[]
for k in range(1,N+2):
    if k==1:
        ans.append(N)
    elif K>=k-1:
        s=deni[N+1]
        s=(s*quot[k])%mod
        s=(s*quot[N+1-k])%mod
        t=deni[K]
        t=(t*quot[k-1])%mod
        t=(t*quot[K-k+1])%mod
        #print(s,t)
        ans.append((s-t)%mod)
    else:
        s=deni[N+1]
        s=(s*quot[k])%mod
        s=(s*quot[N-k+1])%mod
        #print(s)
        ans.append(s)
#print(ans)
print('\n'.join(map(str,ans)))

Fixed submission:
N=int(input())
A=[int(i) for i in input().split()]
mod=10**9+7
deni=[1]*(N+1)#(k)!を格納
quot=[1]*(N+1)#(k)!の逆数を格納
for i in range(1,N+1):
    deni[i]=(deni[i-1]*(i+1))%mod
for i in range(1,N+1):
    t=pow(i+1,mod-2,mod)
    quot[i]=(quot[i-1]*t)%mod
deni=[1]+deni
quot=[1]+quot
box=[[0,0]]*(N+1)
for i in range(N+1):
    if box[A[i]-1][0]==1:
        k_1=box[A[i]-1][1]
        k_2=N-i
        break
    box[A[i]-1]=[1,i]
K=k_1+k_2
ans =[]
for k in range(1,N+2):
    if k==1:
        ans.append(N)
    elif K>=k-1:
        s=deni[N+1]
        s=(s*quot[k])%mod
        s=(s*quot[N+1-k])%mod
        t=deni[K]
        t=(t*quot[k-1])%mod
        t=(t*quot[K-k+1])%mod
        #print(s,t)
        ans.append((s-t)%mod)
    else:
        s=deni[N+1]
        s=(s*quot[k])%mod
        s=(s*quot[N-k+1])%mod
        #print(s)
        ans.append(s)
#print(ans)
print('\n'.join(map(str,ans)))
Buggy python submission (verdict: Runtime Error):
class FK:
    def __init__(self,n):
        self.table=[[0]*n for i in range(n)]
        self.n=n
    
    def add(self,x,y,f):
        self.table[x][y]=f
        
    def ford(self,s,t,f):
        self.visit[s]=True
        if s==t:
            return f
        for i in range(self.n):
            if (not self.visit[i]) and self.table[s][i]>0:
                df=self.ford(i,t,min(f,self.table[s][i]))
                if df>0:
                    self.table[s][i]-=df
                    self.table[i][s]+=df
                    return df
        return 0
    
    def flow(self,s,t):
        ans=0
        inf=10**20
        while True:
            self.visit=[False]*(self.n)
            df=self.ford(s,t,inf)
            if df==0:
                break
            ans+=df
        return ans

H,W=map(int,input().split())
C=[input() for i in range(H)]
maxflow=FK(W+H+2)
inf=10**20
for i in range(W):
    for j in range(H):
        if C[j][i]=='S':
            sx=i
            sy=j
            maxflow.add(0,i+1,inf)
            maxflow.add(0,W+1+j,inf)
            continue
        if C[j][i]=='T':
            gx=i
            gy=j
            maxflow.add(i+1,W+H+1,inf)
            maxflow.add(W+1+j,W+H+1,inf)
            continue
        if C[j][i]=='o':
            maxflow.add(i+1,W+1+j,1)
            maxflow.add(W+1+j,i+1,1)
#print(maxflow.table)
if gx==sx or gy==sy:
    print('-1')
    sys.exit()
ans=maxflow.flow(0,H+W+1)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
class FK:
    def __init__(self,n):
        self.table=[[0]*n for i in range(n)]
        self.n=n
    
    def add(self,x,y,f):
        self.table[x][y]=f
        
    def ford(self,s,t,f):
        self.visit[s]=True
        if s==t:
            return f
        for i in range(self.n):
            if (not self.visit[i]) and self.table[s][i]>0:
                df=self.ford(i,t,min(f,self.table[s][i]))
                if df>0:
                    self.table[s][i]-=df
                    self.table[i][s]+=df
                    return df
        return 0
    
    def flow(self,s,t):
        ans=0
        inf=10**20
        while True:
            self.visit=[False]*(self.n)
            df=self.ford(s,t,inf)
            if df==0:
                break
            ans+=df
        return ans

H,W=map(int,input().split())
C=[input() for i in range(H)]
maxflow=FK(W+H+2)
inf=10**20
for i in range(W):
    for j in range(H):
        if C[j][i]=='S':
            sx=i
            sy=j
            maxflow.add(0,i+1,inf)
            maxflow.add(0,W+1+j,inf)
            continue
        if C[j][i]=='T':
            gx=i
            gy=j
            maxflow.add(i+1,W+H+1,inf)
            maxflow.add(W+1+j,W+H+1,inf)
            continue
        if C[j][i]=='o':
            maxflow.add(i+1,W+1+j,1)
            maxflow.add(W+1+j,i+1,1)
#print(maxflow.table)
if gx==sx or gy==sy:
    print('-1')
    sys.exit()
ans=maxflow.flow(0,H+W+1)
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N=int(input())
A=[]
C=[]
D=[]
for i in range(N):
    a,b=map(int,input().split())
    c,d=min(a,b),max(a,b)
    A.append((c,d))
    C.append(c)
    D.append(d)
#print(A,C,D,si,ti)
if N==1:
    print(0)
    sys.exit()
ans=(max(C)-min(C))*(max(D)-min(D))
cur=min(D)
ma=min(D)
T=10**19
for a,b in A:
    if a>ma:
        T=min(best,cur-ma)
        break
    T=min(T,cur-a)
    cur=max(cur,b)

print(min(ans,(max(D)-min(C))*T))

Fixed submission:
import sys
input = sys.stdin.readline
N=int(input())
A=[]
C=[]
D=[]
for i in range(N):
    a,b=map(int,input().split())
    c,d=min(a,b),max(a,b)
    A.append((c,d))
    C.append(c)
    D.append(d)
A.sort()
#print(A,C,D,si,ti)
if N==1:
    print(0)
    sys.exit()
ans=(max(C)-min(C))*(max(D)-min(D))
cur=max(C)
ma=min(D)
T=10**19
for a,b in A:
    if a>ma:
        T=min(T,cur-ma)
        break
    T=min(T,cur-a)
    cur=max(cur,b)
print(min(ans,(max(D)-min(C))*T))
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from collections import deque
N,M=map(int,input().split())
table=[[] for i in range(N)]
for i in range(M):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)
Q=int(input())
X=[[int(i) for i in input().split()] for i in range(Q)]
color=[0]*Q
dp=[[-1]*N for i in range(10)]
for i in range(Q-1,-1,-1):
    v, d, c=X[i]
    v=v-1
    color[i]=c
    dp[d][v]=i

for d in range(9,0,-1):
    for x in range(N):
        for y in table[x]:
            dp[d-1][y]=max(dp[d][x],dp[d-1][y])
for i in range(N):
    s=-1
    for d in range(10):
        s=max(s,dp[d][i])
    if s==-1:
        print(0)
    else:
        print(color[s])




Fixed submission:
import sys
input = sys.stdin.readline
from collections import deque
N,M=map(int,input().split())
table=[[] for i in range(N)]
for i in range(M):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)
Q=int(input())
X=[[int(i) for i in input().split()] for i in range(Q)]
color=[0]*Q
dp=[[-1]*N for i in range(11)]
for i in range(Q-1,-1,-1):
    v, d, c=X[i]
    v=v-1
    color[i]=c
    dp[d][v]=max(dp[d][v],i)

for d in range(10,0,-1):
    for x in range(N):
        for y in table[x]:
            dp[d-1][y]=max(dp[d][x],dp[d-1][y])
for i in range(N):
    s=-1
    for d in range(11):
        s=max(s,dp[d][i])
    if s==-1:
        print(0)
    else:
        print(color[s])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import deque
N,M=map(int,input().split())
table=[[] for i in range(N)]
for i in range(M):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)
Q=int(input())
X=[[int(i) for i in input().split()] for i in range(Q)]
color=[0]*N
def f(s,d,v):
    H=deque()
    H.append((0,s))
    color[s]=v
    visit=[False]*N
    visit[s]=True
    while H:
        dis,pt=H.popleft()
        if dis>=d:
            continue
        for x in table[pt]:
            if not visit[x]:
                color[x]=v
                visit[x]=True
                H.append((dis+1,x))
    return
for v,d,c in X:
    v=v-1
    f(v,d,c)
    #print(color)
for i in color:
    print(i)



Fixed submission:
import sys
input = sys.stdin.readline
from collections import deque
N,M=map(int,input().split())
table=[[] for i in range(N)]
for i in range(M):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)
Q=int(input())
X=[[int(i) for i in input().split()] for i in range(Q)]
color=[0]*Q
dp=[[-1]*N for i in range(11)]
for i in range(Q-1,-1,-1):
    v, d, c=X[i]
    v=v-1
    color[i]=c
    dp[d][v]=max(dp[d][v],i)

for d in range(10,0,-1):
    for x in range(N):
        for y in table[x]:
            dp[d-1][y]=max(dp[d][x],dp[d-1][y])
for i in range(N):
    s=-1
    for d in range(11):
        s=max(s,dp[d][i])
    if s==-1:
        print(0)
    else:
        print(color[s])
Buggy python submission (verdict: Time Limit Exceeded):
N,M,K=map(int,input().split())
table=[]
for i in range(N):
    a,b,c=map(int,input().split())
    table.append([a,b,c])

inf=10**9
#[N+1][400a][400b]
dp=[[[inf]*401 for i in range(401)]for i in range(N+1)]
dp[0][0][0]=0
for i in range(1,N+1):
    a,b,c = table[i-1]
    for j in range(401):
        for k in range(401):
            dp[i][j][k]=dp[i-1][j][k]
            if 400>=j-a>=0 and 400>=k-b>=0:
                dp[i][j][k] =min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)

ans =10**9
i=1
while max(M,K)*i <401:
    ans = min(ans,dp[N][M*i][K*i])
    i=i+1
if ans ==10**9:
    print(-1)
else:
    print(ans)


Fixed submission:
N,M,K=map(int,input().split())
table=[]
for i in range(N):
    a,b,c=map(int,input().split())
    table.append([a,b,c])

inf=10**9
#[N+1][400a][400b]
dp=[[[inf]*401 for i in range(401)]for i in range(N+1)]
dp[0][0][0]=0
for i in range(1,N+1):
    a,b,c = table[i-1]
    for j in range(401):
        for k in range(401):
            dp[i][j][k]=dp[i-1][j][k]
            if 400>=j-a>=0 and 400>=k-b>=0:
                dp[i][j][k] =min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)

ans =10**9
i=1
while max(M,K)*i <401:
    ans = min(ans,dp[N][M*i][K*i])
    i=i+1
if ans ==10**9:
    print(-1)
else:
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
#from collections import deque
N=int(input())
A=[int(input())-1 for i in range(N-1)]
A=[0]+A
table=[[] for i in range(N)]
for i in range(1,N):
    table[A[i]].append(i)
#print(table)

num=[-1]*N
def dfs(s):
    if num[s]!=-1:
        return num[s]
    child=len(table[s])
    if child==0:
        num[s]=0
        return 0
    L=[]
    n=0
    for y in table[s]:
        x=dfs(y)
        L.append(x)
    L.sort()
    for l in L:
        n=max(n+1,l+1)
    num[s]=n
    return n

dfs(0)
#print(num)
print(num[0])



Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
#from collections import deque
N=int(input())
A=[int(input())-1 for i in range(N-1)]
A=[0]+A
table=[[] for i in range(N)]
for i in range(1,N):
    table[A[i]].append(i)
#print(table)

num=[-1]*N
def dfs(s):
    if num[s]!=-1:
        return num[s]
    child=len(table[s])
    if child==0:
        num[s]=0
        return 0
    L=[]
    n=0
    for y in table[s]:
        x=dfs(y)
        L.append(x)
    L.sort()
    for l in L:
        n=max(n+1,l+1)
    num[s]=n
    return n

dfs(0)
#print(num)
print(num[0])