import sys
input = sys.stdin.readline

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):
    now = i
    visit = [False]*N
    visit[now] = True
    l = []
    
    while True:
        nex = P[now]-1
        l.append(C[nex])
        
        if visit[nex]:
            break
        
        visit[nex] = True
        now = nex
    
    s = sum(l)
    acc = 0
    
    for j in range(min(K, len(l))):
        acc += l[j]
        ans = max(ans, acc+max(0, (K-j-1)//len(l)*s))

print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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):
    now = i
    visit = [False]*N
    visit[now] = True
    l = []
    
    while True:
        nex = P[now]-1
        l.append(C[nex])
        
        if visit[nex]:
            break
        
        visit[nex] = True
        now = nex
    
    s = sum(l)
    acc = 0
    
    for j in range(min(K, len(l))):
        acc += l[j]
        ans = max(ans, acc+max(0, (K-j-1)//len(l)*s)) i range(N) range N now = i now i visit = [False]*N visit [False]*N [False] False N visit[now] = True visit[now] visit now True l = [] l [] while True:
        nex = P[now]-1
        l.append(C[nex])
        
        if visit[nex]:
            break
        
        visit[nex] = True
        now = nex
    
     True nex = P[now]-1 nex P[now]-1 P[now] P now 1 l.append(C[nex]) l.append l append C[nex] C nex if visit[nex]:
            break
        
         visit[nex] visit nex break visit[nex] = True visit[nex] visit nex True now = nex now nex s = sum(l) s sum(l) sum l acc = 0 acc 0 for j in range(min(K, len(l))):
        acc += l[j]
        ans = max(ans, acc+max(0, (K-j-1)//len(l)*s)) j range(min(K, len(l))) range min(K, len(l)) min K len(l) len l acc += l[j] acc l[j] l j ans = max(ans, acc+max(0, (K-j-1)//len(l)*s)) ans max(ans, acc+max(0, (K-j-1)//len(l)*s)) max ans acc+max(0, (K-j-1)//len(l)*s) acc max(0, (K-j-1)//len(l)*s) max 0 (K-j-1)//len(l)*s (K-j-1)//len(l) K-j-1 K-j K j 1 len(l) len l s print(ans) print ans visit = [False]*N [False]*N visit now = nex nex now nex = P[now]-1 P[now]-1 nex input = sys.stdin.readline sys.stdin.readline input acc += l[j] l[j] acc acc = 0 0 acc s = sum(l) sum(l) s ans = max(ans, acc+max(0, (K-j-1)//len(l)*s)) max(ans, acc+max(0, (K-j-1)//len(l)*s)) ans l = [] [] l N, K = map(int, input().split()) map(int, input().split()) N now = i i now P = list(map(int, input().split())) list(map(int, input().split())) P K = map(int, input().split()) map(int, input().split()) K ans = -10**18 -10**18 ans C = list(map(int, input().split())) list(map(int, input().split())) C
import sys
input = sys.stdin.readline

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):
    now = i
    visit = [False]*N
    visit[now] = True
    l = []
    
    while True:
        nex = P[now]-1
        l.append(C[nex])
        
        if visit[nex]:
            break
        
        visit[nex] = True
        now = nex
    
    s = sum(l)
    acc = 0
    
    for j in range(min(K, len(l))):
        acc += l[j]
        ans = max(ans, acc+max(0, (K-j-1)//len(l)*s))

print(ans)