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

ans = -10**100

for i in range(n):
    chk = [0]*n
    chk[p[i]] = 1
    now = p[i]
    cnt = 1
    val = [-10**100]*n
    val[0] = c[p[i]]
    while 1 == 1:
        if cnt >= k:
            break
        #print(p[now])
        if chk[p[now]] == 1:
            break
        else:
            chk[p[now]] = 1
            val[cnt] = val[cnt-1]+c[p[now]]
            now = p[now]
            cnt += 1
    
    cnt -= 1
    
    #print(val)
    
    if val[cnt] <= 0:
        ans = max(ans,max(val))
    else:
        num = k // (cnt+1)
        amari = k % (cnt+1)
        #print(cnt,k)
        for j in range(cnt+1):
            if j < amari:
                tmp = num*val[cnt]
            else:
                tmp = (num-1)*val[cnt]
            ans = max(ans,tmp+val[j])
            #print(ans,tmp,val[j],j,cnt,num)
    
    #ans = max(ans,max(val))
    
print(ans) 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()] ans = -10**100 ans -10**100 -10 100 for i in range(n):
    chk = [0]*n
    chk[p[i]] = 1
    now = p[i]
    cnt = 1
    val = [-10**100]*n
    val[0] = c[p[i]]
    while 1 == 1:
        if cnt >= k:
            break
        #print(p[now])
        if chk[p[now]] == 1:
            break
        else:
            chk[p[now]] = 1
            val[cnt] = val[cnt-1]+c[p[now]]
            now = p[now]
            cnt += 1
    
    cnt -= 1
    
    #print(val)
    
    if val[cnt] <= 0:
        ans = max(ans,max(val))
    else:
        num = k // (cnt+1)
        amari = k % (cnt+1)
        #print(cnt,k)
        for j in range(cnt+1):
            if j < amari:
                tmp = num*val[cnt]
            else:
                tmp = (num-1)*val[cnt]
            ans = max(ans,tmp+val[j])
            #print(ans,tmp,val[j],j,cnt,num)
    
    #ans = max(ans,max(val))
     i range(n) range n chk = [0]*n chk [0]*n [0] 0 n chk[p[i]] = 1 chk[p[i]] chk p[i] p i 1 now = p[i] now p[i] p i cnt = 1 cnt 1 val = [-10**100]*n val [-10**100]*n [-10**100] -10**100 -10 100 n val[0] = c[p[i]] val[0] val 0 c[p[i]] c p[i] p i while 1 == 1:
        if cnt >= k:
            break
        #print(p[now])
        if chk[p[now]] == 1:
            break
        else:
            chk[p[now]] = 1
            val[cnt] = val[cnt-1]+c[p[now]]
            now = p[now]
            cnt += 1
    
     1 == 1 1 1 if cnt >= k:
            break
        #print(p[now])
         cnt >= k cnt k break if chk[p[now]] == 1:
            break
        else:
            chk[p[now]] = 1
            val[cnt] = val[cnt-1]+c[p[now]]
            now = p[now]
            cnt += 1
    
     chk[p[now]] == 1 chk[p[now]] chk p[now] p now 1 break chk[p[now]] = 1 chk[p[now]] chk p[now] p now 1 val[cnt] = val[cnt-1]+c[p[now]] val[cnt] val cnt val[cnt-1]+c[p[now]] val[cnt-1] val cnt-1 cnt 1 c[p[now]] c p[now] p now now = p[now] now p[now] p now cnt += 1 cnt 1 cnt -= 1 cnt 1 if val[cnt] <= 0:
        ans = max(ans,max(val))
    else:
        num = k // (cnt+1)
        amari = k % (cnt+1)
        #print(cnt,k)
        for j in range(cnt+1):
            if j < amari:
                tmp = num*val[cnt]
            else:
                tmp = (num-1)*val[cnt]
            ans = max(ans,tmp+val[j])
            #print(ans,tmp,val[j],j,cnt,num)
    
    #ans = max(ans,max(val))
     val[cnt] <= 0 val[cnt] val cnt 0 ans = max(ans,max(val)) ans max(ans,max(val)) max ans max(val) max val num = k // (cnt+1) num k // (cnt+1) k cnt+1 cnt 1 amari = k % (cnt+1) amari k % (cnt+1) k cnt+1 cnt 1 for j in range(cnt+1):
            if j < amari:
                tmp = num*val[cnt]
            else:
                tmp = (num-1)*val[cnt]
            ans = max(ans,tmp+val[j])
            #print(ans,tmp,val[j],j,cnt,num)
    
    #ans = max(ans,max(val))
     j range(cnt+1) range cnt+1 cnt 1 if j < amari:
                tmp = num*val[cnt]
            else:
                tmp = (num-1)*val[cnt]
             j < amari j amari tmp = num*val[cnt] tmp num*val[cnt] num val[cnt] val cnt tmp = (num-1)*val[cnt] tmp (num-1)*val[cnt] num-1 num 1 val[cnt] val cnt ans = max(ans,tmp+val[j]) ans max(ans,tmp+val[j]) max ans tmp+val[j] tmp val[j] val j print(ans) print ans num = k // (cnt+1) k // (cnt+1) num tmp = (num-1)*val[cnt] (num-1)*val[cnt] tmp val = [-10**100]*n [-10**100]*n val now = p[now] p[now] now ans = max(ans,max(val)) max(ans,max(val)) ans tmp = num*val[cnt] num*val[cnt] tmp chk = [0]*n [0]*n chk n,k = map(int,input().split()) map(int,input().split()) n cnt = 1 1 cnt cnt -= 1 1 cnt ans = -10**100 -10**100 ans now = p[i] p[i] now ans = max(ans,tmp+val[j]) max(ans,tmp+val[j]) ans p = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] p amari = k % (cnt+1) k % (cnt+1) amari cnt += 1 1 cnt k = map(int,input().split()) map(int,input().split()) k c = [int(i) for i in input().split()] [int(i) for i in input().split()] c
n,k = map(int,input().split())
p = [int(i)-1 for i in input().split()]
c = [int(i) for i in input().split()]

ans = -10**100

for i in range(n):
    chk = [0]*n
    chk[p[i]] = 1
    now = p[i]
    cnt = 1
    val = [-10**100]*n
    val[0] = c[p[i]]
    while 1 == 1:
        if cnt >= k:
            break
        #print(p[now])
        if chk[p[now]] == 1:
            break
        else:
            chk[p[now]] = 1
            val[cnt] = val[cnt-1]+c[p[now]]
            now = p[now]
            cnt += 1
    
    cnt -= 1
    
    #print(val)
    
    if val[cnt] <= 0:
        ans = max(ans,max(val))
    else:
        num = k // (cnt+1)
        amari = k % (cnt+1)
        #print(cnt,k)
        for j in range(cnt+1):
            if j < amari:
                tmp = num*val[cnt]
            else:
                tmp = (num-1)*val[cnt]
            ans = max(ans,tmp+val[j])
            #print(ans,tmp,val[j],j,cnt,num)
    
    #ans = max(ans,max(val))
    
print(ans)