import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

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

for i in range(N):
    P[i] -= 1

# 何回で自分に戻ってくるか
ret = [0]*N
for i in range(N):
    p = i
    c = 1
    while True:
        if P[p] == i:
            ret[i] = c
            break
        else:
            p = P[p]
            c += 1

val = [[0]*(N+1) for _ in range(N)]
for i in range(N):
    p = i
    temp = 0
    for j in range(1,N+1):
        p = P[p]
        temp += C[p]
        val[i][j] = temp


ans = -float('inf')
for i in range(N):
    if val[i][ret[i]] <= 0:
        temp_max = max(val[i][1:min(N,K)+1])
    else:
        temp1 = val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1])
        temp2 = -float('inf')
        if K//ret[i] >= 1:
            temp2 = val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1])
        temp_max = max(temp1,temp2)
    ans = max(ans,temp_max)

print(ans) import sys sys import math math from collections import defaultdict from bisect import bisect_left, bisect_right sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 def input():
    return sys.stdin.readline()[:-1] input return sys.stdin.readline()[:-1] sys.stdin.readline()[:-1] sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline :-1 -1 mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 def I(): return int(input()) I def LI(): return list(map(int, input().split())) LI def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

################# LIR if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

################# row <= 0 row 0 [] for _ in range(col) for _ in range(col) _ range(col) range col for _ in range(col) [] return [[] for _ in range(col)] [[] for _ in range(col)] elif col == 1:
        return [I() for _ in range(row)]
     col == 1 col 1 I() for _ in range(row) for _ in range(row) _ range(row) range row for _ in range(row) I() I return [I() for _ in range(row)] [I() for _ in range(row)] LI() for _ in range(row) for _ in range(row) _ range(row) range row for _ in range(row) LI() LI read_all = [LI() for _ in range(row)] read_all [LI() for _ in range(row)] return map(list, zip(*read_all)) map(list, zip(*read_all)) map list zip(*read_all) zip *read_all read_all row row col col N,K = LI() N LI() LI K P = LI() P LI() LI C = LI() C LI() LI for i in range(N):
    P[i] -= 1

# 何回で自分に戻ってくるか i range(N) range N P[i] -= 1 P[i] P i 1 ret = [0]*N ret [0]*N [0] 0 N for i in range(N):
    p = i
    c = 1
    while True:
        if P[p] == i:
            ret[i] = c
            break
        else:
            p = P[p]
            c += 1 i range(N) range N p = i p i c = 1 c 1 while True:
        if P[p] == i:
            ret[i] = c
            break
        else:
            p = P[p]
            c += 1 True if P[p] == i:
            ret[i] = c
            break
        else:
            p = P[p]
            c += 1 P[p] == i P[p] P p i ret[i] = c ret[i] ret i c break p = P[p] p P[p] P p c += 1 c 1 [0]*(N+1) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0]*(N+1) [0] 0 N+1 N 1 val = [[0]*(N+1) for _ in range(N)] val [[0]*(N+1) for _ in range(N)] for i in range(N):
    p = i
    temp = 0
    for j in range(1,N+1):
        p = P[p]
        temp += C[p]
        val[i][j] = temp i range(N) range N p = i p i temp = 0 temp 0 for j in range(1,N+1):
        p = P[p]
        temp += C[p]
        val[i][j] = temp j range(1,N+1) range 1 N+1 N 1 p = P[p] p P[p] P p temp += C[p] temp C[p] C p val[i][j] = temp val[i][j] [i] val i j temp ans = -float('inf') ans -float('inf') float('inf') float 'inf' for i in range(N):
    if val[i][ret[i]] <= 0:
        temp_max = max(val[i][1:min(N,K)+1])
    else:
        temp1 = val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1])
        temp2 = -float('inf')
        if K//ret[i] >= 1:
            temp2 = val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1])
        temp_max = max(temp1,temp2)
    ans = max(ans,temp_max) i range(N) range N if val[i][ret[i]] <= 0:
        temp_max = max(val[i][1:min(N,K)+1])
    else:
        temp1 = val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1])
        temp2 = -float('inf')
        if K//ret[i] >= 1:
            temp2 = val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1])
        temp_max = max(temp1,temp2)
     val[i][ret[i]] <= 0 val[i][ret[i]] [i] val i ret[i] ret i 0 temp_max = max(val[i][1:min(N,K)+1]) temp_max max(val[i][1:min(N,K)+1]) max val[i][1:min(N,K)+1] [i] val i 1:min(N,K)+1 1 min(N,K)+1 min(N,K) min N K 1 temp1 = val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1]) temp1 val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1]) val[i][ret[i]]*(K//ret[i]) val[i][ret[i]] [i] val i ret[i] ret i K//ret[i] K ret[i] ret i max(val[i][:K-ret[i]*(K//ret[i])+1]) max val[i][:K-ret[i]*(K//ret[i])+1] [i] val i :K-ret[i]*(K//ret[i])+1 K-ret[i]*(K//ret[i])+1 K-ret[i]*(K//ret[i]) K ret[i]*(K//ret[i]) ret[i] ret i K//ret[i] K ret[i] ret i 1 temp2 = -float('inf') temp2 -float('inf') float('inf') float 'inf' if K//ret[i] >= 1:
            temp2 = val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1])
         K//ret[i] >= 1 K//ret[i] K ret[i] ret i 1 temp2 = val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1]) temp2 val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1]) val[i][ret[i]]*(K//ret[i]-1) val[i][ret[i]] [i] val i ret[i] ret i K//ret[i]-1 K//ret[i] K ret[i] ret i 1 max(val[i][:ret[i]+1]) max val[i][:ret[i]+1] [i] val i :ret[i]+1 ret[i]+1 ret[i] ret i 1 temp_max = max(temp1,temp2) temp_max max(temp1,temp2) max temp1 temp2 ans = max(ans,temp_max) ans max(ans,temp_max) max ans temp_max print(ans) print ans C = LI() LI() C c = 1 1 c p = i i p P = LI() LI() P ans = max(ans,temp_max) max(ans,temp_max) ans temp2 = -float('inf') -float('inf') temp2 ans = -float('inf') -float('inf') ans mod = 10**9 + 7 10**9 + 7 mod def LI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) LI N,K = LI() LI() N p = P[p] P[p] p temp_max = max(temp1,temp2) max(temp1,temp2) temp_max val = [[0]*(N+1) for _ in range(N)] [[0]*(N+1) for _ in range(N)] val p = i i p ret = [0]*N [0]*N ret def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

################# def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

################# LIR def input():
    return sys.stdin.readline()[:-1] def input():
    return sys.stdin.readline()[:-1] input temp1 = val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1]) val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1]) temp1 def I(): return int(input()) def I(): return int(input()) I K = LI() LI() K temp += C[p] C[p] temp temp2 = val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1]) val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1]) temp2 p = P[p] P[p] p temp_max = max(val[i][1:min(N,K)+1]) max(val[i][1:min(N,K)+1]) temp_max temp = 0 0 temp c += 1 1 c
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

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

for i in range(N):
    P[i] -= 1

# 何回で自分に戻ってくるか
ret = [0]*N
for i in range(N):
    p = i
    c = 1
    while True:
        if P[p] == i:
            ret[i] = c
            break
        else:
            p = P[p]
            c += 1

val = [[0]*(N+1) for _ in range(N)]
for i in range(N):
    p = i
    temp = 0
    for j in range(1,N+1):
        p = P[p]
        temp += C[p]
        val[i][j] = temp


ans = -float('inf')
for i in range(N):
    if val[i][ret[i]] <= 0:
        temp_max = max(val[i][1:min(N,K)+1])
    else:
        temp1 = val[i][ret[i]]*(K//ret[i]) + max(val[i][:K-ret[i]*(K//ret[i])+1])
        temp2 = -float('inf')
        if K//ret[i] >= 1:
            temp2 = val[i][ret[i]]*(K//ret[i]-1) + max(val[i][:ret[i]+1])
        temp_max = max(temp1,temp2)
    ans = max(ans,temp_max)

print(ans)