#出来るだけループして端数を全部みるだとうまくいかない（最初と最後に負のスコアが固まっている場合など）

from numba import njit,i8
import numpy as np


n,k = map(int,input().split())
p = np.array([int(i)-1 for i in input().split()],np.int64)
c = np.array(list(map(int,input().split())),np.int64)

@njit(i8(i8,i8[:],i8[:]), cache = True)
def calc_score(x,p,c):
    n = p.size
    score = 0
    score_list = np.zeros(n,np.int64)
    cnt = 0
    visit = [0]*n
    while not visit[p[x]]:
        x = p[x]
        visit[x] = 1
        cnt += 1
        if cnt<=k:
            score += c[x]
            score_list[cnt-1] = score
        else:
            return score_list[:k].max()
    
    if score>0:
        tmp = np.arange(cnt)
        return (score_list[tmp] + ((k-1-tmp)//cnt) * score).max()
    else:
        return score_list[:cnt].max()

ans = max(calc_score(i,p,c) for i in range(n))
print(ans) from numba import njit,i8 import numpy as np numpy n,k = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 p = np.array([int(i)-1 for i in input().split()],np.int64) p np.array([int(i)-1 for i in input().split()],np.int64) np.array np array [int(i)-1 for i in input().split()] np.int64 np int64 c = np.array(list(map(int,input().split())),np.int64) c np.array(list(map(int,input().split())),np.int64) np.array np array list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split np.int64 np int64 def calc_score(x,p,c):
    n = p.size
    score = 0
    score_list = np.zeros(n,np.int64)
    cnt = 0
    visit = [0]*n
    while not visit[p[x]]:
        x = p[x]
        visit[x] = 1
        cnt += 1
        if cnt<=k:
            score += c[x]
            score_list[cnt-1] = score
        else:
            return score_list[:k].max()
    
    if score>0:
        tmp = np.arange(cnt)
        return (score_list[tmp] + ((k-1-tmp)//cnt) * score).max()
    else:
        return score_list[:cnt].max() calc_score n = p.size n p.size p size score = 0 score 0 score_list = np.zeros(n,np.int64) score_list np.zeros(n,np.int64) np.zeros np zeros n np.int64 np int64 cnt = 0 cnt 0 visit = [0]*n visit [0]*n [0] 0 n while not visit[p[x]]:
        x = p[x]
        visit[x] = 1
        cnt += 1
        if cnt<=k:
            score += c[x]
            score_list[cnt-1] = score
        else:
            return score_list[:k].max()
    
     not visit[p[x]] visit[p[x]] visit p[x] p x x = p[x] x p[x] p x visit[x] = 1 visit[x] visit x 1 cnt += 1 cnt 1 if cnt<=k:
            score += c[x]
            score_list[cnt-1] = score
        else:
            return score_list[:k].max()
    
     cnt<=k cnt k score += c[x] score c[x] c x score_list[cnt-1] = score score_list[cnt-1] score_list cnt-1 cnt 1 score return score_list[:k].max() score_list[:k].max() [:k].max [:k] score_list :k k max if score>0:
        tmp = np.arange(cnt)
        return (score_list[tmp] + ((k-1-tmp)//cnt) * score).max()
    else:
        return score_list[:cnt].max() score>0 score 0 tmp = np.arange(cnt) tmp np.arange(cnt) np.arange np arange cnt return (score_list[tmp] + ((k-1-tmp)//cnt) * score).max() (score_list[tmp] + ((k-1-tmp)//cnt) * score).max() score_list[tmp] + ((k-1-tmp)//cnt) * score).max score_list[tmp] + ((k-1-tmp)//cnt) * score score_list[tmp] score_list tmp ((k-1-tmp)//cnt) * score (k-1-tmp)//cnt k-1-tmp k-1 k 1 tmp cnt score max return score_list[:cnt].max() score_list[:cnt].max() [:cnt].max [:cnt] score_list :cnt cnt max x x p p c c @njit(i8(i8,i8[:],i8[:]), cache = True) calc_score i8(i8,i8[:],i8[:]), cache = True @njit(i8(i8,i8[:],i8[:]), cache = True) njit i8(i8,i8[:],i8[:]) i8 i8 i8[:] i8 : i8[:] i8 : cache = True cache True calc_score ans = max(calc_score(i,p,c) for i in range(n)) ans max(calc_score(i,p,c) for i in range(n)) max calc_score(i,p,c) calc_score i p c print(ans) print ans ans = max(calc_score(i,p,c) for i in range(n)) max(calc_score(i,p,c) for i in range(n)) ans k = map(int,input().split()) map(int,input().split()) k c = np.array(list(map(int,input().split())),np.int64) np.array(list(map(int,input().split())),np.int64) c n,k = map(int,input().split()) map(int,input().split()) n def calc_score(x,p,c):
    n = p.size
    score = 0
    score_list = np.zeros(n,np.int64)
    cnt = 0
    visit = [0]*n
    while not visit[p[x]]:
        x = p[x]
        visit[x] = 1
        cnt += 1
        if cnt<=k:
            score += c[x]
            score_list[cnt-1] = score
        else:
            return score_list[:k].max()
    
    if score>0:
        tmp = np.arange(cnt)
        return (score_list[tmp] + ((k-1-tmp)//cnt) * score).max()
    else:
        return score_list[:cnt].max() def calc_score(x,p,c):
    n = p.size
    score = 0
    score_list = np.zeros(n,np.int64)
    cnt = 0
    visit = [0]*n
    while not visit[p[x]]:
        x = p[x]
        visit[x] = 1
        cnt += 1
        if cnt<=k:
            score += c[x]
            score_list[cnt-1] = score
        else:
            return score_list[:k].max()
    
    if score>0:
        tmp = np.arange(cnt)
        return (score_list[tmp] + ((k-1-tmp)//cnt) * score).max()
    else:
        return score_list[:cnt].max() calc_score p = np.array([int(i)-1 for i in input().split()],np.int64) np.array([int(i)-1 for i in input().split()],np.int64) p
#出来るだけループして端数を全部みるだとうまくいかない（最初と最後に負のスコアが固まっている場合など）

from numba import njit,i8
import numpy as np


n,k = map(int,input().split())
p = np.array([int(i)-1 for i in input().split()],np.int64)
c = np.array(list(map(int,input().split())),np.int64)

@njit(i8(i8,i8[:],i8[:]), cache = True)
def calc_score(x,p,c):
    n = p.size
    score = 0
    score_list = np.zeros(n,np.int64)
    cnt = 0
    visit = [0]*n
    while not visit[p[x]]:
        x = p[x]
        visit[x] = 1
        cnt += 1
        if cnt<=k:
            score += c[x]
            score_list[cnt-1] = score
        else:
            return score_list[:k].max()
    
    if score>0:
        tmp = np.arange(cnt)
        return (score_list[tmp] + ((k-1-tmp)//cnt) * score).max()
    else:
        return score_list[:cnt].max()

ans = max(calc_score(i,p,c) for i in range(n))
print(ans)