#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    #l,r,d=map(int,input().split())
    n,k = map(int, input().split())
    p=list(map(int,input().split()))
    c = list(map(int, input().split()))
    loop=min(n,k)
    score=-INFI
    arr=set()
    loopscore=[]
    tempscore=0
    for i in range(n):
        temp=i
        arr = set()
        loopscore = []
        temp2=0
        iscore=0
        tempscore=0
        for j in range(loop):
            if p[temp]-1 in arr:
                break
            tempscore+=c[p[temp]-1]
            arr.add(p[temp]-1)
            temp=p[temp]-1
            loopscore.append(tempscore)

        if len(loopscore)<k:
            if loopscore[-1]>0:
                temp2=k//len(loopscore)-1
                if temp2<=0:
                    temp2=0
            iscore=temp2*loopscore[-1]
            nokori = k - (temp2 * len(loopscore))
            last=loopscore[-1]
            temploop=list(map(lambda x: x+last,loopscore))
            loopscore.extend(temploop)

            nokoriscore=max(loopscore[:nokori])
            iscore+=nokoriscore
        else:
            iscore=max(loopscore[:k])
        score=max(score,iscore)

    print(score)




if __name__ == "__main__":

    main()


Measured memory_bytes: 172316