import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
MOD = 10**9+7

N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))

sumlist1 = [[0]*N for _ in range(N)]
walist = [[0]*N for _ in range(N)]
loopcount = [0]*N

for h in range(N):
    sumlist1[h][0] = C[h]
    walist[h][0] = C[h]
    now = h
    l = 0
    for i in range(1,N):
        now = P[now]-1

        if sumlist1[h][i-1] < 0:
            sumlist1[h][i] = C[now]
        else:
            sumlist1[h][i] += sumlist1[h][i-1] + C[now]
        walist[h][i] += walist[h][i-1] + C[now]
        
        l += 1
        if now == h:
            break
    else:
        l += 1
    loopcount[h] = l
#print(sumlist1)

maxlist = [0]*N
for i in range(N):
    T = loopcount[i]
    wa = walist[i][T-1]
    #print(wa,T)

    loop = K//T
    amari = K%T

    if wa >= 0:
        if loop == 0:
            if amari != 0:
                maxlist[i] = wa*loop + max(sumlist1[i][:amari])
            else:
                maxlist[i] = max(sumlist1[i][:amari])
        else:
            if amari != 0:
                maxlist[i] = max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i]))
            else:
                maxlist[i] = max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T]))
            

    else:
        if loop == 0:
            maxlist[i] = max(sumlist1[i][:amari])
        else:
            maxlist[i] = max(sumlist1[i])

print(max(maxlist))
#print(sumlist1) import bisect bisect import collections collections import copy copy import functools functools import heapq heapq import math math import sys sys from collections import deque from collections import defaultdict input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 MOD = 10**9+7 MOD 10**9+7 10**9 10 9 7 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 [0]*N for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0]*N [0] 0 N sumlist1 = [[0]*N for _ in range(N)] sumlist1 [[0]*N for _ in range(N)] [0]*N for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0]*N [0] 0 N walist = [[0]*N for _ in range(N)] walist [[0]*N for _ in range(N)] loopcount = [0]*N loopcount [0]*N [0] 0 N for h in range(N):
    sumlist1[h][0] = C[h]
    walist[h][0] = C[h]
    now = h
    l = 0
    for i in range(1,N):
        now = P[now]-1

        if sumlist1[h][i-1] < 0:
            sumlist1[h][i] = C[now]
        else:
            sumlist1[h][i] += sumlist1[h][i-1] + C[now]
        walist[h][i] += walist[h][i-1] + C[now]
        
        l += 1
        if now == h:
            break
    else:
        l += 1
    loopcount[h] = l
#print(sumlist1) h range(N) range N sumlist1[h][0] = C[h] sumlist1[h][0] [h] sumlist1 h 0 C[h] C h walist[h][0] = C[h] walist[h][0] [h] walist h 0 C[h] C h now = h now h l = 0 l 0 for i in range(1,N):
        now = P[now]-1

        if sumlist1[h][i-1] < 0:
            sumlist1[h][i] = C[now]
        else:
            sumlist1[h][i] += sumlist1[h][i-1] + C[now]
        walist[h][i] += walist[h][i-1] + C[now]
        
        l += 1
        if now == h:
            break
    else:
        l += 1
     i range(1,N) range 1 N now = P[now]-1 now P[now]-1 P[now] P now 1 if sumlist1[h][i-1] < 0:
            sumlist1[h][i] = C[now]
        else:
            sumlist1[h][i] += sumlist1[h][i-1] + C[now]
         sumlist1[h][i-1] < 0 sumlist1[h][i-1] [h] sumlist1 h i-1 i 1 0 sumlist1[h][i] = C[now] sumlist1[h][i] [h] sumlist1 h i C[now] C now sumlist1[h][i] += sumlist1[h][i-1] + C[now] sumlist1[h][i] [h] sumlist1 h i sumlist1[h][i-1] + C[now] sumlist1[h][i-1] [h] sumlist1 h i-1 i 1 C[now] C now walist[h][i] += walist[h][i-1] + C[now] walist[h][i] [h] walist h i walist[h][i-1] + C[now] walist[h][i-1] [h] walist h i-1 i 1 C[now] C now l += 1 l 1 if now == h:
            break
     now == h now h break l += 1 l 1 loopcount[h] = l loopcount[h] loopcount h l maxlist = [0]*N maxlist [0]*N [0] 0 N for i in range(N):
    T = loopcount[i]
    wa = walist[i][T-1]
    #print(wa,T)

    loop = K//T
    amari = K%T

    if wa >= 0:
        if loop == 0:
            if amari != 0:
                maxlist[i] = wa*loop + max(sumlist1[i][:amari])
            else:
                maxlist[i] = max(sumlist1[i][:amari])
        else:
            if amari != 0:
                maxlist[i] = max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i]))
            else:
                maxlist[i] = max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T]))
            

    else:
        if loop == 0:
            maxlist[i] = max(sumlist1[i][:amari])
        else:
            maxlist[i] = max(sumlist1[i]) i range(N) range N T = loopcount[i] T loopcount[i] loopcount i wa = walist[i][T-1] wa walist[i][T-1] [i] walist i T-1 T 1 loop = K//T loop K//T K T amari = K%T amari K%T K T if wa >= 0:
        if loop == 0:
            if amari != 0:
                maxlist[i] = wa*loop + max(sumlist1[i][:amari])
            else:
                maxlist[i] = max(sumlist1[i][:amari])
        else:
            if amari != 0:
                maxlist[i] = max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i]))
            else:
                maxlist[i] = max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T]))
            

    else:
        if loop == 0:
            maxlist[i] = max(sumlist1[i][:amari])
        else:
            maxlist[i] = max(sumlist1[i]) wa >= 0 wa 0 if loop == 0:
            if amari != 0:
                maxlist[i] = wa*loop + max(sumlist1[i][:amari])
            else:
                maxlist[i] = max(sumlist1[i][:amari])
        else:
            if amari != 0:
                maxlist[i] = max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i]))
            else:
                maxlist[i] = max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T]))
            

     loop == 0 loop 0 if amari != 0:
                maxlist[i] = wa*loop + max(sumlist1[i][:amari])
            else:
                maxlist[i] = max(sumlist1[i][:amari])
         amari != 0 amari 0 maxlist[i] = wa*loop + max(sumlist1[i][:amari]) maxlist[i] maxlist i wa*loop + max(sumlist1[i][:amari]) wa*loop wa loop max(sumlist1[i][:amari]) max sumlist1[i][:amari] [i] sumlist1 i :amari amari maxlist[i] = max(sumlist1[i][:amari]) maxlist[i] maxlist i max(sumlist1[i][:amari]) max sumlist1[i][:amari] [i] sumlist1 i :amari amari if amari != 0:
                maxlist[i] = max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i]))
            else:
                maxlist[i] = max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T]))
            

     amari != 0 amari 0 maxlist[i] = max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i])) maxlist[i] maxlist i max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i])) max wa*loop + max(sumlist1[i][:amari]) wa*loop wa loop max(sumlist1[i][:amari]) max sumlist1[i][:amari] [i] sumlist1 i :amari amari wa*(loop-1) + max(sumlist1[i]) wa*(loop-1) wa loop-1 loop 1 max(sumlist1[i]) max sumlist1[i] sumlist1 i maxlist[i] = max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T])) maxlist[i] maxlist i max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T])) max wa*loop wa loop wa*(loop-1) + max(sumlist1[i][:T]) wa*(loop-1) wa loop-1 loop 1 max(sumlist1[i][:T]) max sumlist1[i][:T] [i] sumlist1 i :T T if loop == 0:
            maxlist[i] = max(sumlist1[i][:amari])
        else:
            maxlist[i] = max(sumlist1[i]) loop == 0 loop 0 maxlist[i] = max(sumlist1[i][:amari]) maxlist[i] maxlist i max(sumlist1[i][:amari]) max sumlist1[i][:amari] [i] sumlist1 i :amari amari maxlist[i] = max(sumlist1[i]) maxlist[i] maxlist i max(sumlist1[i]) max sumlist1[i] sumlist1 i print(max(maxlist)) print max(maxlist) max maxlist loopcount = [0]*N [0]*N loopcount l += 1 1 l now = h h now walist = [[0]*N for _ in range(N)] [[0]*N for _ in range(N)] walist l += 1 1 l T = loopcount[i] loopcount[i] T wa = walist[i][T-1] walist[i][T-1] wa N,K = map(int,input().split()) map(int,input().split()) N l = 0 0 l loop = K//T K//T loop K = map(int,input().split()) map(int,input().split()) K now = P[now]-1 P[now]-1 now C = list(map(int,input().split())) list(map(int,input().split())) C MOD = 10**9+7 10**9+7 MOD P = list(map(int,input().split())) list(map(int,input().split())) P maxlist = [0]*N [0]*N maxlist amari = K%T K%T amari input = sys.stdin.readline sys.stdin.readline input sumlist1 = [[0]*N for _ in range(N)] [[0]*N for _ in range(N)] sumlist1
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
MOD = 10**9+7

N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))

sumlist1 = [[0]*N for _ in range(N)]
walist = [[0]*N for _ in range(N)]
loopcount = [0]*N

for h in range(N):
    sumlist1[h][0] = C[h]
    walist[h][0] = C[h]
    now = h
    l = 0
    for i in range(1,N):
        now = P[now]-1

        if sumlist1[h][i-1] < 0:
            sumlist1[h][i] = C[now]
        else:
            sumlist1[h][i] += sumlist1[h][i-1] + C[now]
        walist[h][i] += walist[h][i-1] + C[now]
        
        l += 1
        if now == h:
            break
    else:
        l += 1
    loopcount[h] = l
#print(sumlist1)

maxlist = [0]*N
for i in range(N):
    T = loopcount[i]
    wa = walist[i][T-1]
    #print(wa,T)

    loop = K//T
    amari = K%T

    if wa >= 0:
        if loop == 0:
            if amari != 0:
                maxlist[i] = wa*loop + max(sumlist1[i][:amari])
            else:
                maxlist[i] = max(sumlist1[i][:amari])
        else:
            if amari != 0:
                maxlist[i] = max(wa*loop + max(sumlist1[i][:amari]), wa*(loop-1) + max(sumlist1[i]))
            else:
                maxlist[i] = max(wa*loop, wa*(loop-1) + max(sumlist1[i][:T]))
            

    else:
        if loop == 0:
            maxlist[i] = max(sumlist1[i][:amari])
        else:
            maxlist[i] = max(sumlist1[i])

print(max(maxlist))
#print(sumlist1)