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

p = list(map(int, input().split()))

c = list(map(int, input().split()))



ans = -10**18



for i in range(n):

    tmp_score = 0

    pos = i

    # cycle = 0

    for l in range(k):

        pos = p[pos]-1

        tmp_score += c[pos]

        ans = max(ans,tmp_score)

        # cycle += 1

        if pos == i: break

    q,r = divmod(k,l+1)

    if r == 0 and q > 1:

        r = l+1

        q -= 1

    tmp_score *= q

    ans = max(ans,tmp_score)

    for _ in range(r):

        pos = p[pos]-1

        tmp_score += c[pos]

        ans = max(ans,tmp_score)        

print(ans)

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

p = list(map(int, input().split()))

c = list(map(int, input().split()))



ans = -10**18



for i in range(n):

    tmp_score = 0

    pos = i

    cycle = 0

    for l in range(k):

        pos = p[pos]-1

        tmp_score += c[pos]

        ans = max(ans,tmp_score)

        cycle += 1

        if pos == i: break

    q,r = divmod(k,cycle)

    if r == 0 and q > 1:

        r = cycle

        q -= 1

    tmp_score *= q

    ans = max(ans,tmp_score)

    for _ in range(r):

        pos = p[pos]-1

        tmp_score += c[pos]

        ans = max(ans,tmp_score)        

print(ans)

n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = -(10**18)
for i in range(n):
    tmp_score = 0
    pos = i
    # cycle = 0
    for l in range(k):
        pos = p[pos] - 1
        tmp_score += c[pos]
        ans = max(ans, tmp_score)
        # cycle += 1
        if pos == i:
            break
    q, r = divmod(k, l + 1)
    if r == 0 and q > 1:
        r = l + 1
        q -= 1
    tmp_score *= q
    ans = max(ans, tmp_score)
    for _ in range(r):
        pos = p[pos] - 1
        tmp_score += c[pos]
        ans = max(ans, tmp_score)
print(ans)

n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = -(10**18)
for i in range(n):
    tmp_score = 0
    pos = i
    cycle = 0
    for l in range(k):
        pos = p[pos] - 1
        tmp_score += c[pos]
        ans = max(ans, tmp_score)
        cycle += 1
        if pos == i:
            break
    q, r = divmod(k, cycle)
    if r == 0 and q > 1:
        r = cycle
        q -= 1
    tmp_score *= q
    ans = max(ans, tmp_score)
    for _ in range(r):
        pos = p[pos] - 1
        tmp_score += c[pos]
        ans = max(ans, tmp_score)
print(ans)

-    # cycle = 0 +    cycle = 0 -        # cycle += 1 +        cycle += 1 -    q, r = divmod(k, l + 1) +    q, r = divmod(k, cycle) -        r = l + 1 +        r = cycle