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

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), max(score), ans)

    #print('sum_cycle:', sum_cycle, 'cycle:', cycle, 'ans:', ans)
    #print(i+1, score)
print(ans)

Fixed submission:
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

if n >= k:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        for j in range(k):
            now = p[now] - 1
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
        ans = max(max(score), ans)
    print(ans)
else:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        check = [0] * n
        for j in range(k):
            go = p[now] - 1
            if check[go] == 1:
                break
            now = go
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
            check[now] = 1
        d = k % j
        t = k // j
        score_2 = []
        for j in range(d):
            now = p[now] - 1
            if j == 0:
                score_2.append(c[now])
            else:
                score_2.append(score_2[j-1] + c[now])
        res = max(score)
        res_2 = score[-1] * t + max(score_2)
        ans = max(ans, res, res_2)

    print(ans)

Fixed submission:
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

if n >= k:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        for j in range(k):
            now = p[now] - 1
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
        ans = max(max(score), ans)
else:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        check = [0] * n
        cnt = 0
        for j in range(k):
            go = p[now] - 1
            if check[go] == 1:
                break
            now = go
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
            check[now] = 1
            cnt += 1
        d = k // cnt
        e = k % cnt
        ans = max(score[-1] * d + max(score[:e]), max(score), ans)
print(ans)

Fixed submission:
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

if n >= k:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        for j in range(k):
            now = p[now] - 1
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
        ans = max(max(score), ans)
    print(ans)
else:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        check = [0] * n
        for j in range(k):
            go = p[now] - 1
            if check[go] == 1:
                break
            now = go
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
            check[now] = 1
        d = k % j
        t = k // j
        score_2 = []
        for j in range(d):
            now = p[now] - 1
            if j == 0:
                score_2.append(c[now])
            else:
                score_2.append(score_2[j-1] + c[now])
        res = max(score)
        res_2 = score[-1] * t + max(score_2)
        ans = max(ans, res, res_2)

    print(ans)

Fixed submission:
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

if n >= k:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        for j in range(k):
            now = p[now] - 1
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j] + c[now])
        ans = max(max(score), ans)
    print(ans)
else:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        check = [0] * n
        for j in range(k):
            go = p[now] - 1
            if check[go] == 1:
                break
            now = go
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j] + c[now])
            check[now] = 1
        d = k % j
        t = k // j
        score_2 = []
        for j in range(d):
            now = p[now] - 1
            if j == 0:
                score_2.append(c[now])
            else:
                score_2.append(score_2[j] + c[now])
        res = max(score)
        res_2 = score[-1] * t + max(score_2)
        ans = max(ans, res, res_2)

    print(ans)

Fixed submission:
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

if n >= k:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        for j in range(k):
            now = p[now] - 1
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
        ans = max(max(score), ans)
else:
    ans = -float('inf')
    for i in range(n):
        now = i
        score = []
        check = [0] * n
        cnt = 0
        for j in range(k):
            go = p[now] - 1
            if check[go] == 1:
                break
            now = go
            if j == 0:
                score.append(c[now])
            else:
                score.append(score[j-1] + c[now])
            check[now] = 1
            cnt += 1

        if score[-1] >= 0:
            d = k // cnt
            e = k % cnt
            res = score[-1] * d
            score = []
            for j in range(e):
                now = p[now] - 1
                if j == 0:
                    score.append(c[now])
                else:
                    score.append(score[j-1] + c[now])
            res += max(score)
        else:
            res = max(score)
        ans = max(ans, res)
print(ans)

Fixed submission:
n, k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans)

print(ans)