# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
N, K = map(int, input().split())
Ps = list(map(int, input().split()))
Cs = list(map(int, input().split()))
max_list = list()
for i in range(N):
    next = Ps[i] - 1
    scores = [Cs[next]]
    while next != i:
        next = Ps[next] - 1
        scores.append(scores[-1] + Cs[next])
    l = len(scores)
    if K <= l:
        max_list.append(max(scores[:K]))
    else:
        if scores[-1] < 0:
            max_list.append(max(scores))
        else:
            a, b = divmod(K, l)
            cand1 = 0
            if b!=0:
                cand1 = a*scores[-1] + max(scores[:b])
            cand2 = (a-1)*scores[-1] + max(scores)
            max_list.append(max(cand1, cand2))
    #print(scores)
print(max(max_list)) import sys sys sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K Ps = list(map(int, input().split())) Ps list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split Cs = list(map(int, input().split())) Cs list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split max_list = list() max_list list() list for i in range(N):
    next = Ps[i] - 1
    scores = [Cs[next]]
    while next != i:
        next = Ps[next] - 1
        scores.append(scores[-1] + Cs[next])
    l = len(scores)
    if K <= l:
        max_list.append(max(scores[:K]))
    else:
        if scores[-1] < 0:
            max_list.append(max(scores))
        else:
            a, b = divmod(K, l)
            cand1 = 0
            if b!=0:
                cand1 = a*scores[-1] + max(scores[:b])
            cand2 = (a-1)*scores[-1] + max(scores)
            max_list.append(max(cand1, cand2))
    #print(scores) i range(N) range N next = Ps[i] - 1 next Ps[i] - 1 Ps[i] Ps i 1 scores = [Cs[next]] scores [Cs[next]] Cs[next] Cs next while next != i:
        next = Ps[next] - 1
        scores.append(scores[-1] + Cs[next])
     next != i next i next = Ps[next] - 1 next Ps[next] - 1 Ps[next] Ps next 1 scores.append(scores[-1] + Cs[next]) scores.append scores append scores[-1] + Cs[next] scores[-1] scores -1 Cs[next] Cs next l = len(scores) l len(scores) len scores if K <= l:
        max_list.append(max(scores[:K]))
    else:
        if scores[-1] < 0:
            max_list.append(max(scores))
        else:
            a, b = divmod(K, l)
            cand1 = 0
            if b!=0:
                cand1 = a*scores[-1] + max(scores[:b])
            cand2 = (a-1)*scores[-1] + max(scores)
            max_list.append(max(cand1, cand2))
    #print(scores) K <= l K l max_list.append(max(scores[:K])) max_list.append max_list append max(scores[:K]) max scores[:K] scores :K K if scores[-1] < 0:
            max_list.append(max(scores))
        else:
            a, b = divmod(K, l)
            cand1 = 0
            if b!=0:
                cand1 = a*scores[-1] + max(scores[:b])
            cand2 = (a-1)*scores[-1] + max(scores)
            max_list.append(max(cand1, cand2))
    #print(scores) scores[-1] < 0 scores[-1] scores -1 0 max_list.append(max(scores)) max_list.append max_list append max(scores) max scores a, b = divmod(K, l) a divmod(K, l) divmod K l b cand1 = 0 cand1 0 if b!=0:
                cand1 = a*scores[-1] + max(scores[:b])
             b!=0 b 0 cand1 = a*scores[-1] + max(scores[:b]) cand1 a*scores[-1] + max(scores[:b]) a*scores[-1] a scores[-1] scores -1 max(scores[:b]) max scores[:b] scores :b b cand2 = (a-1)*scores[-1] + max(scores) cand2 (a-1)*scores[-1] + max(scores) (a-1)*scores[-1] a-1 a 1 scores[-1] scores -1 max(scores) max scores max_list.append(max(cand1, cand2)) max_list.append max_list append max(cand1, cand2) max cand1 cand2 print(max(max_list)) print max(max_list) max max_list cand2 = (a-1)*scores[-1] + max(scores) (a-1)*scores[-1] + max(scores) cand2 Ps = list(map(int, input().split())) list(map(int, input().split())) Ps cand1 = a*scores[-1] + max(scores[:b]) a*scores[-1] + max(scores[:b]) cand1 K = map(int, input().split()) map(int, input().split()) K l = len(scores) len(scores) l cand1 = 0 0 cand1 next = Ps[next] - 1 Ps[next] - 1 next a, b = divmod(K, l) divmod(K, l) a N, K = map(int, input().split()) map(int, input().split()) N max_list = list() list() max_list Cs = list(map(int, input().split())) list(map(int, input().split())) Cs scores = [Cs[next]] [Cs[next]] scores next = Ps[i] - 1 Ps[i] - 1 next b = divmod(K, l) divmod(K, l) b
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
N, K = map(int, input().split())
Ps = list(map(int, input().split()))
Cs = list(map(int, input().split()))
max_list = list()
for i in range(N):
    next = Ps[i] - 1
    scores = [Cs[next]]
    while next != i:
        next = Ps[next] - 1
        scores.append(scores[-1] + Cs[next])
    l = len(scores)
    if K <= l:
        max_list.append(max(scores[:K]))
    else:
        if scores[-1] < 0:
            max_list.append(max(scores))
        else:
            a, b = divmod(K, l)
            cand1 = 0
            if b!=0:
                cand1 = a*scores[-1] + max(scores[:b])
            cand2 = (a-1)*scores[-1] + max(scores)
            max_list.append(max(cand1, cand2))
    #print(scores)
print(max(max_list))
