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

lastans = -1e20

for i in range(n):
    now = p[i] - 1 
    score = [c[now]]
    while True:
        now = p[now] - 1
        score.append(score[-1] + c[now])
        if now == i:
            break
    loopsum = score[-1]
    looplen = len(score)
#    print(score)
    if looplen >= k:
        ans = max(score[:k])
    elif loopsum <= 0:
        ans = max(score)
    else:
        loopnum = k//looplen
        loopnum_mod = k % looplen
        ans1 = loopsum * (loopnum - 1) + max(score)
        ans2 = loopsum * loopnum
        if loopnum_mod != 0:
            ans2 = ans2 +max(0,max(score[:loopnum_mod]))
        ans =max(ans1,ans2)
    lastans = max(lastans,ans)

print(lastans)
 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 lastans = -1e20 lastans -1e20 for i in range(n):
    now = p[i] - 1 
    score = [c[now]]
    while True:
        now = p[now] - 1
        score.append(score[-1] + c[now])
        if now == i:
            break
    loopsum = score[-1]
    looplen = len(score)
#    print(score)
    if looplen >= k:
        ans = max(score[:k])
    elif loopsum <= 0:
        ans = max(score)
    else:
        loopnum = k//looplen
        loopnum_mod = k % looplen
        ans1 = loopsum * (loopnum - 1) + max(score)
        ans2 = loopsum * loopnum
        if loopnum_mod != 0:
            ans2 = ans2 +max(0,max(score[:loopnum_mod]))
        ans =max(ans1,ans2)
    lastans = max(lastans,ans) i range(n) range n now = p[i] - 1 now p[i] - 1 p[i] p i 1 score = [c[now]] score [c[now]] c[now] c now while True:
        now = p[now] - 1
        score.append(score[-1] + c[now])
        if now == i:
            break
     True now = p[now] - 1 now p[now] - 1 p[now] p now 1 score.append(score[-1] + c[now]) score.append score append score[-1] + c[now] score[-1] score -1 c[now] c now if now == i:
            break
     now == i now i break loopsum = score[-1] loopsum score[-1] score -1 looplen = len(score) looplen len(score) len score if looplen >= k:
        ans = max(score[:k])
    elif loopsum <= 0:
        ans = max(score)
    else:
        loopnum = k//looplen
        loopnum_mod = k % looplen
        ans1 = loopsum * (loopnum - 1) + max(score)
        ans2 = loopsum * loopnum
        if loopnum_mod != 0:
            ans2 = ans2 +max(0,max(score[:loopnum_mod]))
        ans =max(ans1,ans2)
     looplen >= k looplen k ans = max(score[:k]) ans max(score[:k]) max score[:k] score :k k elif loopsum <= 0:
        ans = max(score)
     loopsum <= 0 loopsum 0 ans = max(score) ans max(score) max score loopnum = k//looplen loopnum k//looplen k looplen loopnum_mod = k % looplen loopnum_mod k % looplen k looplen ans1 = loopsum * (loopnum - 1) + max(score) ans1 loopsum * (loopnum - 1) + max(score) loopsum * (loopnum - 1) loopsum loopnum - 1 loopnum 1 max(score) max score ans2 = loopsum * loopnum ans2 loopsum * loopnum loopsum loopnum if loopnum_mod != 0:
            ans2 = ans2 +max(0,max(score[:loopnum_mod]))
         loopnum_mod != 0 loopnum_mod 0 ans2 = ans2 +max(0,max(score[:loopnum_mod])) ans2 ans2 +max(0,max(score[:loopnum_mod])) ans2 max(0,max(score[:loopnum_mod])) max 0 max(score[:loopnum_mod]) max score[:loopnum_mod] score :loopnum_mod loopnum_mod ans =max(ans1,ans2) ans max(ans1,ans2) max ans1 ans2 lastans = max(lastans,ans) lastans max(lastans,ans) max lastans ans print(lastans) print lastans n, k = map(int, input().split()) map(int, input().split()) n lastans = -1e20 -1e20 lastans loopnum = k//looplen k//looplen loopnum score = [c[now]] [c[now]] score c = list(map(int, input().split())) list(map(int, input().split())) c k = map(int, input().split()) map(int, input().split()) k loopnum_mod = k % looplen k % looplen loopnum_mod now = p[now] - 1 p[now] - 1 now ans2 = ans2 +max(0,max(score[:loopnum_mod])) ans2 +max(0,max(score[:loopnum_mod])) ans2 now = p[i] - 1 p[i] - 1 now looplen = len(score) len(score) looplen ans =max(ans1,ans2) max(ans1,ans2) ans ans = max(score) max(score) ans ans1 = loopsum * (loopnum - 1) + max(score) loopsum * (loopnum - 1) + max(score) ans1 ans2 = loopsum * loopnum loopsum * loopnum ans2 lastans = max(lastans,ans) max(lastans,ans) lastans p = list(map(int, input().split())) list(map(int, input().split())) p loopsum = score[-1] score[-1] loopsum ans = max(score[:k]) max(score[:k]) ans
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

lastans = -1e20

for i in range(n):
    now = p[i] - 1 
    score = [c[now]]
    while True:
        now = p[now] - 1
        score.append(score[-1] + c[now])
        if now == i:
            break
    loopsum = score[-1]
    looplen = len(score)
#    print(score)
    if looplen >= k:
        ans = max(score[:k])
    elif loopsum <= 0:
        ans = max(score)
    else:
        loopnum = k//looplen
        loopnum_mod = k % looplen
        ans1 = loopsum * (loopnum - 1) + max(score)
        ans2 = loopsum * loopnum
        if loopnum_mod != 0:
            ans2 = ans2 +max(0,max(score[:loopnum_mod]))
        ans =max(ans1,ans2)
    lastans = max(lastans,ans)

print(lastans)

