
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))


# 順列を各サイクルに分解する
used = [0] * N
ss = []
for i in range(N):
    if used[i]: continue
    cur = i
    s = []
    while used[cur] == 0:
        used[cur] = 1
        s.append(C[cur])
        cur = P[cur] - 1 # 0-index
    ss.append(s)

# 各サイクルごとに考える
res = -float("inf")
for vec in ss:
    M = len(vec)
    
    # サイクルを2周したものの累積和
    accum = [0] * (2*M+1)
    for i in range(M*2):
        accum[i+1] = accum[i] + vec[i%M]

    # amari[r] := 連続するr個の総和の最大値
    amari = [-float("inf")] * M
    for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j], accum[i+j]-accum[i])

    # あまりの長さで場合分け
    for r in range(M):
        if r > K: continue
        q = (K - r) // M # ループ回数
        if r == 0 and q == 0: continue

        if accum[M] > 0:
            res = max(res, amari[r] + accum[M] * q)
        elif r > 0:
            res = max(res, amari[r])

print(res)

 N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(int, input().split())) P list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split used = [0] * N used [0] * N [0] 0 N ss = [] ss [] for i in range(N):
    if used[i]: continue
    cur = i
    s = []
    while used[cur] == 0:
        used[cur] = 1
        s.append(C[cur])
        cur = P[cur] - 1 # 0-index
    ss.append(s)

# 各サイクルごとに考える i range(N) range N if used[i]: continue
     used[i] used i cur = i cur i s = [] s [] while used[cur] == 0:
        used[cur] = 1
        s.append(C[cur])
        cur = P[cur] - 1 # 0-index
     used[cur] == 0 used[cur] used cur 0 used[cur] = 1 used[cur] used cur 1 s.append(C[cur]) s.append s append C[cur] C cur cur = P[cur] - 1 cur P[cur] - 1 P[cur] P cur 1 ss.append(s) ss.append ss append s res = -float("inf") res -float("inf") float("inf") float "inf" for vec in ss:
    M = len(vec)
    
    # サイクルを2周したものの累積和
    accum = [0] * (2*M+1)
    for i in range(M*2):
        accum[i+1] = accum[i] + vec[i%M]

    # amari[r] := 連続するr個の総和の最大値
    amari = [-float("inf")] * M
    for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j], accum[i+j]-accum[i])

    # あまりの長さで場合分け
    for r in range(M):
        if r > K: continue
        q = (K - r) // M # ループ回数
        if r == 0 and q == 0: continue

        if accum[M] > 0:
            res = max(res, amari[r] + accum[M] * q)
        elif r > 0:
            res = max(res, amari[r]) vec ss M = len(vec) M len(vec) len vec accum = [0] * (2*M+1) accum [0] * (2*M+1) [0] 0 2*M+1 2*M 2 M 1 for i in range(M*2):
        accum[i+1] = accum[i] + vec[i%M]

    # amari[r] := 連続するr個の総和の最大値
     i range(M*2) range M*2 M 2 accum[i+1] = accum[i] + vec[i%M] accum[i+1] accum i+1 i 1 accum[i] + vec[i%M] accum[i] accum i vec[i%M] vec i%M i M amari = [-float("inf")] * M amari [-float("inf")] * M [-float("inf")] -float("inf") float("inf") float "inf" M for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j], accum[i+j]-accum[i])

    # あまりの長さで場合分け
     i range(M) range M for j in range(M):
            amari[j] = max(amari[j], accum[i+j]-accum[i])

    # あまりの長さで場合分け
     j range(M) range M amari[j] = max(amari[j], accum[i+j]-accum[i]) amari[j] amari j max(amari[j], accum[i+j]-accum[i]) max amari[j] amari j accum[i+j]-accum[i] accum[i+j] accum i+j i j accum[i] accum i for r in range(M):
        if r > K: continue
        q = (K - r) // M # ループ回数
        if r == 0 and q == 0: continue

        if accum[M] > 0:
            res = max(res, amari[r] + accum[M] * q)
        elif r > 0:
            res = max(res, amari[r]) r range(M) range M if r > K: continue
         r > K r K 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 if accum[M] > 0:
            res = max(res, amari[r] + accum[M] * q)
        elif r > 0:
            res = max(res, amari[r]) accum[M] > 0 accum[M] accum M 0 res = max(res, amari[r] + accum[M] * q) res max(res, amari[r] + accum[M] * q) max res amari[r] + accum[M] * q amari[r] amari r accum[M] * q accum[M] accum 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 K = map(int, input().split()) map(int, input().split()) K C = list(map(int, input().split())) list(map(int, input().split())) C accum = [0] * (2*M+1) [0] * (2*M+1) accum ss = [] [] ss M = len(vec) len(vec) M cur = P[cur] - 1 P[cur] - 1 cur N, K = map(int, input().split()) map(int, input().split()) N used = [0] * N [0] * N used res = max(res, amari[r] + accum[M] * q) max(res, amari[r] + accum[M] * q) res P = list(map(int, input().split())) list(map(int, input().split())) P amari = [-float("inf")] * M [-float("inf")] * M amari q = (K - r) // M (K - r) // M q cur = i i cur res = max(res, amari[r]) max(res, amari[r]) res s = [] [] s res = -float("inf") -float("inf") res

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))


# 順列を各サイクルに分解する
used = [0] * N
ss = []
for i in range(N):
    if used[i]: continue
    cur = i
    s = []
    while used[cur] == 0:
        used[cur] = 1
        s.append(C[cur])
        cur = P[cur] - 1 # 0-index
    ss.append(s)

# 各サイクルごとに考える
res = -float("inf")
for vec in ss:
    M = len(vec)
    
    # サイクルを2周したものの累積和
    accum = [0] * (2*M+1)
    for i in range(M*2):
        accum[i+1] = accum[i] + vec[i%M]

    # amari[r] := 連続するr個の総和の最大値
    amari = [-float("inf")] * M
    for i in range(M):
        for j in range(M):
            amari[j] = max(amari[j], accum[i+j]-accum[i])

    # あまりの長さで場合分け
    for r in range(M):
        if r > K: continue
        q = (K - r) // M # ループ回数
        if r == 0 and q == 0: continue

        if accum[M] > 0:
            res = max(res, amari[r] + accum[M] * q)
        elif r > 0:
            res = max(res, amari[r])

print(res)


