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) n, k = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k p = list(map(int,input().split())) p list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split c = list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ans = -float('inf') ans -float('inf') float('inf') 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) i range(n) range n score = [] score [] check = [0] * n check [0] * n [0] 0 n now = i now i cycle = 0 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

     _ range(k) range k go = p[now] - 1 go p[now] - 1 p[now] p now 1 if check[go] == 1:
            break
         check[go] == 1 check[go] check go 1 break now = go now go if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
         len(score) == 0 len(score) len score 0 score.append(c[now]) score.append score append c[now] c now score.append(score[-1] + c[now]) score.append score append score[-1] + c[now] score[-1] score -1 c[now] c now check[now] = 1 check[now] check now 1 cycle += 1 cycle 1 t = k // cycle t k // cycle k cycle m = k % cycle m k % cycle k cycle sum_cycle = score[-1] sum_cycle score[-1] 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) m == 0 m 0 ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans) ans max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans) max sum_cycle * t sum_cycle t sum_cycle * (t - 1) + max(score) sum_cycle * (t - 1) sum_cycle t - 1 t 1 max(score) max score max(score) max score ans ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans) ans max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans) max sum_cycle * t + max(score[:m]) sum_cycle * t sum_cycle t max(score[:m]) max score[:m] score :m m sum_cycle * (t - 1) + max(score) sum_cycle * (t - 1) sum_cycle t - 1 t 1 max(score) max score max(score) max score ans print(ans) print ans cycle += 1 1 cycle go = p[now] - 1 p[now] - 1 go sum_cycle = score[-1] score[-1] sum_cycle ans = -float('inf') -float('inf') ans check = [0] * n [0] * n check c = list(map(int,input().split())) list(map(int,input().split())) c score = [] [] score p = list(map(int,input().split())) list(map(int,input().split())) p n, k = map(int,input().split()) map(int,input().split()) n cycle = 0 0 cycle ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans) max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans) ans k = map(int,input().split()) map(int,input().split()) k m = k % cycle k % cycle m ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans) max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans) ans now = go go now t = k // cycle k // cycle t now = i i now
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)