n, k =map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
roop = []
seen = [0 for  _ in range(n)]
count = 0
for i in range(n):
    if seen[i]>0:
        continue
    else:
        count+=1
        seen[i] = count
        roop.append([c[i]])
        t = i
        while i!=p[t]-1:
            seen[p[t]-1]=count
            roop[-1].append(c[p[t]-1])
            t = p[t]-1
#print(seen)
#print(roop)
ans = max(c)

for i in roop:
    #print(ans)
    
    s = sum(i)
    ii = i+i
    #print(ii)
    if len(i)>=k:
        for j in range(1, k+1):
            temp = sum(ii[:j])
            for l in range(len(i)):
                if l!=0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                    #temp = sum(ii[l:l+j])
                #print(i, j, l, temp)
                ans  = max(ans, temp)
    else:
        for j in range(1, len(i)+1):
            temp=  sum(ii[:j])
            for l in range(len(i)):
                if l != 0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                #print(temp)
                if s>0:
                    temp1 = temp+s*((k-j)//len(i))
                else:
                    temp1 = temp
                
                #print(i, j, l, temp)
                ans  = max(ans, temp)
                ans  = max(ans, temp1)
            
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 roop = [] roop [] 0 for  _ in range(n) for  _ in range(n) _ range(n) range n for  _ in range(n) 0 seen = [0 for  _ in range(n)] seen [0 for  _ in range(n)] count = 0 count 0 for i in range(n):
    if seen[i]>0:
        continue
    else:
        count+=1
        seen[i] = count
        roop.append([c[i]])
        t = i
        while i!=p[t]-1:
            seen[p[t]-1]=count
            roop[-1].append(c[p[t]-1])
            t = p[t]-1
#print(seen)
#print(roop) i range(n) range n if seen[i]>0:
        continue
    else:
        count+=1
        seen[i] = count
        roop.append([c[i]])
        t = i
        while i!=p[t]-1:
            seen[p[t]-1]=count
            roop[-1].append(c[p[t]-1])
            t = p[t]-1
#print(seen)
#print(roop) seen[i]>0 seen[i] seen i 0 continue count+=1 count 1 seen[i] = count seen[i] seen i count roop.append([c[i]]) roop.append roop append [c[i]] c[i] c i t = i t i while i!=p[t]-1:
            seen[p[t]-1]=count
            roop[-1].append(c[p[t]-1])
            t = p[t]-1
#print(seen)
#print(roop) i!=p[t]-1 i p[t]-1 p[t] p t 1 seen[p[t]-1]=count seen[p[t]-1] seen p[t]-1 p[t] p t 1 count roop[-1].append(c[p[t]-1]) [-1].append [-1] roop -1 append c[p[t]-1] c p[t]-1 p[t] p t 1 t = p[t]-1 t p[t]-1 p[t] p t 1 ans = max(c) ans max(c) max c for i in roop:
    #print(ans)
    
    s = sum(i)
    ii = i+i
    #print(ii)
    if len(i)>=k:
        for j in range(1, k+1):
            temp = sum(ii[:j])
            for l in range(len(i)):
                if l!=0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                    #temp = sum(ii[l:l+j])
                #print(i, j, l, temp)
                ans  = max(ans, temp)
    else:
        for j in range(1, len(i)+1):
            temp=  sum(ii[:j])
            for l in range(len(i)):
                if l != 0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                #print(temp)
                if s>0:
                    temp1 = temp+s*((k-j)//len(i))
                else:
                    temp1 = temp
                
                #print(i, j, l, temp)
                ans  = max(ans, temp)
                ans  = max(ans, temp1)
             i roop s = sum(i) s sum(i) sum i ii = i+i ii i+i i i if len(i)>=k:
        for j in range(1, k+1):
            temp = sum(ii[:j])
            for l in range(len(i)):
                if l!=0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                    #temp = sum(ii[l:l+j])
                #print(i, j, l, temp)
                ans  = max(ans, temp)
    else:
        for j in range(1, len(i)+1):
            temp=  sum(ii[:j])
            for l in range(len(i)):
                if l != 0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                #print(temp)
                if s>0:
                    temp1 = temp+s*((k-j)//len(i))
                else:
                    temp1 = temp
                
                #print(i, j, l, temp)
                ans  = max(ans, temp)
                ans  = max(ans, temp1)
             len(i)>=k len(i) len i k for j in range(1, k+1):
            temp = sum(ii[:j])
            for l in range(len(i)):
                if l!=0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                    #temp = sum(ii[l:l+j])
                #print(i, j, l, temp)
                ans  = max(ans, temp)
     j range(1, k+1) range 1 k+1 k 1 temp = sum(ii[:j]) temp sum(ii[:j]) sum ii[:j] ii :j j for l in range(len(i)):
                if l!=0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                    #temp = sum(ii[l:l+j])
                #print(i, j, l, temp)
                ans  = max(ans, temp)
     l range(len(i)) range len(i) len i if l!=0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                    #temp = sum(ii[l:l+j])
                #print(i, j, l, temp)
                 l!=0 l 0 temp+=ii[l+j-1] temp ii[l+j-1] ii l+j-1 l+j l j 1 temp-=ii[l-1] temp ii[l-1] ii l-1 l 1 ans  = max(ans, temp) ans max(ans, temp) max ans temp for j in range(1, len(i)+1):
            temp=  sum(ii[:j])
            for l in range(len(i)):
                if l != 0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                #print(temp)
                if s>0:
                    temp1 = temp+s*((k-j)//len(i))
                else:
                    temp1 = temp
                
                #print(i, j, l, temp)
                ans  = max(ans, temp)
                ans  = max(ans, temp1)
             j range(1, len(i)+1) range 1 len(i)+1 len(i) len i 1 temp=  sum(ii[:j]) temp sum(ii[:j]) sum ii[:j] ii :j j for l in range(len(i)):
                if l != 0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                #print(temp)
                if s>0:
                    temp1 = temp+s*((k-j)//len(i))
                else:
                    temp1 = temp
                
                #print(i, j, l, temp)
                ans  = max(ans, temp)
                ans  = max(ans, temp1)
             l range(len(i)) range len(i) len i if l != 0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                #print(temp)
                 l != 0 l 0 temp+=ii[l+j-1] temp ii[l+j-1] ii l+j-1 l+j l j 1 temp-=ii[l-1] temp ii[l-1] ii l-1 l 1 if s>0:
                    temp1 = temp+s*((k-j)//len(i))
                else:
                    temp1 = temp
                
                #print(i, j, l, temp)
                 s>0 s 0 temp1 = temp+s*((k-j)//len(i)) temp1 temp+s*((k-j)//len(i)) temp s*((k-j)//len(i)) s (k-j)//len(i) k-j k j len(i) len i temp1 = temp temp1 temp ans  = max(ans, temp) ans max(ans, temp) max ans temp ans  = max(ans, temp1) ans max(ans, temp1) max ans temp1 print(ans) print ans k =map(int, input().split()) map(int, input().split()) k ans  = max(ans, temp) max(ans, temp) ans temp-=ii[l-1] ii[l-1] temp count = 0 0 count p = list(map(int, input().split())) list(map(int, input().split())) p temp = sum(ii[:j]) sum(ii[:j]) temp t = p[t]-1 p[t]-1 t ii = i+i i+i ii ans  = max(ans, temp1) max(ans, temp1) ans ans  = max(ans, temp) max(ans, temp) ans temp=  sum(ii[:j]) sum(ii[:j]) temp temp1 = temp+s*((k-j)//len(i)) temp+s*((k-j)//len(i)) temp1 count+=1 1 count n, k =map(int, input().split()) map(int, input().split()) n seen = [0 for  _ in range(n)] [0 for  _ in range(n)] seen roop = [] [] roop temp1 = temp temp temp1 s = sum(i) sum(i) s temp-=ii[l-1] ii[l-1] temp c = list(map(int, input().split())) list(map(int, input().split())) c t = i i t ans = max(c) max(c) ans temp+=ii[l+j-1] ii[l+j-1] temp temp+=ii[l+j-1] ii[l+j-1] temp
n, k =map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
roop = []
seen = [0 for  _ in range(n)]
count = 0
for i in range(n):
    if seen[i]>0:
        continue
    else:
        count+=1
        seen[i] = count
        roop.append([c[i]])
        t = i
        while i!=p[t]-1:
            seen[p[t]-1]=count
            roop[-1].append(c[p[t]-1])
            t = p[t]-1
#print(seen)
#print(roop)
ans = max(c)

for i in roop:
    #print(ans)
    
    s = sum(i)
    ii = i+i
    #print(ii)
    if len(i)>=k:
        for j in range(1, k+1):
            temp = sum(ii[:j])
            for l in range(len(i)):
                if l!=0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                    #temp = sum(ii[l:l+j])
                #print(i, j, l, temp)
                ans  = max(ans, temp)
    else:
        for j in range(1, len(i)+1):
            temp=  sum(ii[:j])
            for l in range(len(i)):
                if l != 0:
                    temp+=ii[l+j-1]
                    temp-=ii[l-1]
                #print(temp)
                if s>0:
                    temp1 = temp+s*((k-j)//len(i))
                else:
                    temp1 = temp
                
                #print(i, j, l, temp)
                ans  = max(ans, temp)
                ans  = max(ans, temp1)
            
print(ans)

        
