N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = -float('inf')
for i in range(N):
    piece = i+1
    temp = 0
    hoge = []
    #hogehoge = []
    key = 0
    for j in range(N):
        if j+1 > K:
            key = 1
            break
        piece = P[piece-1]
        temp += C[piece-1]
        #hogehoge.append(C[piece-1])
        hoge.append(temp)
        if piece == i+1:
            break
    if key == 1:
        ans = max(ans,max(hoge))
    elif hoge[-1]<=0:
        ans = max(ans,max(hoge))
    else:
        d = K//len(hoge)
        r = K%len(hoge)
        if r != 0:
            peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r]))
        else:
            peak = hoge[-1]*(d-1)+max(hoge)
        ans = max(ans,peak)
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 = -float('inf') ans -float('inf') float('inf') float 'inf' for i in range(N):
    piece = i+1
    temp = 0
    hoge = []
    #hogehoge = []
    key = 0
    for j in range(N):
        if j+1 > K:
            key = 1
            break
        piece = P[piece-1]
        temp += C[piece-1]
        #hogehoge.append(C[piece-1])
        hoge.append(temp)
        if piece == i+1:
            break
    if key == 1:
        ans = max(ans,max(hoge))
    elif hoge[-1]<=0:
        ans = max(ans,max(hoge))
    else:
        d = K//len(hoge)
        r = K%len(hoge)
        if r != 0:
            peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r]))
        else:
            peak = hoge[-1]*(d-1)+max(hoge)
        ans = max(ans,peak) i range(N) range N piece = i+1 piece i+1 i 1 temp = 0 temp 0 hoge = [] hoge [] key = 0 key 0 for j in range(N):
        if j+1 > K:
            key = 1
            break
        piece = P[piece-1]
        temp += C[piece-1]
        #hogehoge.append(C[piece-1])
        hoge.append(temp)
        if piece == i+1:
            break
     j range(N) range N if j+1 > K:
            key = 1
            break
         j+1 > K j+1 j 1 K key = 1 key 1 break piece = P[piece-1] piece P[piece-1] P piece-1 piece 1 temp += C[piece-1] temp C[piece-1] C piece-1 piece 1 hoge.append(temp) hoge.append hoge append temp if piece == i+1:
            break
     piece == i+1 piece i+1 i 1 break if key == 1:
        ans = max(ans,max(hoge))
    elif hoge[-1]<=0:
        ans = max(ans,max(hoge))
    else:
        d = K//len(hoge)
        r = K%len(hoge)
        if r != 0:
            peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r]))
        else:
            peak = hoge[-1]*(d-1)+max(hoge)
        ans = max(ans,peak) key == 1 key 1 ans = max(ans,max(hoge)) ans max(ans,max(hoge)) max ans max(hoge) max hoge elif hoge[-1]<=0:
        ans = max(ans,max(hoge))
     hoge[-1]<=0 hoge[-1] hoge -1 0 ans = max(ans,max(hoge)) ans max(ans,max(hoge)) max ans max(hoge) max hoge d = K//len(hoge) d K//len(hoge) K len(hoge) len hoge r = K%len(hoge) r K%len(hoge) K len(hoge) len hoge if r != 0:
            peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r]))
        else:
            peak = hoge[-1]*(d-1)+max(hoge)
         r != 0 r 0 peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r])) peak max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r])) max hoge[-1]*(d-1)+max(hoge[r:]) hoge[-1]*(d-1) hoge[-1] hoge -1 d-1 d 1 max(hoge[r:]) max hoge[r:] hoge r: r hoge[-1]*d+max(hoge[:r]) hoge[-1]*d hoge[-1] hoge -1 d max(hoge[:r]) max hoge[:r] hoge :r r peak = hoge[-1]*(d-1)+max(hoge) peak hoge[-1]*(d-1)+max(hoge) hoge[-1]*(d-1) hoge[-1] hoge -1 d-1 d 1 max(hoge) max hoge ans = max(ans,peak) ans max(ans,peak) max ans peak print(ans) print ans key = 0 0 key hoge = [] [] hoge piece = i+1 i+1 piece piece = P[piece-1] P[piece-1] piece r = K%len(hoge) K%len(hoge) r ans = max(ans,peak) max(ans,peak) ans temp += C[piece-1] C[piece-1] temp peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r])) max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r])) peak P = list(map(int,input().split())) list(map(int,input().split())) P d = K//len(hoge) K//len(hoge) d key = 1 1 key temp = 0 0 temp C = list(map(int,input().split())) list(map(int,input().split())) C peak = hoge[-1]*(d-1)+max(hoge) hoge[-1]*(d-1)+max(hoge) peak ans = -float('inf') -float('inf') ans ans = max(ans,max(hoge)) max(ans,max(hoge)) ans K = map(int,input().split()) map(int,input().split()) K N,K = map(int,input().split()) map(int,input().split()) N ans = max(ans,max(hoge)) max(ans,max(hoge)) ans
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = -float('inf')
for i in range(N):
    piece = i+1
    temp = 0
    hoge = []
    #hogehoge = []
    key = 0
    for j in range(N):
        if j+1 > K:
            key = 1
            break
        piece = P[piece-1]
        temp += C[piece-1]
        #hogehoge.append(C[piece-1])
        hoge.append(temp)
        if piece == i+1:
            break
    if key == 1:
        ans = max(ans,max(hoge))
    elif hoge[-1]<=0:
        ans = max(ans,max(hoge))
    else:
        d = K//len(hoge)
        r = K%len(hoge)
        if r != 0:
            peak = max(hoge[-1]*(d-1)+max(hoge[r:]),hoge[-1]*d+max(hoge[:r]))
        else:
            peak = hoge[-1]*(d-1)+max(hoge)
        ans = max(ans,peak)
print(ans)
