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

c_list = []
rink_list = []
visited = set()
ans = -10 ** 10
for i in range(n):
    sum_c = c[i]
    j = p[i] - 1
    num = 1
    while i != j:
        sum_c += c[j]
        j = p[j] - 1
        num += 1

    sum_c = max(0, sum_c)
    # print(f'num:{num}')
    cnt = 1
    j = p[i] - 1
    tmp = c[i]
    ans = max(ans, tmp + sum_c * ((k - cnt) // num))
    while cnt < k and i != j:
        cnt += 1
        tmp += c[j]

        ans = max(ans, tmp + sum_c * ((k - cnt) // num))
        j = p[j] - 1

if ans == 0:
    ans = max(c)

print(ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = list(map(int, input().split())) p list(map(int, input().split())) list map(int, input().split()) map int 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 c_list = [] c_list [] rink_list = [] rink_list [] visited = set() visited set() set ans = -10 ** 10 ans -10 ** 10 -10 10 for i in range(n):
    sum_c = c[i]
    j = p[i] - 1
    num = 1
    while i != j:
        sum_c += c[j]
        j = p[j] - 1
        num += 1

    sum_c = max(0, sum_c)
    # print(f'num:{num}')
    cnt = 1
    j = p[i] - 1
    tmp = c[i]
    ans = max(ans, tmp + sum_c * ((k - cnt) // num))
    while cnt < k and i != j:
        cnt += 1
        tmp += c[j]

        ans = max(ans, tmp + sum_c * ((k - cnt) // num))
        j = p[j] - 1 i range(n) range n sum_c = c[i] sum_c c[i] c i j = p[i] - 1 j p[i] - 1 p[i] p i 1 num = 1 num 1 while i != j:
        sum_c += c[j]
        j = p[j] - 1
        num += 1

     i != j i j sum_c += c[j] sum_c c[j] c j j = p[j] - 1 j p[j] - 1 p[j] p j 1 num += 1 num 1 sum_c = max(0, sum_c) sum_c max(0, sum_c) max 0 sum_c cnt = 1 cnt 1 j = p[i] - 1 j p[i] - 1 p[i] p i 1 tmp = c[i] tmp c[i] c i ans = max(ans, tmp + sum_c * ((k - cnt) // num)) ans max(ans, tmp + sum_c * ((k - cnt) // num)) max ans tmp + sum_c * ((k - cnt) // num) tmp sum_c * ((k - cnt) // num) sum_c (k - cnt) // num k - cnt k cnt num while cnt < k and i != j:
        cnt += 1
        tmp += c[j]

        ans = max(ans, tmp + sum_c * ((k - cnt) // num))
        j = p[j] - 1 cnt < k and i != j cnt < k cnt k i != j i j cnt += 1 cnt 1 tmp += c[j] tmp c[j] c j ans = max(ans, tmp + sum_c * ((k - cnt) // num)) ans max(ans, tmp + sum_c * ((k - cnt) // num)) max ans tmp + sum_c * ((k - cnt) // num) tmp sum_c * ((k - cnt) // num) sum_c (k - cnt) // num k - cnt k cnt num j = p[j] - 1 j p[j] - 1 p[j] p j 1 if ans == 0:
    ans = max(c) ans == 0 ans 0 ans = max(c) ans max(c) max c print(ans) print ans num += 1 1 num ans = max(ans, tmp + sum_c * ((k - cnt) // num)) max(ans, tmp + sum_c * ((k - cnt) // num)) ans k = map(int, input().split()) map(int, input().split()) k p = list(map(int, input().split())) list(map(int, input().split())) p cnt += 1 1 cnt sum_c += c[j] c[j] sum_c ans = max(ans, tmp + sum_c * ((k - cnt) // num)) max(ans, tmp + sum_c * ((k - cnt) // num)) ans ans = max(c) max(c) ans j = p[j] - 1 p[j] - 1 j ans = -10 ** 10 -10 ** 10 ans c_list = [] [] c_list visited = set() set() visited cnt = 1 1 cnt j = p[i] - 1 p[i] - 1 j rink_list = [] [] rink_list sum_c = max(0, sum_c) max(0, sum_c) sum_c sum_c = c[i] c[i] sum_c c = list(map(int, input().split())) list(map(int, input().split())) c num = 1 1 num j = p[i] - 1 p[i] - 1 j n, k = map(int, input().split()) map(int, input().split()) n tmp += c[j] c[j] tmp j = p[j] - 1 p[j] - 1 j tmp = c[i] c[i] tmp
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

c_list = []
rink_list = []
visited = set()
ans = -10 ** 10
for i in range(n):
    sum_c = c[i]
    j = p[i] - 1
    num = 1
    while i != j:
        sum_c += c[j]
        j = p[j] - 1
        num += 1

    sum_c = max(0, sum_c)
    # print(f'num:{num}')
    cnt = 1
    j = p[i] - 1
    tmp = c[i]
    ans = max(ans, tmp + sum_c * ((k - cnt) // num))
    while cnt < k and i != j:
        cnt += 1
        tmp += c[j]

        ans = max(ans, tmp + sum_c * ((k - cnt) // num))
        j = p[j] - 1

if ans == 0:
    ans = max(c)

print(ans)