n, k = map(int, input().split())
next = list(map(int, input().split()))
next = list(map(lambda x: x - 1, next))
c = list(map(int, input().split()))

ans = -(10 ** 17)

for i in range(n):
    now = i
    cycle_sum = 0
    cycle_len = 0

    while True:
        cycle_sum += c[now]
        cycle_len += 1
        now = next[now]
        if now == i:
            break

    sum = 0
    cnt = 0

    while True:
        sum += c[now]
        cnt += 1

        if cnt > k:
            break

        laps = (k - cnt) // cycle_len
        score = sum + max(0, cycle_sum) * laps
        ans = max(ans, score)

        now = next[now]
        if now == i:
            break

print(ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k next = list(map(int, input().split())) next list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split next = list(map(lambda x: x - 1, next)) next list(map(lambda x: x - 1, next)) list map(lambda x: x - 1, next) map lambda x: x - 1 x - 1 x 1 x next 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 = -(10 ** 17) ans -(10 ** 17) 10 ** 17 10 17 for i in range(n):
    now = i
    cycle_sum = 0
    cycle_len = 0

    while True:
        cycle_sum += c[now]
        cycle_len += 1
        now = next[now]
        if now == i:
            break

    sum = 0
    cnt = 0

    while True:
        sum += c[now]
        cnt += 1

        if cnt > k:
            break

        laps = (k - cnt) // cycle_len
        score = sum + max(0, cycle_sum) * laps
        ans = max(ans, score)

        now = next[now]
        if now == i:
            break i range(n) range n now = i now i cycle_sum = 0 cycle_sum 0 cycle_len = 0 cycle_len 0 while True:
        cycle_sum += c[now]
        cycle_len += 1
        now = next[now]
        if now == i:
            break

     True cycle_sum += c[now] cycle_sum c[now] c now cycle_len += 1 cycle_len 1 now = next[now] now next[now] next now if now == i:
            break

     now == i now i break sum = 0 sum 0 cnt = 0 cnt 0 while True:
        sum += c[now]
        cnt += 1

        if cnt > k:
            break

        laps = (k - cnt) // cycle_len
        score = sum + max(0, cycle_sum) * laps
        ans = max(ans, score)

        now = next[now]
        if now == i:
            break True sum += c[now] sum c[now] c now cnt += 1 cnt 1 if cnt > k:
            break

         cnt > k cnt k break laps = (k - cnt) // cycle_len laps (k - cnt) // cycle_len k - cnt k cnt cycle_len score = sum + max(0, cycle_sum) * laps score sum + max(0, cycle_sum) * laps sum max(0, cycle_sum) * laps max(0, cycle_sum) max 0 cycle_sum laps ans = max(ans, score) ans max(ans, score) max ans score now = next[now] now next[now] next now if now == i:
            break now == i now i break print(ans) print ans score = sum + max(0, cycle_sum) * laps sum + max(0, cycle_sum) * laps score now = next[now] next[now] now cnt += 1 1 cnt cycle_len = 0 0 cycle_len laps = (k - cnt) // cycle_len (k - cnt) // cycle_len laps now = next[now] next[now] now cycle_sum = 0 0 cycle_sum next = list(map(int, input().split())) list(map(int, input().split())) next ans = -(10 ** 17) -(10 ** 17) ans cycle_len += 1 1 cycle_len k = map(int, input().split()) map(int, input().split()) k sum = 0 0 sum n, k = map(int, input().split()) map(int, input().split()) n sum += c[now] c[now] sum now = i i now next = list(map(lambda x: x - 1, next)) list(map(lambda x: x - 1, next)) next cycle_sum += c[now] c[now] cycle_sum c = list(map(int, input().split())) list(map(int, input().split())) c cnt = 0 0 cnt ans = max(ans, score) max(ans, score) ans
n, k = map(int, input().split())
next = list(map(int, input().split()))
next = list(map(lambda x: x - 1, next))
c = list(map(int, input().split()))

ans = -(10 ** 17)

for i in range(n):
    now = i
    cycle_sum = 0
    cycle_len = 0

    while True:
        cycle_sum += c[now]
        cycle_len += 1
        now = next[now]
        if now == i:
            break

    sum = 0
    cnt = 0

    while True:
        sum += c[now]
        cnt += 1

        if cnt > k:
            break

        laps = (k - cnt) // cycle_len
        score = sum + max(0, cycle_sum) * laps
        ans = max(ans, score)

        now = next[now]
        if now == i:
            break

print(ans)