Problem p02585 (python)

Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, k = map(int, input().split())
p = [int(i)-1 for i in input().split()]
c = list(map(int, input().split()))

ret = - 10 ** 18
for s in range(n):
    nxt = p[s]
    S = [c[nxt]]
    while nxt != s:
        nxt = p[nxt]
        S.append(S[-1]+c[nxt])

    if k<=len(S):
        pt = max(S[:k])
    else:
        if S[-1] <= 0:
            pt = max(S)
        else:
            pt = max(S)
            if k%len(S)!=0:
                pt = max(pt, S[-1]+max(S[:k%len(S)]))
            pt += S[-1]*(k//len(S)-1)
    ret = max(ret, pt)
print(ret)

Buggy submission (verdict: Runtime Error):
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, k = map(int, input().split())
p = [int(i)-1 for i in input().split()]
c = list(map(int, input().split()))

rc = [0] * n
rp = [0] * n
for i in range(n):
    if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = np
ret = - 10 ** 18
for i in range(n):
    if rc[i] == 1:
        if rp[i]>=0:
            ret = max(ret, k*rp[i])
        else:
            ret = max(ret, rp[i])
        continue
    pseq = [0]
    nx = p[i]
    for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
    pseq = pseq[1:]
    if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp)
print(ret)

Buggy submission (verdict: Runtime Error):
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, k = map(int, input().split())
p = [int(i)-1 for i in input().split()]
c = list(map(int, input().split()))

rc = [0] * n
rp = [0] * n

for i in range(n):
    if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = max(0, np)
ret = - 10 ** 18
for i in range(n):
    pseq = [0]
    nx = p[i]
    for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
    pseq = pseq[1:]
    if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp)
print(ret)

Buggy submission (verdict: Runtime Error):
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, k = map(int, input().split())
p = [int(i)-1 for i in input().split()]
c = list(map(int, input().split()))

rc = [0] * n
rp = [0] * n

for i in range(n):
    if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = max(0, np)
ret = - 10 ** 18
for i in range(n):
    pseq = [0]
    nx = p[i]
    for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
    pseq = pseq[1:]
    if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp)
print(ret)

Buggy submission (verdict: Runtime Error):
#!/usr/bin python3
# -*- coding: utf-8 -*-

n, k = map(int, input().split())
p = [int(i)-1 for i in input().split()]
c = list(map(int, input().split()))

rc = [0] * n
rp = [0] * n

for i in range(n):
    if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = max(0, np)
ret = - 10 ** 18
for i in range(n):
    pseq = [0]
    nx = p[i]
    for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
    pseq = pseq[1:]
    if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp)
print(ret)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
pos = [int(i)-1 for i in input().split()]
c = list(map(int, input().split()))
ret = - 10**18
for i in range(n):
    nowsum = [0]
    nxtpos = pos[i]
    while nxtpos != i:
        nowsum.append(nowsum[-1]+c[nxtpos])
        nxtpos = pos[nxtpos]
    nowsum.append(nowsum[-1]+c[nxtpos])
    nowsum = nowsum[1:]
    if k<len(nowsum):
        ret = max(ret, max(nowsum[:k]))
    else:
        if nowsum[-1]<0:
            ret = max(ret, max(nowsum))
        else:
            ret = max(ret, nowsum[-1]*(k//len(nowsum)-1)+max(max(nowsum), nowsum[-1]+max(nowsum[:k%len(nowsum)])))
print(ret)