import math
import sys
import os

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N,K = LI()
P = LI()
C = LI()

P = list(map(lambda x:x-1,P))

res = -INF
used = [False] * N
ss = []

for i in range(N):
    # print('i',i)
    if used[i]:
        continue
    cur = i
    s = []
    while not used[cur]:
        used[cur] = True
        s.append(C[cur])
        cur = P[cur]
        # print(cur)
    ss.append(s)

# print(ss)

for vec in ss:
    M = len(vec)
    # 2周の累積和
    sum = [0]*(2*M + 1)
    for i in range(2*M):
        sum[i+1] = sum[i]+vec[i%M]
    
    # amari[r] := 連続するr個の最大値
    amari = [-INF]*M
    for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j],sum[i+j] - sum[i])
    
    for r in range(M):
        if r>K:
            continue
        q = (K-r)//M
        if r==0 and q==0:
            continue
        if sum[M]>0:
            res = max(res, amari[r] + sum[M] * q)
        elif r > 0:
            res = max(res, amari[r])
print(res)

 import math math import sys sys import os os sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 def _S(): return sys.stdin.readline().rstrip() _S def I(): return int(_S()) I def LS(): return list(_S().split()) LS def LI(): return list(map(int,LS())) LI if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r") os.getenv("LOCAL") os.getenv os getenv "LOCAL" inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt' inputFile os.path.splitext(os.path.basename(__file__))[0]+'.txt' os.path.splitext(os.path.basename(__file__))[0] os.path.splitext(os.path.basename(__file__)) os.path.splitext os.path os path splitext os.path.basename(__file__) os.path.basename os.path os path basename __file__ 0 '.txt' = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt' basename_without_ext os.path.splitext(os.path.basename(__file__))[0]+'.txt' sys.stdin = open(inputFile, "r") sys.stdin sys stdin open(inputFile, "r") open inputFile "r" INF = float("inf") INF float("inf") float "inf" N,K = LI() N LI() LI K P = LI() P LI() LI C = LI() C LI() LI P = list(map(lambda x:x-1,P)) P list(map(lambda x:x-1,P)) list map(lambda x:x-1,P) map lambda x:x-1 x-1 x 1 x P res = -INF res -INF INF used = [False] * N used [False] * N [False] False N ss = [] ss [] for i in range(N):
    # print('i',i)
    if used[i]:
        continue
    cur = i
    s = []
    while not used[cur]:
        used[cur] = True
        s.append(C[cur])
        cur = P[cur]
        # print(cur)
    ss.append(s)

# print(ss) i range(N) range N if used[i]:
        continue
     used[i] used i continue cur = i cur i s = [] s [] while not used[cur]:
        used[cur] = True
        s.append(C[cur])
        cur = P[cur]
        # print(cur)
     not used[cur] used[cur] used cur used[cur] = True used[cur] used cur True s.append(C[cur]) s.append s append C[cur] C cur cur = P[cur] cur P[cur] P cur ss.append(s) ss.append ss append s for vec in ss:
    M = len(vec)
    # 2周の累積和
    sum = [0]*(2*M + 1)
    for i in range(2*M):
        sum[i+1] = sum[i]+vec[i%M]
    
    # amari[r] := 連続するr個の最大値
    amari = [-INF]*M
    for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j],sum[i+j] - sum[i])
    
    for r in range(M):
        if r>K:
            continue
        q = (K-r)//M
        if r==0 and q==0:
            continue
        if sum[M]>0:
            res = max(res, amari[r] + sum[M] * q)
        elif r > 0:
            res = max(res, amari[r]) vec ss M = len(vec) M len(vec) len vec sum = [0]*(2*M + 1) sum [0]*(2*M + 1) [0] 0 2*M + 1 2*M 2 M 1 for i in range(2*M):
        sum[i+1] = sum[i]+vec[i%M]
    
    # amari[r] := 連続するr個の最大値
     i range(2*M) range 2*M 2 M sum[i+1] = sum[i]+vec[i%M] sum[i+1] sum i+1 i 1 sum[i]+vec[i%M] sum[i] sum i vec[i%M] vec i%M i M amari = [-INF]*M amari [-INF]*M [-INF] -INF INF M for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j],sum[i+j] - sum[i])
    
     i range(M) range M for j in range(M):
            amari[j] = max(amari[j],sum[i+j] - sum[i])
    
     j range(M) range M amari[j] = max(amari[j],sum[i+j] - sum[i]) amari[j] amari j max(amari[j],sum[i+j] - sum[i]) max amari[j] amari j sum[i+j] - sum[i] sum[i+j] sum i+j i j sum[i] sum i for r in range(M):
        if r>K:
            continue
        q = (K-r)//M
        if r==0 and q==0:
            continue
        if sum[M]>0:
            res = max(res, amari[r] + sum[M] * q)
        elif r > 0:
            res = max(res, amari[r]) r range(M) range M if r>K:
            continue
         r>K r K continue q = (K-r)//M q (K-r)//M K-r K r M if r==0 and q==0:
            continue
         r==0 and q==0 r==0 r 0 q==0 q 0 continue if sum[M]>0:
            res = max(res, amari[r] + sum[M] * q)
        elif r > 0:
            res = max(res, amari[r]) sum[M]>0 sum[M] sum M 0 res = max(res, amari[r] + sum[M] * q) res max(res, amari[r] + sum[M] * q) max res amari[r] + sum[M] * q amari[r] amari r sum[M] * q sum[M] sum M q elif r > 0:
            res = max(res, amari[r]) r > 0 r 0 res = max(res, amari[r]) res max(res, amari[r]) max res amari[r] amari r print(res) print res basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt' os.path.splitext(os.path.basename(__file__))[0]+'.txt' basename_without_ext N,K = LI() LI() N ss = [] [] ss amari = [-INF]*M [-INF]*M amari res = -INF -INF res res = max(res, amari[r]) max(res, amari[r]) res P = LI() LI() P def LI(): return list(map(int,LS())) def LI(): return list(map(int,LS())) LI P = list(map(lambda x:x-1,P)) list(map(lambda x:x-1,P)) P M = len(vec) len(vec) M sum = [0]*(2*M + 1) [0]*(2*M + 1) sum inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt' os.path.splitext(os.path.basename(__file__))[0]+'.txt' inputFile q = (K-r)//M (K-r)//M q res = max(res, amari[r] + sum[M] * q) max(res, amari[r] + sum[M] * q) res C = LI() LI() C used = [False] * N [False] * N used K = LI() LI() K def LS(): return list(_S().split()) def LS(): return list(_S().split()) LS def I(): return int(_S()) def I(): return int(_S()) I cur = i i cur cur = P[cur] P[cur] cur INF = float("inf") float("inf") INF s = [] [] s def _S(): return sys.stdin.readline().rstrip() def _S(): return sys.stdin.readline().rstrip() _S
import math
import sys
import os

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N,K = LI()
P = LI()
C = LI()

P = list(map(lambda x:x-1,P))

res = -INF
used = [False] * N
ss = []

for i in range(N):
    # print('i',i)
    if used[i]:
        continue
    cur = i
    s = []
    while not used[cur]:
        used[cur] = True
        s.append(C[cur])
        cur = P[cur]
        # print(cur)
    ss.append(s)

# print(ss)

for vec in ss:
    M = len(vec)
    # 2周の累積和
    sum = [0]*(2*M + 1)
    for i in range(2*M):
        sum[i+1] = sum[i]+vec[i%M]
    
    # amari[r] := 連続するr個の最大値
    amari = [-INF]*M
    for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j],sum[i+j] - sum[i])
    
    for r in range(M):
        if r>K:
            continue
        q = (K-r)//M
        if r==0 and q==0:
            continue
        if sum[M]>0:
            res = max(res, amari[r] + sum[M] * q)
        elif r > 0:
            res = max(res, amari[r])
print(res)


