import copy
import math
import time
import statistics
import math
import itertools
import bisect
from decimal import *


# a = get_int()
def get_int():
    return int(input())


# a = get_string()
def get_string():
    return input()


# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]


# a_list = get_string_list():
def get_string_list():
    return input().split()


# a, b = get_int_multi()
def get_int_multi():
    return map(int, input().split())


# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))


# print("{} {}".format(a, b))
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''


def main():
    start = time.time()

    n, k = get_int_multi()
    p = get_int_list()
    c = get_int_list()

    kaisuu = [-1] * n
    score = [0] * n
    score_max = [0] * n

    for i in range(n):
        wk = 0
        wk2 = 0
        ichi = i
        for ii in range(n):
            ichi = p[ichi] - 1
            wk += c[ichi]
            wk2 = max(wk2, wk)
            if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

    wk = 0
    ans = - 10 ** 20
    for i in range(n):
        ichi = p[i] - 1
        wk = c[ichi]
        wk2 = c[ichi]
        idx = 0
        while idx < k - 1:
            ichi = p[ichi] - 1
            wk += c[ichi]

            wk2 = max(wk2, wk)
            idx += 1

            # ループするならメモを使って減らす
            if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

        ans = max(ans, wk2)
    print(ans)


if __name__ == '__main__':
    main()
 import copy copy import math math import time time import statistics statistics import math math import itertools itertools import bisect bisect from decimal import * def get_int():
    return int(input())


# a = get_string() get_int return int(input()) int(input()) int input() input def get_string():
    return input()


# a_list = get_int_list() get_string return input() input() input def get_int_list():
    return [int(x) for x in input().split()]


# a_list = get_string_list(): get_int_list int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x return [int(x) for x in input().split()] [int(x) for x in input().split()] def get_string_list():
    return input().split()


# a, b = get_int_multi() get_string_list return input().split() input().split() ().split () input split def get_int_multi():
    return map(int, input().split())


# a_list = get_string_char_list() get_int_multi return map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split def get_string_char_list():
    return list(str(input()))


# print("{} {}".format(a, b))
# a_list = [0] * a get_string_char_list return list(str(input())) list(str(input())) list str(input()) str input() input '''
while (idx < n) and ():

    idx += 1
''' def main():
    start = time.time()

    n, k = get_int_multi()
    p = get_int_list()
    c = get_int_list()

    kaisuu = [-1] * n
    score = [0] * n
    score_max = [0] * n

    for i in range(n):
        wk = 0
        wk2 = 0
        ichi = i
        for ii in range(n):
            ichi = p[ichi] - 1
            wk += c[ichi]
            wk2 = max(wk2, wk)
            if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

    wk = 0
    ans = - 10 ** 20
    for i in range(n):
        ichi = p[i] - 1
        wk = c[ichi]
        wk2 = c[ichi]
        idx = 0
        while idx < k - 1:
            ichi = p[ichi] - 1
            wk += c[ichi]

            wk2 = max(wk2, wk)
            idx += 1

            # ループするならメモを使って減らす
            if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

        ans = max(ans, wk2)
    print(ans) main start = time.time() start time.time() time.time time time n, k = get_int_multi() n get_int_multi() get_int_multi k p = get_int_list() p get_int_list() get_int_list c = get_int_list() c get_int_list() get_int_list kaisuu = [-1] * n kaisuu [-1] * n [-1] -1 n score = [0] * n score [0] * n [0] 0 n score_max = [0] * n score_max [0] * n [0] 0 n for i in range(n):
        wk = 0
        wk2 = 0
        ichi = i
        for ii in range(n):
            ichi = p[ichi] - 1
            wk += c[ichi]
            wk2 = max(wk2, wk)
            if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

     i range(n) range n wk = 0 wk 0 wk2 = 0 wk2 0 ichi = i ichi i for ii in range(n):
            ichi = p[ichi] - 1
            wk += c[ichi]
            wk2 = max(wk2, wk)
            if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

     ii range(n) range n ichi = p[ichi] - 1 ichi p[ichi] - 1 p[ichi] p ichi 1 wk += c[ichi] wk c[ichi] c ichi wk2 = max(wk2, wk) wk2 max(wk2, wk) max wk2 wk if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

     i == ichi i ichi kaisuu[i] = ii + 1 kaisuu[i] kaisuu i ii + 1 ii 1 score[i] = wk score[i] score i wk score_max[i] = wk2 score_max[i] score_max i wk2 break wk = 0 wk 0 ans = - 10 ** 20 ans - 10 ** 20 - 10 20 for i in range(n):
        ichi = p[i] - 1
        wk = c[ichi]
        wk2 = c[ichi]
        idx = 0
        while idx < k - 1:
            ichi = p[ichi] - 1
            wk += c[ichi]

            wk2 = max(wk2, wk)
            idx += 1

            # ループするならメモを使って減らす
            if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

        ans = max(ans, wk2)
     i range(n) range n ichi = p[i] - 1 ichi p[i] - 1 p[i] p i 1 wk = c[ichi] wk c[ichi] c ichi wk2 = c[ichi] wk2 c[ichi] c ichi idx = 0 idx 0 while idx < k - 1:
            ichi = p[ichi] - 1
            wk += c[ichi]

            wk2 = max(wk2, wk)
            idx += 1

            # ループするならメモを使って減らす
            if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

         idx < k - 1 idx k - 1 k 1 ichi = p[ichi] - 1 ichi p[ichi] - 1 p[ichi] p ichi 1 wk += c[ichi] wk c[ichi] c ichi wk2 = max(wk2, wk) wk2 max(wk2, wk) max wk2 wk idx += 1 idx 1 if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

         (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0 k - 1 - idx > kaisuu[ichi] * 2 k - 1 - idx k - 1 k 1 idx kaisuu[ichi] * 2 kaisuu[ichi] kaisuu ichi 2 kaisuu[ichi] > 0 kaisuu[ichi] kaisuu ichi 0 roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1 roopsuu ((k - 1 - idx) // kaisuu[ichi]) - 1 (k - 1 - idx) // kaisuu[ichi] k - 1 - idx k - 1 k 1 idx kaisuu[ichi] kaisuu ichi 1 wk += roopsuu * score[ichi] wk roopsuu * score[ichi] roopsuu score[ichi] score ichi idx += roopsuu * kaisuu[ichi] idx roopsuu * kaisuu[ichi] roopsuu kaisuu[ichi] kaisuu ichi wk2 = max(wk2, max(wk, score_max[ichi])) wk2 max(wk2, max(wk, score_max[ichi])) max wk2 max(wk, score_max[ichi]) max wk score_max[ichi] score_max ichi ans = max(ans, wk2) ans max(ans, wk2) max ans wk2 print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def get_int():
    return int(input())


# a = get_string() def get_int():
    return int(input())


# a = get_string() get_int def get_int_multi():
    return map(int, input().split())


# a_list = get_string_char_list() def get_int_multi():
    return map(int, input().split())


# a_list = get_string_char_list() get_int_multi def get_string_list():
    return input().split()


# a, b = get_int_multi() def get_string_list():
    return input().split()


# a, b = get_int_multi() get_string_list def main():
    start = time.time()

    n, k = get_int_multi()
    p = get_int_list()
    c = get_int_list()

    kaisuu = [-1] * n
    score = [0] * n
    score_max = [0] * n

    for i in range(n):
        wk = 0
        wk2 = 0
        ichi = i
        for ii in range(n):
            ichi = p[ichi] - 1
            wk += c[ichi]
            wk2 = max(wk2, wk)
            if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

    wk = 0
    ans = - 10 ** 20
    for i in range(n):
        ichi = p[i] - 1
        wk = c[ichi]
        wk2 = c[ichi]
        idx = 0
        while idx < k - 1:
            ichi = p[ichi] - 1
            wk += c[ichi]

            wk2 = max(wk2, wk)
            idx += 1

            # ループするならメモを使って減らす
            if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

        ans = max(ans, wk2)
    print(ans) def main():
    start = time.time()

    n, k = get_int_multi()
    p = get_int_list()
    c = get_int_list()

    kaisuu = [-1] * n
    score = [0] * n
    score_max = [0] * n

    for i in range(n):
        wk = 0
        wk2 = 0
        ichi = i
        for ii in range(n):
            ichi = p[ichi] - 1
            wk += c[ichi]
            wk2 = max(wk2, wk)
            if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

    wk = 0
    ans = - 10 ** 20
    for i in range(n):
        ichi = p[i] - 1
        wk = c[ichi]
        wk2 = c[ichi]
        idx = 0
        while idx < k - 1:
            ichi = p[ichi] - 1
            wk += c[ichi]

            wk2 = max(wk2, wk)
            idx += 1

            # ループするならメモを使って減らす
            if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

        ans = max(ans, wk2)
    print(ans) main def get_int_list():
    return [int(x) for x in input().split()]


# a_list = get_string_list(): def get_int_list():
    return [int(x) for x in input().split()]


# a_list = get_string_list(): get_int_list def get_string():
    return input()


# a_list = get_int_list() def get_string():
    return input()


# a_list = get_int_list() get_string def get_string_char_list():
    return list(str(input()))


# print("{} {}".format(a, b))
# a_list = [0] * a def get_string_char_list():
    return list(str(input()))


# print("{} {}".format(a, b))
# a_list = [0] * a get_string_char_list
import copy
import math
import time
import statistics
import math
import itertools
import bisect
from decimal import *


# a = get_int()
def get_int():
    return int(input())


# a = get_string()
def get_string():
    return input()


# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]


# a_list = get_string_list():
def get_string_list():
    return input().split()


# a, b = get_int_multi()
def get_int_multi():
    return map(int, input().split())


# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))


# print("{} {}".format(a, b))
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''


def main():
    start = time.time()

    n, k = get_int_multi()
    p = get_int_list()
    c = get_int_list()

    kaisuu = [-1] * n
    score = [0] * n
    score_max = [0] * n

    for i in range(n):
        wk = 0
        wk2 = 0
        ichi = i
        for ii in range(n):
            ichi = p[ichi] - 1
            wk += c[ichi]
            wk2 = max(wk2, wk)
            if i == ichi:
                kaisuu[i] = ii + 1
                score[i] = wk
                score_max[i] = wk2
                break

    wk = 0
    ans = - 10 ** 20
    for i in range(n):
        ichi = p[i] - 1
        wk = c[ichi]
        wk2 = c[ichi]
        idx = 0
        while idx < k - 1:
            ichi = p[ichi] - 1
            wk += c[ichi]

            wk2 = max(wk2, wk)
            idx += 1

            # ループするならメモを使って減らす
            if (k - 1 - idx > kaisuu[ichi] * 2) and kaisuu[ichi] > 0:
                roopsuu = ((k - 1 - idx) // kaisuu[ichi]) - 1
                wk += roopsuu * score[ichi]
                idx += roopsuu * kaisuu[ichi]
                wk2 = max(wk2, max(wk, score_max[ichi]))

        ans = max(ans, wk2)
    print(ans)


if __name__ == '__main__':
    main()

