#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18+7
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
p = inl()
c = inl()
p = [x-1 for x in p]
used = [False]*n
mx = -BIG
for i in range(n):
    if used[i]:
        continue
    j = p[i]
    used[i] = used[j] = True
    pt = c[j]
    ids = [i,j]
    acc = [0,pt]
    while j!=i:
        j = p[j]
        used[j] = True
        pt += c[j]
        ids.append(j)
        acc.append(pt)
    ids.pop()
    cycsum = acc[-1]
    t = len(ids)
    if k>=t:
        mx = max(mx, cycsum*(k//t))
        mx = max(mx, cycsum)
    for j in range(1,t):
        acc.append(acc[j]+cycsum)
    #ddprint(f"{ids=} {acc=} {cycsum=} {t=}")
    for j in range(t):
        for m in range(1,min(k+1,t)):
            mx = max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t)))
print(mx) printn = lambda x: print(x,end='') printn lambda x: print(x,end='') print(x,end='') print x end='' end '' x inn = lambda : int(input()) inn lambda : int(input()) int(input()) int input() input inl   = lambda: list(map(int, input().split())) inl lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split inm   = lambda:      map(int, input().split()) inm lambda:      map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split ins = lambda : input().strip() ins lambda : input().strip() input().strip() ().strip () input strip DBG = True DBG True BIG = 10**18+7 BIG 10**18+7 10**18 10 18 7 R = 10**9 + 7 R 10**9 + 7 10**9 10 9 7 def ddprint(x):
  if DBG:
    print(x) ddprint if DBG:
    print(x) DBG print(x) print x x x n,k = inm() n inm() inm k p = inl() p inl() inl c = inl() c inl() inl x-1 for x in p for x in p x p for x in p x-1 x 1 p = [x-1 for x in p] p [x-1 for x in p] used = [False]*n used [False]*n [False] False n mx = -BIG mx -BIG BIG for i in range(n):
    if used[i]:
        continue
    j = p[i]
    used[i] = used[j] = True
    pt = c[j]
    ids = [i,j]
    acc = [0,pt]
    while j!=i:
        j = p[j]
        used[j] = True
        pt += c[j]
        ids.append(j)
        acc.append(pt)
    ids.pop()
    cycsum = acc[-1]
    t = len(ids)
    if k>=t:
        mx = max(mx, cycsum*(k//t))
        mx = max(mx, cycsum)
    for j in range(1,t):
        acc.append(acc[j]+cycsum)
    #ddprint(f"{ids=} {acc=} {cycsum=} {t=}")
    for j in range(t):
        for m in range(1,min(k+1,t)):
            mx = max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t))) i range(n) range n if used[i]:
        continue
     used[i] used i continue j = p[i] j p[i] p i used[i] = used[j] = True used[i] used i True = used[j] = True used[j] used j True pt = c[j] pt c[j] c j ids = [i,j] ids [i,j] i j acc = [0,pt] acc [0,pt] 0 pt while j!=i:
        j = p[j]
        used[j] = True
        pt += c[j]
        ids.append(j)
        acc.append(pt)
     j!=i j i j = p[j] j p[j] p j used[j] = True used[j] used j True pt += c[j] pt c[j] c j ids.append(j) ids.append ids append j acc.append(pt) acc.append acc append pt ids.pop() ids.pop ids pop cycsum = acc[-1] cycsum acc[-1] acc -1 t = len(ids) t len(ids) len ids if k>=t:
        mx = max(mx, cycsum*(k//t))
        mx = max(mx, cycsum)
     k>=t k t mx = max(mx, cycsum*(k//t)) mx max(mx, cycsum*(k//t)) max mx cycsum*(k//t) cycsum k//t k t mx = max(mx, cycsum) mx max(mx, cycsum) max mx cycsum for j in range(1,t):
        acc.append(acc[j]+cycsum)
    #ddprint(f"{ids=} {acc=} {cycsum=} {t=}")
     j range(1,t) range 1 t acc.append(acc[j]+cycsum) acc.append acc append acc[j]+cycsum acc[j] acc j cycsum for j in range(t):
        for m in range(1,min(k+1,t)):
            mx = max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t))) j range(t) range t for m in range(1,min(k+1,t)):
            mx = max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t))) m range(1,min(k+1,t)) range 1 min(k+1,t) min k+1 k 1 t mx = max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t))) mx max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t))) max mx acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t)) acc[j+m]-acc[j] acc[j+m] acc j+m j m acc[j] acc j max(0,cycsum*((k-m)//t)) max 0 cycsum*((k-m)//t) cycsum (k-m)//t k-m k m t print(mx) print mx ins = lambda : input().strip() lambda : input().strip() ins inn = lambda : int(input()) lambda : int(input()) inn mx = max(mx, cycsum) max(mx, cycsum) mx def ddprint(x):
  if DBG:
    print(x) def ddprint(x):
  if DBG:
    print(x) ddprint n,k = inm() inm() n p = inl() inl() p DBG = True True DBG BIG = 10**18+7 10**18+7 BIG k = inm() inm() k j = p[j] p[j] j mx = -BIG -BIG mx ids = [i,j] [i,j] ids used = [False]*n [False]*n used pt += c[j] c[j] pt inm   = lambda:      map(int, input().split()) lambda:      map(int, input().split()) inm c = inl() inl() c acc = [0,pt] [0,pt] acc printn = lambda x: print(x,end='') lambda x: print(x,end='') printn pt = c[j] c[j] pt t = len(ids) len(ids) t R = 10**9 + 7 10**9 + 7 R mx = max(mx, cycsum*(k//t)) max(mx, cycsum*(k//t)) mx cycsum = acc[-1] acc[-1] cycsum p = [x-1 for x in p] [x-1 for x in p] p inl   = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) inl mx = max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t))) max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t))) mx j = p[i] p[i] j
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18+7
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
p = inl()
c = inl()
p = [x-1 for x in p]
used = [False]*n
mx = -BIG
for i in range(n):
    if used[i]:
        continue
    j = p[i]
    used[i] = used[j] = True
    pt = c[j]
    ids = [i,j]
    acc = [0,pt]
    while j!=i:
        j = p[j]
        used[j] = True
        pt += c[j]
        ids.append(j)
        acc.append(pt)
    ids.pop()
    cycsum = acc[-1]
    t = len(ids)
    if k>=t:
        mx = max(mx, cycsum*(k//t))
        mx = max(mx, cycsum)
    for j in range(1,t):
        acc.append(acc[j]+cycsum)
    #ddprint(f"{ids=} {acc=} {cycsum=} {t=}")
    for j in range(t):
        for m in range(1,min(k+1,t)):
            mx = max(mx, acc[j+m]-acc[j]+max(0,cycsum*((k-m)//t)))
print(mx)
