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

ans=-10**18
for i in range(n):
    start,count=i,1
    val=c[start]
    while p[start]-1 != i:
        start = p[start]-1
        count += 1
        val += c[start]
    start = i
    if val > 0:
        a = (k//count-1)*val
        ans=max(a,ans)
        num=count+k%count
    else:
        a=0
        num=min(k,count)
    for _ in range(num):
        a += c[start]
        start=p[start]-1
        ans=max(a,ans)
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 ans=-10**18 ans -10**18 -10 18 for i in range(n):
    start,count=i,1
    val=c[start]
    while p[start]-1 != i:
        start = p[start]-1
        count += 1
        val += c[start]
    start = i
    if val > 0:
        a = (k//count-1)*val
        ans=max(a,ans)
        num=count+k%count
    else:
        a=0
        num=min(k,count)
    for _ in range(num):
        a += c[start]
        start=p[start]-1
        ans=max(a,ans) i range(n) range n start,count=i,1 start i count 1 val=c[start] val c[start] c start while p[start]-1 != i:
        start = p[start]-1
        count += 1
        val += c[start]
     p[start]-1 != i p[start]-1 p[start] p start 1 i start = p[start]-1 start p[start]-1 p[start] p start 1 count += 1 count 1 val += c[start] val c[start] c start start = i start i if val > 0:
        a = (k//count-1)*val
        ans=max(a,ans)
        num=count+k%count
    else:
        a=0
        num=min(k,count)
     val > 0 val 0 a = (k//count-1)*val a (k//count-1)*val k//count-1 k//count k count 1 val ans=max(a,ans) ans max(a,ans) max a ans num=count+k%count num count+k%count count k%count k count a=0 a 0 num=min(k,count) num min(k,count) min k count for _ in range(num):
        a += c[start]
        start=p[start]-1
        ans=max(a,ans) _ range(num) range num a += c[start] a c[start] c start start=p[start]-1 start p[start]-1 p[start] p start 1 ans=max(a,ans) ans max(a,ans) max a ans print(ans) print ans k = map(int,input().split()) map(int,input().split()) k start = i i start a = (k//count-1)*val (k//count-1)*val a num=count+k%count count+k%count num n,k = map(int,input().split()) map(int,input().split()) n val += c[start] c[start] val ans=max(a,ans) max(a,ans) ans val=c[start] c[start] val start=p[start]-1 p[start]-1 start p = list(map(int,input().split())) list(map(int,input().split())) p c = list(map(int,input().split())) list(map(int,input().split())) c count += 1 1 count count=i,1 1 count num=min(k,count) min(k,count) num ans=-10**18 -10**18 ans ans=max(a,ans) max(a,ans) ans start = p[start]-1 p[start]-1 start a += c[start] c[start] a a=0 0 a start,count=i,1 i start
n,k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans=-10**18
for i in range(n):
    start,count=i,1
    val=c[start]
    while p[start]-1 != i:
        start = p[start]-1
        count += 1
        val += c[start]
    start = i
    if val > 0:
        a = (k//count-1)*val
        ans=max(a,ans)
        num=count+k%count
    else:
        a=0
        num=min(k,count)
    for _ in range(num):
        a += c[start]
        start=p[start]-1
        ans=max(a,ans)
print(ans)