Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))
 
has_visited = [False]*N
score_max = -float('inf')
 
for i in range(N):
    if has_visited[i]:
        continue
    
    loop_val = []
 
    j = i
    while True:
        loop_val.append(C[j])
        has_visited[j] = True
        j = P[j] - 1
        if loop_val[0] == C[j]:
            break
    
    # sum_arr = [0]
    # loop_length = len(loop_val)
 
    # for k in range(1, loop_length + K):
    # sum_arr.append(sum_arr[k-1] + loop_val[(k - 1) % loop_length])
 
    # print(loop_val)
 
    sm = 0
    for p in range(len(loop_val)):
        sm += loop_val[p]
    
    # print(sm)
    
    loop_length = len(loop_val)
 
    for l in range(loop_length):
        res = -1
        s = 0
        for k in range(K):
            s = max([s + loop_val[(k+l) % loop_length], loop_val[(k+l) % loop_length]])
            res = max([res, s])
        
        if res == -1:
            res = max(loop_val)
 
        if score_max < res:
            score_max = res
 
print(score_max)

Fixed submission:
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

res = -10**10

for i in range(N):
    j = i
    total = 0
    loop_values = []

    while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
    loop_length = len(loop_values)
    point_sum = 0

    for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

res = -10**10

for i in range(N):
    j = i
    total = 0
    loop_values = []

    while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
    loop_length = len(loop_values)
    point_sum = 0

    for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand

print(res)

Fixed submission:
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

res = -10**10

for i in range(N):
    j = i
    total = 0
    loop_values = []

    while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
    loop_length = len(loop_values)
    point_sum = 0

    for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

has_visited = [False]*N
score_max = -float('inf')

for i in range(N):
    if has_visited[i]:
        continue
    
    loop_val = []

    j = i
    while True:
        loop_val.append(C[j])
        has_visited[j] = True
        j = P[j] - 1
        if loop_val[0] == C[j]:
            break
    
    # sum_arr = [0]
    # loop_length = len(loop_val)

    # for k in range(1, loop_length + K):
    # sum_arr.append(sum_arr[k-1] + loop_val[(k - 1) % loop_length])

    # print(loop_val)

    sm = 0
    for p in range(len(loop_val)):
        sm += loop_val[p]
    
    # print(sm)
    
    loop_length = len(loop_val)

    for l in range(loop_length):
        res = -1
        s = 0
        for k in range(K):
            s = max([s + loop_val[(k+l) % loop_length], loop_val[(k+l) % loop_length]])
            res = max([res, s])
        
        if res == -1:
            res = max(loop_val)
        
        # print(res)

        if score_max < res:
            score_max = res

print(score_max)

Fixed submission:
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

res = -10**10

for i in range(N):
    j = i
    total = 0
    loop_values = []

    while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
    loop_length = len(loop_values)
    point_sum = 0

    for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand

print(res)