n, k = list(map(int,input().split()))
jump = list(map(int,input().split()))
arr = list(map(int,input().split()))
best = - (10**28)
VV = []
 
for i in range(n):
  if i not in VV:
    order = []
    prev_pos = i
    visited = []
    sub_best = 0
    order = []
    l = 0
 
    for jjj in range(n):
      order.append(arr[prev_pos])
      #sub_best = max(sub_best, sum(order))
      visited.append(prev_pos)
      VV.append(prev_pos)
      l += 1
 
      next_pos = jump[prev_pos] - 1
      prev_pos = next_pos
      if next_pos in visited:
        break
 
    if sum(order)<1:
      limit = min(k,l*2)
      ss = 0
    else:
      limit = (k % l) + l
      limit = min(k,limit)
      ss = sum(order) * ((k//l) - 1)
      ss = max(ss,0) 
 
    sub_best = ss
   # print(limit, order, ss, sub_best)
    
    order = order + order + order
    
    for chuj in range(1,limit+1):
      kk = sum(order[0:0+chuj])
      best = max(best, sub_best + kk)
      for op in range(len(order)-chuj):
        kk -= order[op]
        kk += order[op+chuj]
        best = max(best, sub_best + kk)
      
print(best) n, k = list(map(int,input().split())) n list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split k jump = list(map(int,input().split())) jump list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split arr = list(map(int,input().split())) arr list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split best = - (10**28) best - (10**28) 10**28 10 28 VV = [] VV [] for i in range(n):
  if i not in VV:
    order = []
    prev_pos = i
    visited = []
    sub_best = 0
    order = []
    l = 0
 
    for jjj in range(n):
      order.append(arr[prev_pos])
      #sub_best = max(sub_best, sum(order))
      visited.append(prev_pos)
      VV.append(prev_pos)
      l += 1
 
      next_pos = jump[prev_pos] - 1
      prev_pos = next_pos
      if next_pos in visited:
        break
 
    if sum(order)<1:
      limit = min(k,l*2)
      ss = 0
    else:
      limit = (k % l) + l
      limit = min(k,limit)
      ss = sum(order) * ((k//l) - 1)
      ss = max(ss,0) 
 
    sub_best = ss
   # print(limit, order, ss, sub_best)
    
    order = order + order + order
    
    for chuj in range(1,limit+1):
      kk = sum(order[0:0+chuj])
      best = max(best, sub_best + kk)
      for op in range(len(order)-chuj):
        kk -= order[op]
        kk += order[op+chuj]
        best = max(best, sub_best + kk)
       i range(n) range n if i not in VV:
    order = []
    prev_pos = i
    visited = []
    sub_best = 0
    order = []
    l = 0
 
    for jjj in range(n):
      order.append(arr[prev_pos])
      #sub_best = max(sub_best, sum(order))
      visited.append(prev_pos)
      VV.append(prev_pos)
      l += 1
 
      next_pos = jump[prev_pos] - 1
      prev_pos = next_pos
      if next_pos in visited:
        break
 
    if sum(order)<1:
      limit = min(k,l*2)
      ss = 0
    else:
      limit = (k % l) + l
      limit = min(k,limit)
      ss = sum(order) * ((k//l) - 1)
      ss = max(ss,0) 
 
    sub_best = ss
   # print(limit, order, ss, sub_best)
    
    order = order + order + order
    
    for chuj in range(1,limit+1):
      kk = sum(order[0:0+chuj])
      best = max(best, sub_best + kk)
      for op in range(len(order)-chuj):
        kk -= order[op]
        kk += order[op+chuj]
        best = max(best, sub_best + kk)
       i not in VV i VV order = [] order [] prev_pos = i prev_pos i visited = [] visited [] sub_best = 0 sub_best 0 order = [] order [] l = 0 l 0 for jjj in range(n):
      order.append(arr[prev_pos])
      #sub_best = max(sub_best, sum(order))
      visited.append(prev_pos)
      VV.append(prev_pos)
      l += 1
 
      next_pos = jump[prev_pos] - 1
      prev_pos = next_pos
      if next_pos in visited:
        break
 
     jjj range(n) range n order.append(arr[prev_pos]) order.append order append arr[prev_pos] arr prev_pos visited.append(prev_pos) visited.append visited append prev_pos VV.append(prev_pos) VV.append VV append prev_pos l += 1 l 1 next_pos = jump[prev_pos] - 1 next_pos jump[prev_pos] - 1 jump[prev_pos] jump prev_pos 1 prev_pos = next_pos prev_pos next_pos if next_pos in visited:
        break
 
     next_pos in visited next_pos visited break if sum(order)<1:
      limit = min(k,l*2)
      ss = 0
    else:
      limit = (k % l) + l
      limit = min(k,limit)
      ss = sum(order) * ((k//l) - 1)
      ss = max(ss,0) 
 
     sum(order)<1 sum(order) sum order 1 limit = min(k,l*2) limit min(k,l*2) min k l*2 l 2 ss = 0 ss 0 limit = (k % l) + l limit (k % l) + l k % l k l l limit = min(k,limit) limit min(k,limit) min k limit ss = sum(order) * ((k//l) - 1) ss sum(order) * ((k//l) - 1) sum(order) sum order (k//l) - 1 k//l k l 1 ss = max(ss,0) ss max(ss,0) max ss 0 sub_best = ss sub_best ss order = order + order + order order order + order + order order + order order order order for chuj in range(1,limit+1):
      kk = sum(order[0:0+chuj])
      best = max(best, sub_best + kk)
      for op in range(len(order)-chuj):
        kk -= order[op]
        kk += order[op+chuj]
        best = max(best, sub_best + kk)
       chuj range(1,limit+1) range 1 limit+1 limit 1 kk = sum(order[0:0+chuj]) kk sum(order[0:0+chuj]) sum order[0:0+chuj] order 0:0+chuj 0 0+chuj 0 chuj best = max(best, sub_best + kk) best max(best, sub_best + kk) max best sub_best + kk sub_best kk for op in range(len(order)-chuj):
        kk -= order[op]
        kk += order[op+chuj]
        best = max(best, sub_best + kk)
       op range(len(order)-chuj) range len(order)-chuj len(order) len order chuj kk -= order[op] kk order[op] order op kk += order[op+chuj] kk order[op+chuj] order op+chuj op chuj best = max(best, sub_best + kk) best max(best, sub_best + kk) max best sub_best + kk sub_best kk print(best) print best order = [] [] order ss = sum(order) * ((k//l) - 1) sum(order) * ((k//l) - 1) ss visited = [] [] visited ss = max(ss,0) max(ss,0) ss n, k = list(map(int,input().split())) list(map(int,input().split())) n next_pos = jump[prev_pos] - 1 jump[prev_pos] - 1 next_pos kk += order[op+chuj] order[op+chuj] kk ss = 0 0 ss sub_best = 0 0 sub_best l += 1 1 l prev_pos = next_pos next_pos prev_pos arr = list(map(int,input().split())) list(map(int,input().split())) arr kk -= order[op] order[op] kk l = 0 0 l k = list(map(int,input().split())) list(map(int,input().split())) k VV = [] [] VV best = - (10**28) - (10**28) best kk = sum(order[0:0+chuj]) sum(order[0:0+chuj]) kk limit = (k % l) + l (k % l) + l limit order = order + order + order order + order + order order best = max(best, sub_best + kk) max(best, sub_best + kk) best prev_pos = i i prev_pos limit = min(k,limit) min(k,limit) limit best = max(best, sub_best + kk) max(best, sub_best + kk) best order = [] [] order sub_best = ss ss sub_best limit = min(k,l*2) min(k,l*2) limit jump = list(map(int,input().split())) list(map(int,input().split())) jump
n, k = list(map(int,input().split()))
jump = list(map(int,input().split()))
arr = list(map(int,input().split()))
best = - (10**28)
VV = []
 
for i in range(n):
  if i not in VV:
    order = []
    prev_pos = i
    visited = []
    sub_best = 0
    order = []
    l = 0
 
    for jjj in range(n):
      order.append(arr[prev_pos])
      #sub_best = max(sub_best, sum(order))
      visited.append(prev_pos)
      VV.append(prev_pos)
      l += 1
 
      next_pos = jump[prev_pos] - 1
      prev_pos = next_pos
      if next_pos in visited:
        break
 
    if sum(order)<1:
      limit = min(k,l*2)
      ss = 0
    else:
      limit = (k % l) + l
      limit = min(k,limit)
      ss = sum(order) * ((k//l) - 1)
      ss = max(ss,0) 
 
    sub_best = ss
   # print(limit, order, ss, sub_best)
    
    order = order + order + order
    
    for chuj in range(1,limit+1):
      kk = sum(order[0:0+chuj])
      best = max(best, sub_best + kk)
      for op in range(len(order)-chuj):
        kk -= order[op]
        kk += order[op+chuj]
        best = max(best, sub_best + kk)
      
print(best)