import sys
from heapq import heappush, heappop, heapify
import math
from math import gcd
import itertools as it
from collections import deque 

def inp():
    return int(input())
def inpl():
    return list(map(int, input().split()))

INF = 1001001001

# ---------------------------------------



n, k = inpl()
p = [0] + inpl()
c = [0] + inpl()

ans = - INF

for i in range(1, n + 1):
    cycle_cnt = 0
    cycle_sum = 0
    v = i

    while True:
        v = p[v]
        cycle_cnt += 1
        cycle_sum += c[v]
        if v == i: break
    
    cnt = 0
    path = 0

    while True:
        v = p[v]
        cnt += 1
        path += c[v]

        if cnt > k: break

        num = (k - cnt) // cycle_cnt
        score = path + max(0, cycle_sum) * num
        ans = max(ans, score)

        if v == i: break

print(ans) import sys sys from heapq import heappush, heappop, heapify import math math from math import gcd import itertools as it itertools from collections import deque def inp():
    return int(input()) inp return int(input()) int(input()) int input() input def inpl():
    return list(map(int, input().split())) inpl return list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split INF = 1001001001 INF 1001001001 n, k = inpl() n inpl() inpl k p = [0] + inpl() p [0] + inpl() [0] 0 inpl() inpl c = [0] + inpl() c [0] + inpl() [0] 0 inpl() inpl ans = - INF ans - INF INF for i in range(1, n + 1):
    cycle_cnt = 0
    cycle_sum = 0
    v = i

    while True:
        v = p[v]
        cycle_cnt += 1
        cycle_sum += c[v]
        if v == i: break
    
    cnt = 0
    path = 0

    while True:
        v = p[v]
        cnt += 1
        path += c[v]

        if cnt > k: break

        num = (k - cnt) // cycle_cnt
        score = path + max(0, cycle_sum) * num
        ans = max(ans, score)

        if v == i: break i range(1, n + 1) range 1 n + 1 n 1 cycle_cnt = 0 cycle_cnt 0 cycle_sum = 0 cycle_sum 0 v = i v i while True:
        v = p[v]
        cycle_cnt += 1
        cycle_sum += c[v]
        if v == i: break
    
     True v = p[v] v p[v] p v cycle_cnt += 1 cycle_cnt 1 cycle_sum += c[v] cycle_sum c[v] c v if v == i: break
    
     v == i v i cnt = 0 cnt 0 path = 0 path 0 while True:
        v = p[v]
        cnt += 1
        path += c[v]

        if cnt > k: break

        num = (k - cnt) // cycle_cnt
        score = path + max(0, cycle_sum) * num
        ans = max(ans, score)

        if v == i: break True v = p[v] v p[v] p v cnt += 1 cnt 1 path += c[v] path c[v] c v if cnt > k: break

         cnt > k cnt k num = (k - cnt) // cycle_cnt num (k - cnt) // cycle_cnt k - cnt k cnt cycle_cnt score = path + max(0, cycle_sum) * num score path + max(0, cycle_sum) * num path max(0, cycle_sum) * num max(0, cycle_sum) max 0 cycle_sum num ans = max(ans, score) ans max(ans, score) max ans score if v ==  v == i v i print(ans) print ans v = p[v] p[v] v cnt += 1 1 cnt num = (k - cnt) // cycle_cnt (k - cnt) // cycle_cnt num path = 0 0 path p = [0] + inpl() [0] + inpl() p ans = - INF - INF ans c = [0] + inpl() [0] + inpl() c path += c[v] c[v] path ans = max(ans, score) max(ans, score) ans n, k = inpl() inpl() n v = i i v cycle_sum = 0 0 cycle_sum cnt = 0 0 cnt v = p[v] p[v] v cycle_sum += c[v] c[v] cycle_sum cycle_cnt += 1 1 cycle_cnt k = inpl() inpl() k score = path + max(0, cycle_sum) * num path + max(0, cycle_sum) * num score INF = 1001001001 1001001001 INF cycle_cnt = 0 0 cycle_cnt def inpl():
    return list(map(int, input().split())) def inpl():
    return list(map(int, input().split())) inpl def inp():
    return int(input()) def inp():
    return int(input()) inp
import sys
from heapq import heappush, heappop, heapify
import math
from math import gcd
import itertools as it
from collections import deque 

def inp():
    return int(input())
def inpl():
    return list(map(int, input().split()))

INF = 1001001001

# ---------------------------------------



n, k = inpl()
p = [0] + inpl()
c = [0] + inpl()

ans = - INF

for i in range(1, n + 1):
    cycle_cnt = 0
    cycle_sum = 0
    v = i

    while True:
        v = p[v]
        cycle_cnt += 1
        cycle_sum += c[v]
        if v == i: break
    
    cnt = 0
    path = 0

    while True:
        v = p[v]
        cnt += 1
        path += c[v]

        if cnt > k: break

        num = (k - cnt) // cycle_cnt
        score = path + max(0, cycle_sum) * num
        ans = max(ans, score)

        if v == i: break

print(ans)
