n, k = map(int, input().split())
ps = list(map(int, input().split()))
cs = list(map(int, input().split()))

to_do = []
res = -float('inf')
for i in range(1,n+1):
    tmp_idx = i
    tmp_val = 0
    tmp_dic = {i:(0,0,i)}
    for j in range(1,min(n+1,k+1)):
        tmp_idx = ps[tmp_idx-1]
        tmp_val += cs[tmp_idx-1]
        res = max(res,tmp_val)
        if tmp_idx not in tmp_dic:
            tmp_dic[tmp_idx] = (j,tmp_val,tmp_idx)
        else:
            if tmp_val > tmp_dic[tmp_idx][1]:
                to_do.append([i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]])
            break

for do in to_do:
    itr = (k - do[1]) // do[4]
    if itr > 0:
        itr -= 1
    remain = k - do[1] - do[4] * itr
    tmp_res = do[3] + itr * do[5]
    res = max(res,tmp_res)
    now = do[2]
    for i in range(remain):
        now = ps[now-1]
        tmp_res += cs[now-1]
        res = max(res, tmp_res)
print(res) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k ps = list(map(int, input().split())) ps list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split cs = list(map(int, input().split())) cs list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split to_do = [] to_do [] res = -float('inf') res -float('inf') float('inf') float 'inf' for i in range(1,n+1):
    tmp_idx = i
    tmp_val = 0
    tmp_dic = {i:(0,0,i)}
    for j in range(1,min(n+1,k+1)):
        tmp_idx = ps[tmp_idx-1]
        tmp_val += cs[tmp_idx-1]
        res = max(res,tmp_val)
        if tmp_idx not in tmp_dic:
            tmp_dic[tmp_idx] = (j,tmp_val,tmp_idx)
        else:
            if tmp_val > tmp_dic[tmp_idx][1]:
                to_do.append([i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]])
            break i range(1,n+1) range 1 n+1 n 1 tmp_idx = i tmp_idx i tmp_val = 0 tmp_val 0 tmp_dic = {i:(0,0,i)} tmp_dic {i:(0,0,i)} i i (0,0,i) 0 0 i for j in range(1,min(n+1,k+1)):
        tmp_idx = ps[tmp_idx-1]
        tmp_val += cs[tmp_idx-1]
        res = max(res,tmp_val)
        if tmp_idx not in tmp_dic:
            tmp_dic[tmp_idx] = (j,tmp_val,tmp_idx)
        else:
            if tmp_val > tmp_dic[tmp_idx][1]:
                to_do.append([i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]])
            break j range(1,min(n+1,k+1)) range 1 min(n+1,k+1) min n+1 n 1 k+1 k 1 tmp_idx = ps[tmp_idx-1] tmp_idx ps[tmp_idx-1] ps tmp_idx-1 tmp_idx 1 tmp_val += cs[tmp_idx-1] tmp_val cs[tmp_idx-1] cs tmp_idx-1 tmp_idx 1 res = max(res,tmp_val) res max(res,tmp_val) max res tmp_val if tmp_idx not in tmp_dic:
            tmp_dic[tmp_idx] = (j,tmp_val,tmp_idx)
        else:
            if tmp_val > tmp_dic[tmp_idx][1]:
                to_do.append([i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]])
            break tmp_idx not in tmp_dic tmp_idx tmp_dic tmp_dic[tmp_idx] = (j,tmp_val,tmp_idx) tmp_dic[tmp_idx] tmp_dic tmp_idx (j,tmp_val,tmp_idx) j tmp_val tmp_idx if tmp_val > tmp_dic[tmp_idx][1]:
                to_do.append([i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]])
             tmp_val > tmp_dic[tmp_idx][1] tmp_val tmp_dic[tmp_idx][1] [tmp_idx] tmp_dic tmp_idx 1 to_do.append([i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]]) to_do.append to_do append [i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]] i tmp_dic[tmp_idx][0] [tmp_idx] tmp_dic tmp_idx 0 tmp_dic[tmp_idx][2] [tmp_idx] tmp_dic tmp_idx 2 tmp_dic[tmp_idx][1] [tmp_idx] tmp_dic tmp_idx 1 j-tmp_dic[tmp_idx][0] j tmp_dic[tmp_idx][0] [tmp_idx] tmp_dic tmp_idx 0 tmp_val-tmp_dic[tmp_idx][1] tmp_val tmp_dic[tmp_idx][1] [tmp_idx] tmp_dic tmp_idx 1 break for do in to_do:
    itr = (k - do[1]) // do[4]
    if itr > 0:
        itr -= 1
    remain = k - do[1] - do[4] * itr
    tmp_res = do[3] + itr * do[5]
    res = max(res,tmp_res)
    now = do[2]
    for i in range(remain):
        now = ps[now-1]
        tmp_res += cs[now-1]
        res = max(res, tmp_res) do to_do itr = (k - do[1]) // do[4] itr (k - do[1]) // do[4] k - do[1] k do[1] do 1 do[4] do 4 if itr > 0:
        itr -= 1
     itr > 0 itr 0 itr -= 1 itr 1 remain = k - do[1] - do[4] * itr remain k - do[1] - do[4] * itr k - do[1] k do[1] do 1 do[4] * itr do[4] do 4 itr tmp_res = do[3] + itr * do[5] tmp_res do[3] + itr * do[5] do[3] do 3 itr * do[5] itr do[5] do 5 res = max(res,tmp_res) res max(res,tmp_res) max res tmp_res now = do[2] now do[2] do 2 for i in range(remain):
        now = ps[now-1]
        tmp_res += cs[now-1]
        res = max(res, tmp_res) i range(remain) range remain now = ps[now-1] now ps[now-1] ps now-1 now 1 tmp_res += cs[now-1] tmp_res cs[now-1] cs now-1 now 1 res = max(res, tmp_res) res max(res, tmp_res) max res tmp_res print(res) print res cs = list(map(int, input().split())) list(map(int, input().split())) cs tmp_res += cs[now-1] cs[now-1] tmp_res to_do = [] [] to_do res = max(res, tmp_res) max(res, tmp_res) res tmp_idx = i i tmp_idx now = ps[now-1] ps[now-1] now tmp_val = 0 0 tmp_val tmp_idx = ps[tmp_idx-1] ps[tmp_idx-1] tmp_idx res = max(res,tmp_res) max(res,tmp_res) res res = max(res,tmp_val) max(res,tmp_val) res k = map(int, input().split()) map(int, input().split()) k res = -float('inf') -float('inf') res tmp_dic = {i:(0,0,i)} {i:(0,0,i)} tmp_dic now = do[2] do[2] now tmp_val += cs[tmp_idx-1] cs[tmp_idx-1] tmp_val itr -= 1 1 itr ps = list(map(int, input().split())) list(map(int, input().split())) ps tmp_res = do[3] + itr * do[5] do[3] + itr * do[5] tmp_res itr = (k - do[1]) // do[4] (k - do[1]) // do[4] itr n, k = map(int, input().split()) map(int, input().split()) n remain = k - do[1] - do[4] * itr k - do[1] - do[4] * itr remain
n, k = map(int, input().split())
ps = list(map(int, input().split()))
cs = list(map(int, input().split()))

to_do = []
res = -float('inf')
for i in range(1,n+1):
    tmp_idx = i
    tmp_val = 0
    tmp_dic = {i:(0,0,i)}
    for j in range(1,min(n+1,k+1)):
        tmp_idx = ps[tmp_idx-1]
        tmp_val += cs[tmp_idx-1]
        res = max(res,tmp_val)
        if tmp_idx not in tmp_dic:
            tmp_dic[tmp_idx] = (j,tmp_val,tmp_idx)
        else:
            if tmp_val > tmp_dic[tmp_idx][1]:
                to_do.append([i,tmp_dic[tmp_idx][0],tmp_dic[tmp_idx][2],tmp_dic[tmp_idx][1],j-tmp_dic[tmp_idx][0],tmp_val-tmp_dic[tmp_idx][1]])
            break

for do in to_do:
    itr = (k - do[1]) // do[4]
    if itr > 0:
        itr -= 1
    remain = k - do[1] - do[4] * itr
    tmp_res = do[3] + itr * do[5]
    res = max(res,tmp_res)
    now = do[2]
    for i in range(remain):
        now = ps[now-1]
        tmp_res += cs[now-1]
        res = max(res, tmp_res)
print(res)