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

p = [int(i)-1 for i in input().split()]

c = [int(i) for i in input().split()]

INF = float("INF")

ans = -INF

for i in range(n):

    flag = [False]*n

    now = i

    cnt = 0

    j = 0

    mlst = [-INF]*n

    m = -INF

    while not flag[now]:

        flag[now] = True

        now = p[now]

        cnt += c[now]

        m = max(cnt,m)

        mlst[j] = m

        j += 1

    # print(mlst)

    q,r = divmod(k,j)

    if q == 0: res = mlst[r-1]

    elif cnt >= 0 and r == 0: res = cnt*(q-1)+mlst[j-1]

    elif cnt >= 0: res = max(cnt*q+mlst[r-1],cnt*(q-1)+mlst[j-1])

    else: res = mlst[j-1]

    # print("res",res)

    ans = max(ans,res)

print(ans)
n,k = list(map(int,input().split()))

p = [int(i)-1 for i in input().split()]

c = [int(i) for i in input().split()]

INF = float("INF")

ans = -INF



for si in range(n):

    x = si

    S = []

    tot = 0

    while True:

        x = p[x]

        S.append(c[x])

        tot += c[x]

        if x == si: break

    l = len(S)

    t = 0

    for i in range(l):

        if i+1 > k: break

        t += S[i]

        now = t

        if tot > 0:

            e = (k-(i+1))//l

            now += tot*e

        ans = max(ans, now)

print(ans)
n, k = list(map(int, input().split()))
p = [int(i) - 1 for i in input().split()]
c = [int(i) for i in input().split()]
INF = float("INF")
ans = -INF
for i in range(n):
    flag = [False] * n
    now = i
    cnt = 0
    j = 0
    mlst = [-INF] * n
    m = -INF
    while not flag[now]:
        flag[now] = True
        now = p[now]
        cnt += c[now]
        m = max(cnt, m)
        mlst[j] = m
        j += 1
    # print(mlst)
    q, r = divmod(k, j)
    if q == 0:
        res = mlst[r - 1]
    elif cnt >= 0 and r == 0:
        res = cnt * (q - 1) + mlst[j - 1]
    elif cnt >= 0:
        res = max(cnt * q + mlst[r - 1], cnt * (q - 1) + mlst[j - 1])
    else:
        res = mlst[j - 1]
    # print("res",res)
    ans = max(ans, res)
print(ans)

n, k = list(map(int, input().split()))
p = [int(i) - 1 for i in input().split()]
c = [int(i) for i in input().split()]
INF = float("INF")
ans = -INF
for si in range(n):
    x = si
    S = []
    tot = 0
    while True:
        x = p[x]
        S.append(c[x])
        tot += c[x]
        if x == si:
            break
    l = len(S)
    t = 0
    for i in range(l):
        if i + 1 > k:
            break
        t += S[i]
        now = t
        if tot > 0:
            e = (k - (i + 1)) // l
            now += tot * e
        ans = max(ans, now)
print(ans)

-for i in range(n): -    flag = [False] * n -    now = i -    cnt = 0 -    j = 0 -    mlst = [-INF] * n -    m = -INF -    while not flag[now]: -        flag[now] = True -        now = p[now] -        cnt += c[now] -        m = max(cnt, m) -        mlst[j] = m -        j += 1 -    # print(mlst) -    q, r = divmod(k, j) -    if q == 0: -        res = mlst[r - 1] -    elif cnt >= 0 and r == 0: -        res = cnt * (q - 1) + mlst[j - 1] -    elif cnt >= 0: -        res = max(cnt * q + mlst[r - 1], cnt * (q - 1) + mlst[j - 1]) -    else: -        res = mlst[j - 1] -    # print("res",res) -    ans = max(ans, res) +for si in range(n): +    x = si +    S = [] +    tot = 0 +    while True: +        x = p[x] +        S.append(c[x]) +        tot += c[x] +        if x == si: +            break +    l = len(S) +    t = 0 +    for i in range(l): +        if i + 1 > k: +            break +        t += S[i] +        now = t +        if tot > 0: +            e = (k - (i + 1)) // l +            now += tot * e +        ans = max(ans, now)