from sys import maxsize
from itertools import product

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

used = [0] * n
ss = []
for i in range(n):
    if used[i]:
        continue
    now = i
    s = []
    while not used[now]:
        used[now] = 1
        s.append(c[now])
        now = p[now]
    ss.append(s)

res = -maxsize
for s in ss:
    s_len = len(s)
    cumsum = [0]
    # 2周分の累積和
    for i in range(2*s_len):
        cumsum.append(cumsum[-1] + s[i%s_len])

    max_sum = [-maxsize] * s_len
    for i, j in product(range(s_len), repeat=2):
        max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

    for i in range(s_len):
        if i > k:
            continue
        v = (k - i) // s_len
        if i == 0 and v == 0:
            continue
        if cumsum[s_len] > 0:
            res = max(res, max_sum[i] + cumsum[s_len] * v)
        elif i > 0:
            res = max(res, max_sum[i])
print(res) from sys import maxsize from itertools import product n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = list(map(lambda x: int(x)-1, input().split())) p list(map(lambda x: int(x)-1, input().split())) list map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x 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 used = [0] * n used [0] * n [0] 0 n ss = [] ss [] for i in range(n):
    if used[i]:
        continue
    now = i
    s = []
    while not used[now]:
        used[now] = 1
        s.append(c[now])
        now = p[now]
    ss.append(s) i range(n) range n if used[i]:
        continue
     used[i] used i continue now = i now i s = [] s [] while not used[now]:
        used[now] = 1
        s.append(c[now])
        now = p[now]
     not used[now] used[now] used now used[now] = 1 used[now] used now 1 s.append(c[now]) s.append s append c[now] c now now = p[now] now p[now] p now ss.append(s) ss.append ss append s res = -maxsize res -maxsize maxsize for s in ss:
    s_len = len(s)
    cumsum = [0]
    # 2周分の累積和
    for i in range(2*s_len):
        cumsum.append(cumsum[-1] + s[i%s_len])

    max_sum = [-maxsize] * s_len
    for i, j in product(range(s_len), repeat=2):
        max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

    for i in range(s_len):
        if i > k:
            continue
        v = (k - i) // s_len
        if i == 0 and v == 0:
            continue
        if cumsum[s_len] > 0:
            res = max(res, max_sum[i] + cumsum[s_len] * v)
        elif i > 0:
            res = max(res, max_sum[i]) s ss s_len = len(s) s_len len(s) len s cumsum = [0] cumsum [0] 0 for i in range(2*s_len):
        cumsum.append(cumsum[-1] + s[i%s_len])

     i range(2*s_len) range 2*s_len 2 s_len cumsum.append(cumsum[-1] + s[i%s_len]) cumsum.append cumsum append cumsum[-1] + s[i%s_len] cumsum[-1] cumsum -1 s[i%s_len] s i%s_len i s_len max_sum = [-maxsize] * s_len max_sum [-maxsize] * s_len [-maxsize] -maxsize maxsize s_len for i, j in product(range(s_len), repeat=2):
        max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

     i j product(range(s_len), repeat=2) product range(s_len) range s_len repeat=2 repeat 2 max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i]) max_sum[j] max_sum j max(max_sum[j], cumsum[i+j] - cumsum[i]) max max_sum[j] max_sum j cumsum[i+j] - cumsum[i] cumsum[i+j] cumsum i+j i j cumsum[i] cumsum i for i in range(s_len):
        if i > k:
            continue
        v = (k - i) // s_len
        if i == 0 and v == 0:
            continue
        if cumsum[s_len] > 0:
            res = max(res, max_sum[i] + cumsum[s_len] * v)
        elif i > 0:
            res = max(res, max_sum[i]) i range(s_len) range s_len if i > k:
            continue
         i > k i k continue v = (k - i) // s_len v (k - i) // s_len k - i k i s_len if i == 0 and v == 0:
            continue
         i == 0 and v == 0 i == 0 i 0 v == 0 v 0 continue if cumsum[s_len] > 0:
            res = max(res, max_sum[i] + cumsum[s_len] * v)
        elif i > 0:
            res = max(res, max_sum[i]) cumsum[s_len] > 0 cumsum[s_len] cumsum s_len 0 res = max(res, max_sum[i] + cumsum[s_len] * v) res max(res, max_sum[i] + cumsum[s_len] * v) max res max_sum[i] + cumsum[s_len] * v max_sum[i] max_sum i cumsum[s_len] * v cumsum[s_len] cumsum s_len v elif i > 0:
            res = max(res, max_sum[i]) i > 0 i 0 res = max(res, max_sum[i]) res max(res, max_sum[i]) max res max_sum[i] max_sum i print(res) print res ss = [] [] ss res = -maxsize -maxsize res p = list(map(lambda x: int(x)-1, input().split())) list(map(lambda x: int(x)-1, input().split())) p res = max(res, max_sum[i]) max(res, max_sum[i]) res s_len = len(s) len(s) s_len now = i i now s = [] [] s max_sum = [-maxsize] * s_len [-maxsize] * s_len max_sum n, k = map(int, input().split()) map(int, input().split()) n now = p[now] p[now] now cumsum = [0] [0] cumsum c = list(map(int, input().split())) list(map(int, input().split())) c k = map(int, input().split()) map(int, input().split()) k used = [0] * n [0] * n used res = max(res, max_sum[i] + cumsum[s_len] * v) max(res, max_sum[i] + cumsum[s_len] * v) res v = (k - i) // s_len (k - i) // s_len v
from sys import maxsize
from itertools import product

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

used = [0] * n
ss = []
for i in range(n):
    if used[i]:
        continue
    now = i
    s = []
    while not used[now]:
        used[now] = 1
        s.append(c[now])
        now = p[now]
    ss.append(s)

res = -maxsize
for s in ss:
    s_len = len(s)
    cumsum = [0]
    # 2周分の累積和
    for i in range(2*s_len):
        cumsum.append(cumsum[-1] + s[i%s_len])

    max_sum = [-maxsize] * s_len
    for i, j in product(range(s_len), repeat=2):
        max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

    for i in range(s_len):
        if i > k:
            continue
        v = (k - i) // s_len
        if i == 0 and v == 0:
            continue
        if cumsum[s_len] > 0:
            res = max(res, max_sum[i] + cumsum[s_len] * v)
        elif i > 0:
            res = max(res, max_sum[i])
print(res)
