n,k=map(int,input().split())
p=[int(i)-1 for i in input().split()]
c=[int(i) for i in input().split()]
max_c=min(c)

def calc(x):
    global n,k
    next=x
    ma=max_c
    s=0
    l=[]
    for i in range(n):
        next=p[next]
        s+=c[next]
        l.append(c[next])
        ma=max(ma,s)
        if next==x:
            break
    if s<=0:
        return ma

    cycle=k//(i+1)
    rest=k%(i+1)
    ans=(cycle-1)*s+ma
    f=s*cycle
    for j in range(rest):
        f+=l[j]
        ans=max(ans,f)
    return ans

A=max_c
for start in range(n):
    A=max(A,calc(start))
print(A) n,k=map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 p=[int(i)-1 for i in input().split()] p [int(i)-1 for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i c=[int(i) for i in input().split()] c [int(i) for i in input().split()] max_c=min(c) max_c min(c) min c def calc(x):
    global n,k
    next=x
    ma=max_c
    s=0
    l=[]
    for i in range(n):
        next=p[next]
        s+=c[next]
        l.append(c[next])
        ma=max(ma,s)
        if next==x:
            break
    if s<=0:
        return ma

    cycle=k//(i+1)
    rest=k%(i+1)
    ans=(cycle-1)*s+ma
    f=s*cycle
    for j in range(rest):
        f+=l[j]
        ans=max(ans,f)
    return ans calc global n,k next=x next x ma=max_c ma max_c s=0 s 0 l=[] l [] for i in range(n):
        next=p[next]
        s+=c[next]
        l.append(c[next])
        ma=max(ma,s)
        if next==x:
            break
     i range(n) range n next=p[next] next p[next] p next s+=c[next] s c[next] c next l.append(c[next]) l.append l append c[next] c next ma=max(ma,s) ma max(ma,s) max ma s if next==x:
            break
     next==x next x break if s<=0:
        return ma

     s<=0 s 0 return ma ma cycle=k//(i+1) cycle k//(i+1) k i+1 i 1 rest=k%(i+1) rest k%(i+1) k i+1 i 1 ans=(cycle-1)*s+ma ans (cycle-1)*s+ma (cycle-1)*s cycle-1 cycle 1 s ma f=s*cycle f s*cycle s cycle for j in range(rest):
        f+=l[j]
        ans=max(ans,f)
     j range(rest) range rest f+=l[j] f l[j] l j ans=max(ans,f) ans max(ans,f) max ans f return ans ans x x A=max_c A max_c for start in range(n):
    A=max(A,calc(start)) start range(n) range n A=max(A,calc(start)) A max(A,calc(start)) max A calc(start) calc start print(A) print A p=[int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] p A=max_c max_c A c=[int(i) for i in input().split()] [int(i) for i in input().split()] c def calc(x):
    global n,k
    next=x
    ma=max_c
    s=0
    l=[]
    for i in range(n):
        next=p[next]
        s+=c[next]
        l.append(c[next])
        ma=max(ma,s)
        if next==x:
            break
    if s<=0:
        return ma

    cycle=k//(i+1)
    rest=k%(i+1)
    ans=(cycle-1)*s+ma
    f=s*cycle
    for j in range(rest):
        f+=l[j]
        ans=max(ans,f)
    return ans def calc(x):
    global n,k
    next=x
    ma=max_c
    s=0
    l=[]
    for i in range(n):
        next=p[next]
        s+=c[next]
        l.append(c[next])
        ma=max(ma,s)
        if next==x:
            break
    if s<=0:
        return ma

    cycle=k//(i+1)
    rest=k%(i+1)
    ans=(cycle-1)*s+ma
    f=s*cycle
    for j in range(rest):
        f+=l[j]
        ans=max(ans,f)
    return ans calc max_c=min(c) min(c) max_c n,k=map(int,input().split()) map(int,input().split()) n k=map(int,input().split()) map(int,input().split()) k A=max(A,calc(start)) max(A,calc(start)) A
n,k=map(int,input().split())
p=[int(i)-1 for i in input().split()]
c=[int(i) for i in input().split()]
max_c=min(c)

def calc(x):
    global n,k
    next=x
    ma=max_c
    s=0
    l=[]
    for i in range(n):
        next=p[next]
        s+=c[next]
        l.append(c[next])
        ma=max(ma,s)
        if next==x:
            break
    if s<=0:
        return ma

    cycle=k//(i+1)
    rest=k%(i+1)
    ans=(cycle-1)*s+ma
    f=s*cycle
    for j in range(rest):
        f+=l[j]
        ans=max(ans,f)
    return ans

A=max_c
for start in range(n):
    A=max(A,calc(start))
print(A)
