#親要素を示す
par = {}
sum_union = {}
len_union = {}

# 初期化 -- すべての要素が根である --
def init(n):
  for i in range(n):
    par[i] = i
    sum_union[i] = Cs[i]
    len_union[i] = 1
  return 
  
# 根を求める
def root(x):
  if par[x] == x:
    return x
  else:
    #根の直下にxを置く
    par[x] = root(par[x])
    return par[x]

#　同じ集合に属するかどうかは根が同じかで判定
def same(x, y):
  return root(x) == root(y)

#　別々のUnion同士を融合する
def unite(x, y):
  rx = root(x)
  ry = root(y)
  if rx == ry:
    return
  else:
    par[rx] = ry
    tmp_sum = sum_union[rx] + sum_union[ry]
    tmp_len = len_union[rx] + len_union[ry]
    sum_union[ry] = tmp_sum
    len_union[ry] = tmp_len
    return
  
N, K = map(int, input().split())
Ps = list(map(lambda x: int(x) - 1, input().split()))
Cs = list(map(int, input().split()))


init(N)

# 結合
for i in range(N):
  unite(i, Ps[i])
  
max_result = -(10**9+1)
for i in range(N):
  ri = root(i)
  if sum_union[ri] <= 0:
    partial_range = min(K, len_union[ri])
    roop_sum = 0
  elif K < len_union[ri]:
    partial_range = K
    roop_sum = 0
  else:
    partial_range = (K % len_union[ri]) + len_union[ri]
    roop_sum = sum_union[ri] * ((K // len_union[ri])-1)
  pc = i
  sum_list = []
  sum_c = 0
  for _ in range(partial_range):
    pn = Ps[pc]
    sum_c += Cs[pn]
    sum_list.append(sum_c)
    pc = pn
    
  max_result = max(max_result, roop_sum + max(sum_list))
  
print(max_result)
  

 par = {} par {} sum_union = {} sum_union {} len_union = {} len_union {} def init(n):
  for i in range(n):
    par[i] = i
    sum_union[i] = Cs[i]
    len_union[i] = 1
  return 
  
# 根を求める init for i in range(n):
    par[i] = i
    sum_union[i] = Cs[i]
    len_union[i] = 1
   i range(n) range n par[i] = i par[i] par i i sum_union[i] = Cs[i] sum_union[i] sum_union i Cs[i] Cs i len_union[i] = 1 len_union[i] len_union i 1 return n n def root(x):
  if par[x] == x:
    return x
  else:
    #根の直下にxを置く
    par[x] = root(par[x])
    return par[x]

#　同じ集合に属するかどうかは根が同じかで判定 root if par[x] == x:
    return x
  else:
    #根の直下にxを置く
    par[x] = root(par[x])
    return par[x]

#　同じ集合に属するかどうかは根が同じかで判定 par[x] == x par[x] par x x return x x par[x] = root(par[x]) par[x] par x root(par[x]) root par[x] par x return par[x] par[x] par x x x def same(x, y):
  return root(x) == root(y)

#　別々のUnion同士を融合する same return root(x) == root(y) root(x) == root(y) root(x) root x root(y) root y x x y y def unite(x, y):
  rx = root(x)
  ry = root(y)
  if rx == ry:
    return
  else:
    par[rx] = ry
    tmp_sum = sum_union[rx] + sum_union[ry]
    tmp_len = len_union[rx] + len_union[ry]
    sum_union[ry] = tmp_sum
    len_union[ry] = tmp_len
    return
   unite rx = root(x) rx root(x) root x ry = root(y) ry root(y) root y if rx == ry:
    return
  else:
    par[rx] = ry
    tmp_sum = sum_union[rx] + sum_union[ry]
    tmp_len = len_union[rx] + len_union[ry]
    sum_union[ry] = tmp_sum
    len_union[ry] = tmp_len
    return
   rx == ry rx ry return par[rx] = ry par[rx] par rx ry tmp_sum = sum_union[rx] + sum_union[ry] tmp_sum sum_union[rx] + sum_union[ry] sum_union[rx] sum_union rx sum_union[ry] sum_union ry tmp_len = len_union[rx] + len_union[ry] tmp_len len_union[rx] + len_union[ry] len_union[rx] len_union rx len_union[ry] len_union ry sum_union[ry] = tmp_sum sum_union[ry] sum_union ry tmp_sum len_union[ry] = tmp_len len_union[ry] len_union ry tmp_len return x x y y N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K Ps = list(map(lambda x: int(x) - 1, input().split())) Ps list(map(lambda x: int(x) - 1, input().split())) list map(lambda x: int(x) - 1, input().split()) map lambda x: int(x) - 1 int(x) - 1 int(x) int x 1 x 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 init(N) init N for i in range(N):
  unite(i, Ps[i])
   i range(N) range N unite(i, Ps[i]) unite i Ps[i] Ps i max_result = -(10**9+1) max_result -(10**9+1) 10**9+1 10**9 10 9 1 for i in range(N):
  ri = root(i)
  if sum_union[ri] <= 0:
    partial_range = min(K, len_union[ri])
    roop_sum = 0
  elif K < len_union[ri]:
    partial_range = K
    roop_sum = 0
  else:
    partial_range = (K % len_union[ri]) + len_union[ri]
    roop_sum = sum_union[ri] * ((K // len_union[ri])-1)
  pc = i
  sum_list = []
  sum_c = 0
  for _ in range(partial_range):
    pn = Ps[pc]
    sum_c += Cs[pn]
    sum_list.append(sum_c)
    pc = pn
    
  max_result = max(max_result, roop_sum + max(sum_list))
   i range(N) range N ri = root(i) ri root(i) root i if sum_union[ri] <= 0:
    partial_range = min(K, len_union[ri])
    roop_sum = 0
  elif K < len_union[ri]:
    partial_range = K
    roop_sum = 0
  else:
    partial_range = (K % len_union[ri]) + len_union[ri]
    roop_sum = sum_union[ri] * ((K // len_union[ri])-1)
   sum_union[ri] <= 0 sum_union[ri] sum_union ri 0 partial_range = min(K, len_union[ri]) partial_range min(K, len_union[ri]) min K len_union[ri] len_union ri roop_sum = 0 roop_sum 0 elif K < len_union[ri]:
    partial_range = K
    roop_sum = 0
   K < len_union[ri] K len_union[ri] len_union ri partial_range = K partial_range K roop_sum = 0 roop_sum 0 partial_range = (K % len_union[ri]) + len_union[ri] partial_range (K % len_union[ri]) + len_union[ri] K % len_union[ri] K len_union[ri] len_union ri len_union[ri] len_union ri roop_sum = sum_union[ri] * ((K // len_union[ri])-1) roop_sum sum_union[ri] * ((K // len_union[ri])-1) sum_union[ri] sum_union ri (K // len_union[ri])-1 K // len_union[ri] K len_union[ri] len_union ri 1 pc = i pc i sum_list = [] sum_list [] sum_c = 0 sum_c 0 for _ in range(partial_range):
    pn = Ps[pc]
    sum_c += Cs[pn]
    sum_list.append(sum_c)
    pc = pn
    
   _ range(partial_range) range partial_range pn = Ps[pc] pn Ps[pc] Ps pc sum_c += Cs[pn] sum_c Cs[pn] Cs pn sum_list.append(sum_c) sum_list.append sum_list append sum_c pc = pn pc pn max_result = max(max_result, roop_sum + max(sum_list)) max_result max(max_result, roop_sum + max(sum_list)) max max_result roop_sum + max(sum_list) roop_sum max(sum_list) max sum_list print(max_result) print max_result par = {} {} par K = map(int, input().split()) map(int, input().split()) K partial_range = min(K, len_union[ri]) min(K, len_union[ri]) partial_range Cs = list(map(int, input().split())) list(map(int, input().split())) Cs partial_range = (K % len_union[ri]) + len_union[ri] (K % len_union[ri]) + len_union[ri] partial_range partial_range = K K partial_range roop_sum = 0 0 roop_sum len_union = {} {} len_union def same(x, y):
  return root(x) == root(y)

#　別々のUnion同士を融合する def same(x, y):
  return root(x) == root(y)

#　別々のUnion同士を融合する same def init(n):
  for i in range(n):
    par[i] = i
    sum_union[i] = Cs[i]
    len_union[i] = 1
  return 
  
# 根を求める def init(n):
  for i in range(n):
    par[i] = i
    sum_union[i] = Cs[i]
    len_union[i] = 1
  return 
  
# 根を求める init ri = root(i) root(i) ri sum_list = [] [] sum_list sum_union = {} {} sum_union pn = Ps[pc] Ps[pc] pn Ps = list(map(lambda x: int(x) - 1, input().split())) list(map(lambda x: int(x) - 1, input().split())) Ps max_result = max(max_result, roop_sum + max(sum_list)) max(max_result, roop_sum + max(sum_list)) max_result def unite(x, y):
  rx = root(x)
  ry = root(y)
  if rx == ry:
    return
  else:
    par[rx] = ry
    tmp_sum = sum_union[rx] + sum_union[ry]
    tmp_len = len_union[rx] + len_union[ry]
    sum_union[ry] = tmp_sum
    len_union[ry] = tmp_len
    return
   def unite(x, y):
  rx = root(x)
  ry = root(y)
  if rx == ry:
    return
  else:
    par[rx] = ry
    tmp_sum = sum_union[rx] + sum_union[ry]
    tmp_len = len_union[rx] + len_union[ry]
    sum_union[ry] = tmp_sum
    len_union[ry] = tmp_len
    return
   unite max_result = -(10**9+1) -(10**9+1) max_result def root(x):
  if par[x] == x:
    return x
  else:
    #根の直下にxを置く
    par[x] = root(par[x])
    return par[x]

#　同じ集合に属するかどうかは根が同じかで判定 def root(x):
  if par[x] == x:
    return x
  else:
    #根の直下にxを置く
    par[x] = root(par[x])
    return par[x]

#　同じ集合に属するかどうかは根が同じかで判定 root roop_sum = 0 0 roop_sum sum_c += Cs[pn] Cs[pn] sum_c pc = pn pn pc pc = i i pc N, K = map(int, input().split()) map(int, input().split()) N roop_sum = sum_union[ri] * ((K // len_union[ri])-1) sum_union[ri] * ((K // len_union[ri])-1) roop_sum sum_c = 0 0 sum_c
#親要素を示す
par = {}
sum_union = {}
len_union = {}

# 初期化 -- すべての要素が根である --
def init(n):
  for i in range(n):
    par[i] = i
    sum_union[i] = Cs[i]
    len_union[i] = 1
  return 
  
# 根を求める
def root(x):
  if par[x] == x:
    return x
  else:
    #根の直下にxを置く
    par[x] = root(par[x])
    return par[x]

#　同じ集合に属するかどうかは根が同じかで判定
def same(x, y):
  return root(x) == root(y)

#　別々のUnion同士を融合する
def unite(x, y):
  rx = root(x)
  ry = root(y)
  if rx == ry:
    return
  else:
    par[rx] = ry
    tmp_sum = sum_union[rx] + sum_union[ry]
    tmp_len = len_union[rx] + len_union[ry]
    sum_union[ry] = tmp_sum
    len_union[ry] = tmp_len
    return
  
N, K = map(int, input().split())
Ps = list(map(lambda x: int(x) - 1, input().split()))
Cs = list(map(int, input().split()))


init(N)

# 結合
for i in range(N):
  unite(i, Ps[i])
  
max_result = -(10**9+1)
for i in range(N):
  ri = root(i)
  if sum_union[ri] <= 0:
    partial_range = min(K, len_union[ri])
    roop_sum = 0
  elif K < len_union[ri]:
    partial_range = K
    roop_sum = 0
  else:
    partial_range = (K % len_union[ri]) + len_union[ri]
    roop_sum = sum_union[ri] * ((K // len_union[ri])-1)
  pc = i
  sum_list = []
  sum_c = 0
  for _ in range(partial_range):
    pn = Ps[pc]
    sum_c += Cs[pn]
    sum_list.append(sum_c)
    pc = pn
    
  max_result = max(max_result, roop_sum + max(sum_list))
  
print(max_result)
  


