Problem p03603 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10**6)

N=int(input())
P=list(map(int,input().split()))
child=[[] for i in range(N)]
for i in range(N-1):
    child[P[i]-1].append(i+1)

X=list(map(int,input().split()))

dpb=[float("inf") for i in range(N)]
dpw=[float("inf") for i in range(N)]

def dfs(v):
    for nv in child[v]:
        dfs(nv)
    if child[v]:
        subdpb=[[float("inf") for i in range(0,X[v]+1)] for j in range(len(child[v]))]
        subdpw=[[float("inf") for i in range(0,X[v]+1)] for j in range(len(child[v]))]
        for i in range(X[v]+1):
            nv=child[v][0]
            if i>=X[nv]:
                subdpb[0][i]=min(subdpb[0][i],dpb[nv])
            if i>=dpw[nv]:
                subdpb[0][i]=min(subdpb[0][i],X[nv])
        for i in range(1,len(child[v])):
            nv=child[v][i]
            for j in range(X[nv],X[v]+1):
                subdpb[i][j]=min(subdpb[i][j],subdpb[i-1][j-X[nv]]+dpb[nv])
            if dpw[nv]!=float("inf"):
                for j in range(dpw[nv],X[v]+1):
                    subdpb[i][j]=min(subdpb[i][j],subdpb[i-1][j-dpw[nv]]+X[nv])

        for i in range(X[v]+1):
            nv=child[v][0]
            if i>=X[nv]:
                subdpw[0][i]=min(subdpw[0][i],dpw[nv])
            if i>=dpb[nv]:
                subdpw[0][i]=min(subdpw[0][i],X[nv])
        for i in range(1,len(child[v])):
            nv=child[v][i]
            for j in range(X[nv],X[v]+1):
                subdpw[i][j]=min(subdpw[i][j],subdpw[i-1][j-X[nv]]+dpw[nv])
            if dpb[nv]!=float("inf"):
                for j in range(dpb[nv],X[v]+1):
                    subdpw[i][j]=min(subdpw[i][j],subdpw[i-1][j-dpb[nv]]+X[nv])

        dpb[v]=subdpb[-1][X[v]]
        dpw[v]=subdpw[-1][X[v]]

    else:
        dpb[v]=0
        dpw[v]=0

dfs(0)

if min(dpb[0],dpw[0])==float("inf"):
    print("IMPOSSIBLE")
else:
    print("POSSIBLE")

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10**6)

N=int(input())
P=list(map(int,input().split()))
child=[[] for i in range(N)]
for i in range(N-1):
    child[P[i]-1].append(i+1)

X=list(map(int,input().split()))

dpb=[float("inf") for i in range(N)]
dpw=[float("inf") for i in range(N)]

def dfs(v):
    for nv in child[v]:
        dfs(nv)
    if child[v]:
        subdpb=[[float("inf") for i in range(0,X[v]+1)] for j in range(len(child[v]))]
        subdpw=[[float("inf") for i in range(0,X[v]+1)] for j in range(len(child[v]))]
        for i in range(X[v]+1):
            nv=child[v][0]
            if i>=X[nv]:
                subdpb[0][i]=min(subdpb[0][1],dpb[nv])
            if i>=dpw[nv]:
                subdpb[0][i]=min(subdpb[0][1],X[nv])
        for i in range(1,len(child[v])):
            nv=child[v][i]
            for j in range(X[nv],X[v]+1):
                subdpb[i][j]=min(subdpb[i][j],subdpb[i-1][j-X[nv]]+dpb[nv])
            for j in range(dpw[nv],X[v]+1):
                subdpb[i][j]=min(subdpb[i][j],subdpb[i-1][j-dpw[nv]]+X[nv])

        for i in range(X[v]+1):
            nv=child[v][0]
            if i>=X[nv]:
                subdpw[0][i]=min(subdpw[0][1],dpw[nv])
            if i>=dpb[nv]:
                subdpw[0][i]=min(subdpw[0][1],X[nv])
        for i in range(1,len(child[v])):
            nv=child[v][i]
            for j in range(X[nv],X[v]+1):
                subdpw[i][j]=min(subdpw[i][j],subdpw[i-1][j-X[nv]]+dpw[nv])
            for j in range(dpb[nv],X[v]+1):
                subdpw[i][j]=min(subdpw[i][j],subdpw[i-1][j-dpb[nv]]+X[nv])

        dpb[v]=subdpb[-1][X[v]]
        dpw[v]=subdpw[-1][X[v]]

    else:
        dpb[v]=0
        dpw[v]=0

dfs(0)

if min(dpb[0],dpw[0])==float("inf"):
    print("IMPOSSIBLE")
else:
    print("POSSIBLE")

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10**6)

N=int(input())
P=list(map(int,input().split()))
child=[[] for i in range(N)]
for i in range(N-1):
    child[P[i]-1].append(i+1)

X=list(map(int,input().split()))

dpb=[float("inf") for i in range(N)]
dpw=[float("inf") for i in range(N)]

def dfs(v):
    for nv in child[v]:
        dfs(nv)
    subdpb=[[float("inf") for i in range(0,X[v]+1)] for j in range(len(child[v]))]
    subdpw=[[float("inf") for i in range(0,X[v]+1)] for j in range(len(child[v]))]
    if child[v]:
        for i in range(X[v]+1):
            nv=child[v][0]
            if i>=X[nv]:
                subdpb[0][i]=min(subdpb[0][1],dpb[nv])
            if i>=dpw[nv]:
                subdpb[0][i]=min(subdpb[0][1],X[nv])
        for i in range(1,len(child[v])):
            nv=child[v][i]
            for j in range(X[nv],X[v]+1):
                subdpb[i][j]=min(subdpb[i][j],subdpb[i-1][j-X[nv]]+dpb[nv])
            for j in range(dpw[nv],X[v]+1):
                subdpb[i][j]=min(subdpb[i][j],subdpb[i-1][j-dpw[nv]]+X[nv])

        for i in range(X[v]+1):
            nv=child[v][0]
            if i>=X[nv]:
                subdpw[0][i]=min(subdpw[0][1],dpw[nv])
            if i>=dpb[nv]:
                subdpw[0][i]=min(subdpw[0][1],X[nv])
        for i in range(1,len(child[v])):
            nv=child[v][i]
            for j in range(X[nv],X[v]+1):
                subdpw[i][j]=min(subdpw[i][j],subdpw[i-1][j-X[nv]]+dpw[nv])
            for j in range(dpb[nv],X[v]+1):
                subdpw[i][j]=min(subdpw[i][j],subdpw[i-1][j-dpb[nv]]+X[nv])

        dpb[v]=subdpb[-1][X[v]]
        dpw[v]=subdpw[-1][X[v]]

    else:
        dpb[v]=0
        dpw[v]=0

dfs(0)

if min(dpb[0],dpw[0])==float("inf"):
    print("IMPOSSIBLE")
else:
    print("POSSIBLE")