Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
p = list(map(lambda x: int(x) - 1, input().split()))
c = list(map(int, input().split()))
    
visited = [False] * n
ans = max(c)

for i in range(n):
    if visited[i]:
        continue
    arr = []
    u = i
    while not visited[u]:
        arr.append(c[u])
        visited[u] = True
        u = p[u]
    s = sum(arr) * (k // len(arr))
    rem = k % len(arr)
    arr += arr
    for f in range(len(arr) // 2):
        ans = max(ans, s + sum(arr[f:f + rem]))
        for g in range(f + 1, len(arr) + 1):
            if g - f > k:
                break
            ans = max(ans, sum(arr[f:g]))
print(ans)
    


Fixed submission:
n, k = map(int, input().split())
p = list(map(lambda x: int(x) - 1, input().split()))
c = list(map(int, input().split()))
    
visited = [False] * n
ans = max(c)

for i in range(n):
    if visited[i]:
        continue
    arr = []
    u = i
    while not visited[u]:
        arr.append(c[u])
        visited[u] = True
        u = p[u]
    s = sum(arr) * (k // len(arr))
    rem = k % len(arr)
    if k // len(arr) >= 1:
        s -= sum(arr)
        rem += len(arr)
        
    arr += arr + arr
    pref_sum = [0]
    for i in arr:
        pref_sum.append(pref_sum[-1] + i)
    # print(pref_sum, s, rem) 
            
    for f in range(len(arr) // 2):
        # ans = max(ans, s + sum(arr[f:f + rem])) 
        for g in range(f + 1, len(pref_sum)):
            if g - f <= k:
                ans = max(ans, pref_sum[g] - pref_sum[f])
            if g - f <= rem:
                ans = max(ans, s + pref_sum[g] - pref_sum[f])
print(ans)

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

def solve(u):
    ans = c[p[u]]
    pos = [-1] * n
    score = [-1] * n
    tk = k
    cur_pos, cur_sum = 0, 0
    while tk > 0 and pos[p[u]] == -1:
        u = p[u]
        cur_pos += 1
        cur_sum += c[u]
        ans = max(ans, cur_sum)
        pos[u] = cur_pos
        score[u] = cur_sum
        tk -= 1
    cycle_length = cur_pos - pos[p[u]] + 1
    cycle_sum = cur_sum - score[p[u]] + c[p[u]]
    # print(cur_sum, ans, cycle_length, cycle_sum) 
    
    if cycle_sum <= 0:
        return ans
    
    cur_sum += cycle_sum * (tk // cycle_length)
    ans = max(ans, cur_sum)
    tk %= cycle_length
    while tk > 0:
        u = p[u]
        cur_sum += c[u]
        ans = max(ans, cur_sum)
        tk -= 1
    return ans

# print(list((solve(i), i) for i in range(n))) 
print(max(solve(i) for i in range(n)))
    
    


Fixed submission:
n, k = map(int, input().split())
p = list(map(lambda x: int(x) - 1, input().split()))
c = list(map(int, input().split()))
    
visited = [False] * n
ans = max(c)

for i in range(n):
    if visited[i]:
        continue
    arr = []
    u = i
    while not visited[u]:
        arr.append(c[u])
        visited[u] = True
        u = p[u]
    s = sum(arr) * (k // len(arr))
    rem = k % len(arr)
    if k // len(arr) >= 1:
        s -= sum(arr)
        rem += len(arr)
        
    arr += arr + arr
    pref_sum = [0]
    for i in arr:
        pref_sum.append(pref_sum[-1] + i)
    # print(pref_sum, s, rem) 
            
    for f in range(len(arr) // 2):
        # ans = max(ans, s + sum(arr[f:f + rem])) 
        for g in range(f + 1, len(pref_sum)):
            if g - f <= k:
                ans = max(ans, pref_sum[g] - pref_sum[f])
            if g - f <= rem:
                ans = max(ans, s + pref_sum[g] - pref_sum[f])
print(ans)