import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))


ans = -INF
for i in range(N):
    nx = i
    total = 0
    L = [] 
    while True:
        nx = P[nx]
        total += C[nx]
        L.append(C[nx])
        if nx == i:
            break
    l = len(L)
    tmp = 0
    for j in range(l):
        if j + 1 > K:
            break
        tmp += L[j]
        now = tmp
        if total > 0:
            e = (K - j - 1) // l
            now += total * e
        ans = max(ans, now)
    # if total > 0:
    #     now = total * (K // l)
    #     ans = max(ans, now)
    #     r = K % l
    #     for j in range(r):
    #         now += L[j]
    #         ans = max(ans, now)
print(ans)

 import sys sys import itertools itertools import time time import math math import heapq heapq from collections import defaultdict sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 INF = 10 ** 9 + 7 INF 10 ** 9 + 7 10 ** 9 10 9 7 read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda x: int(x) - 1, input().split())) P 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 C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = -INF ans -INF INF for i in range(N):
    nx = i
    total = 0
    L = [] 
    while True:
        nx = P[nx]
        total += C[nx]
        L.append(C[nx])
        if nx == i:
            break
    l = len(L)
    tmp = 0
    for j in range(l):
        if j + 1 > K:
            break
        tmp += L[j]
        now = tmp
        if total > 0:
            e = (K - j - 1) // l
            now += total * e
        ans = max(ans, now)
    # if total > 0:
    #     now = total * (K // l)
    #     ans = max(ans, now)
    #     r = K % l
    #     for j in range(r):
    #         now += L[j]
    #         ans = max(ans, now) i range(N) range N nx = i nx i total = 0 total 0 L = [] L [] while True:
        nx = P[nx]
        total += C[nx]
        L.append(C[nx])
        if nx == i:
            break
     True nx = P[nx] nx P[nx] P nx total += C[nx] total C[nx] C nx L.append(C[nx]) L.append L append C[nx] C nx if nx == i:
            break
     nx == i nx i break l = len(L) l len(L) len L tmp = 0 tmp 0 for j in range(l):
        if j + 1 > K:
            break
        tmp += L[j]
        now = tmp
        if total > 0:
            e = (K - j - 1) // l
            now += total * e
        ans = max(ans, now)
    # if total > 0:
    #     now = total * (K // l)
    #     ans = max(ans, now)
    #     r = K % l
    #     for j in range(r):
    #         now += L[j]
    #         ans = max(ans, now) j range(l) range l if j + 1 > K:
            break
         j + 1 > K j + 1 j 1 K break tmp += L[j] tmp L[j] L j now = tmp now tmp if total > 0:
            e = (K - j - 1) // l
            now += total * e
         total > 0 total 0 e = (K - j - 1) // l e (K - j - 1) // l K - j - 1 K - j K j 1 l now += total * e now total * e total e ans = max(ans, now) ans max(ans, now) max ans now print(ans) print ans tmp = 0 0 tmp e = (K - j - 1) // l (K - j - 1) // l e tmp += L[j] L[j] tmp readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines l = len(L) len(L) l ans = max(ans, now) max(ans, now) ans nx = P[nx] P[nx] nx P = list(map(lambda x: int(x) - 1, input().split())) list(map(lambda x: int(x) - 1, input().split())) P L = [] [] L readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline now += total * e total * e now INF = 10 ** 9 + 7 10 ** 9 + 7 INF nx = i i nx ans = -INF -INF ans read = sys.stdin.buffer.read sys.stdin.buffer.read read K = map(int, input().split()) map(int, input().split()) K N, K = map(int, input().split()) map(int, input().split()) N now = tmp tmp now C = list(map(int, input().split())) list(map(int, input().split())) C total += C[nx] C[nx] total total = 0 0 total
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))


ans = -INF
for i in range(N):
    nx = i
    total = 0
    L = [] 
    while True:
        nx = P[nx]
        total += C[nx]
        L.append(C[nx])
        if nx == i:
            break
    l = len(L)
    tmp = 0
    for j in range(l):
        if j + 1 > K:
            break
        tmp += L[j]
        now = tmp
        if total > 0:
            e = (K - j - 1) // l
            now += total * e
        ans = max(ans, now)
    # if total > 0:
    #     now = total * (K // l)
    #     ans = max(ans, now)
    #     r = K % l
    #     for j in range(r):
    #         now += L[j]
    #         ans = max(ans, now)
print(ans)


