import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

from itertools import accumulate

from collections import deque, defaultdict

def main():

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

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

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



    scores_all = deque()

    for k1 in range(n):

        scores = deque()

        kita = defaultdict(int)

        kita[k1] = ko

        cnt = ko - 1

        koko = p[k1] - 1

        scores.append(c[koko])

        kita[koko] = cnt

        while cnt:

            cnt -= 1

            koko = p[koko] - 1

            scores.append(c[koko])

            if kita[koko]:

                break

            else:

                kita[koko] = cnt

        cycle_long = kita[koko] - cnt

        scores_a = tuple(accumulate(scores))



        if cnt:

            if scores_a[-1] > 0:

                kaisuu = ko // cycle_long

                s1 = scores_a[-1] * kaisuu

                amari = ko % cycle_long

                if amari:

                    t = max(0, max(scores_a[:amari]))

                    s1 += t

                s2 = scores_a[-1] * (kaisuu - 1)

                s2 += max(scores_a)

                scores_all.append(max(s1, s2))

            else:

                scores_all.append(max(scores_a))

        else:

            scores_all.append(max(scores_a))

    print((max(scores_all)))



if __name__ == '__main__':

    main()
import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

from itertools import accumulate

def main():

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

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

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



    if k == 1:

        print((max(c)))

        sys.exit()

    seen = [0] * n

    cycles = []

    for i1 in range(n):

        if not seen[i1]:

            cycle_t = []

            koko = i1

            while True:

                if seen[koko]:

                    cycles.append(cycle_t)

                    break

                cycle_t.append(c[koko])

                seen[koko] = 1

                koko = p[koko] - 1



    score = max(c)

    for cycle in cycles:

        score_t = max(cycle)

        cycle_len = len(cycle)

        cycle_sum = sum(cycle)

        kaisu, rem = divmod(k, cycle_len)

        if rem == 0:

            kaisu -= 1

            rem = cycle_len

        if kaisu > 0:

            kaisu -= 1

            rem += cycle_len

        cycle = tuple(accumulate(cycle + cycle + cycle))

        for i1 in range(2, rem + 1):

            for i2 in range(cycle_len):

                score_t = max(score_t, cycle[i1+i2] - cycle[i2])

        if cycle_sum > 0:

            score_t += (kaisu * cycle_sum)

        score = max(score, score_t)

    print(score)



if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
from collections import deque, defaultdict


def main():
    n, ko = list(map(int, input().split()))
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    scores_all = deque()
    for k1 in range(n):
        scores = deque()
        kita = defaultdict(int)
        kita[k1] = ko
        cnt = ko - 1
        koko = p[k1] - 1
        scores.append(c[koko])
        kita[koko] = cnt
        while cnt:
            cnt -= 1
            koko = p[koko] - 1
            scores.append(c[koko])
            if kita[koko]:
                break
            else:
                kita[koko] = cnt
        cycle_long = kita[koko] - cnt
        scores_a = tuple(accumulate(scores))
        if cnt:
            if scores_a[-1] > 0:
                kaisuu = ko // cycle_long
                s1 = scores_a[-1] * kaisuu
                amari = ko % cycle_long
                if amari:
                    t = max(0, max(scores_a[:amari]))
                    s1 += t
                s2 = scores_a[-1] * (kaisuu - 1)
                s2 += max(scores_a)
                scores_all.append(max(s1, s2))
            else:
                scores_all.append(max(scores_a))
        else:
            scores_all.append(max(scores_a))
    print((max(scores_all)))


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate


def main():
    n, k = list(map(int, input().split()))
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    if k == 1:
        print((max(c)))
        sys.exit()
    seen = [0] * n
    cycles = []
    for i1 in range(n):
        if not seen[i1]:
            cycle_t = []
            koko = i1
            while True:
                if seen[koko]:
                    cycles.append(cycle_t)
                    break
                cycle_t.append(c[koko])
                seen[koko] = 1
                koko = p[koko] - 1
    score = max(c)
    for cycle in cycles:
        score_t = max(cycle)
        cycle_len = len(cycle)
        cycle_sum = sum(cycle)
        kaisu, rem = divmod(k, cycle_len)
        if rem == 0:
            kaisu -= 1
            rem = cycle_len
        if kaisu > 0:
            kaisu -= 1
            rem += cycle_len
        cycle = tuple(accumulate(cycle + cycle + cycle))
        for i1 in range(2, rem + 1):
            for i2 in range(cycle_len):
                score_t = max(score_t, cycle[i1 + i2] - cycle[i2])
        if cycle_sum > 0:
            score_t += kaisu * cycle_sum
        score = max(score, score_t)
    print(score)


if __name__ == "__main__":
    main()

-from collections import deque, defaultdict -    n, ko = list(map(int, input().split())) +    n, k = list(map(int, input().split())) -    scores_all = deque() -    for k1 in range(n): -        scores = deque() -        kita = defaultdict(int) -        kita[k1] = ko -        cnt = ko - 1 -        koko = p[k1] - 1 -        scores.append(c[koko]) -        kita[koko] = cnt -        while cnt: -            cnt -= 1 -            koko = p[koko] - 1 -            scores.append(c[koko]) -            if kita[koko]: -                break -            else: -                kita[koko] = cnt -        cycle_long = kita[koko] - cnt -        scores_a = tuple(accumulate(scores)) -        if cnt: -            if scores_a[-1] > 0: -                kaisuu = ko // cycle_long -                s1 = scores_a[-1] * kaisuu -                amari = ko % cycle_long -                if amari: -                    t = max(0, max(scores_a[:amari])) -                    s1 += t -                s2 = scores_a[-1] * (kaisuu - 1) -                s2 += max(scores_a) -                scores_all.append(max(s1, s2)) -            else: -                scores_all.append(max(scores_a)) -        else: -            scores_all.append(max(scores_a)) -    print((max(scores_all))) +    if k == 1: +        print((max(c))) +        sys.exit() +    seen = [0] * n +    cycles = [] +    for i1 in range(n): +        if not seen[i1]: +            cycle_t = [] +            koko = i1 +            while True: +                if seen[koko]: +                    cycles.append(cycle_t) +                    break +                cycle_t.append(c[koko]) +                seen[koko] = 1 +                koko = p[koko] - 1 +    score = max(c) +    for cycle in cycles: +        score_t = max(cycle) +        cycle_len = len(cycle) +        cycle_sum = sum(cycle) +        kaisu, rem = divmod(k, cycle_len) +        if rem == 0: +            kaisu -= 1 +            rem = cycle_len +        if kaisu > 0: +            kaisu -= 1 +            rem += cycle_len +        cycle = tuple(accumulate(cycle + cycle + cycle)) +        for i1 in range(2, rem + 1): +            for i2 in range(cycle_len): +                score_t = max(score_t, cycle[i1 + i2] - cycle[i2]) +        if cycle_sum > 0: +            score_t += kaisu * cycle_sum +        score = max(score, score_t) +    print(score)