import sys
input = sys.stdin.buffer.readline

n,k = map(int,input().split())
p = list(map(int,input().split()))

for i in range(n):
    p[i] -= 1

c = list(map(int,input().split()))
used = [False]*n

edge = []

for i in range(n):
    if used[i]:
        continue
    now = i
    edge.append([])

    while True:
        if used[now]:
            break
        used[now] = True
        edge[-1].append(c[now])
        now = p[now]

res = -10**9
for i in range(len(edge)):
    s = sum(edge[i])
    m = len(edge[i])

    for j in range(m):
        tmp = 0
        ctr = 0
        for h in range(j+1,m):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
        for h in range(j+1):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
print(res) import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline 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 for i in range(n):
    p[i] -= 1 i range(n) range n p[i] -= 1 p[i] p i 1 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 = [False]*n used [False]*n [False] False n edge = [] edge [] for i in range(n):
    if used[i]:
        continue
    now = i
    edge.append([])

    while True:
        if used[now]:
            break
        used[now] = True
        edge[-1].append(c[now])
        now = p[now] i range(n) range n if used[i]:
        continue
     used[i] used i continue now = i now i edge.append([]) edge.append edge append [] while True:
        if used[now]:
            break
        used[now] = True
        edge[-1].append(c[now])
        now = p[now] True if used[now]:
            break
         used[now] used now break used[now] = True used[now] used now True edge[-1].append(c[now]) [-1].append [-1] edge -1 append c[now] c now now = p[now] now p[now] p now res = -10**9 res -10**9 -10 9 for i in range(len(edge)):
    s = sum(edge[i])
    m = len(edge[i])

    for j in range(m):
        tmp = 0
        ctr = 0
        for h in range(j+1,m):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
        for h in range(j+1):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s) i range(len(edge)) range len(edge) len edge s = sum(edge[i]) s sum(edge[i]) sum edge[i] edge i m = len(edge[i]) m len(edge[i]) len edge[i] edge i for j in range(m):
        tmp = 0
        ctr = 0
        for h in range(j+1,m):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
        for h in range(j+1):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s) j range(m) range m tmp = 0 tmp 0 ctr = 0 ctr 0 for h in range(j+1,m):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
         h range(j+1,m) range j+1 j 1 m ctr += 1 ctr 1 tmp += edge[i][h] tmp edge[i][h] [i] edge i h if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
         ctr <= k ctr k res = max(res,tmp,tmp+((k-ctr)//m)*s) res max(res,tmp,tmp+((k-ctr)//m)*s) max res tmp tmp+((k-ctr)//m)*s tmp ((k-ctr)//m)*s (k-ctr)//m k-ctr k ctr m s for h in range(j+1):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s) h range(j+1) range j+1 j 1 ctr += 1 ctr 1 tmp += edge[i][h] tmp edge[i][h] [i] edge i h if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s) ctr <= k ctr k res = max(res,tmp,tmp+((k-ctr)//m)*s) res max(res,tmp,tmp+((k-ctr)//m)*s) max res tmp tmp+((k-ctr)//m)*s tmp ((k-ctr)//m)*s (k-ctr)//m k-ctr k ctr m s print(res) print res ctr += 1 1 ctr res = max(res,tmp,tmp+((k-ctr)//m)*s) max(res,tmp,tmp+((k-ctr)//m)*s) res used = [False]*n [False]*n used tmp += edge[i][h] edge[i][h] tmp ctr = 0 0 ctr tmp = 0 0 tmp s = sum(edge[i]) sum(edge[i]) s ctr += 1 1 ctr res = max(res,tmp,tmp+((k-ctr)//m)*s) max(res,tmp,tmp+((k-ctr)//m)*s) res input = sys.stdin.buffer.readline sys.stdin.buffer.readline input k = map(int,input().split()) map(int,input().split()) k p = list(map(int,input().split())) list(map(int,input().split())) p res = -10**9 -10**9 res tmp += edge[i][h] edge[i][h] tmp n,k = map(int,input().split()) map(int,input().split()) n c = list(map(int,input().split())) list(map(int,input().split())) c edge = [] [] edge m = len(edge[i]) len(edge[i]) m now = i i now now = p[now] p[now] now
import sys
input = sys.stdin.buffer.readline

n,k = map(int,input().split())
p = list(map(int,input().split()))

for i in range(n):
    p[i] -= 1

c = list(map(int,input().split()))
used = [False]*n

edge = []

for i in range(n):
    if used[i]:
        continue
    now = i
    edge.append([])

    while True:
        if used[now]:
            break
        used[now] = True
        edge[-1].append(c[now])
        now = p[now]

res = -10**9
for i in range(len(edge)):
    s = sum(edge[i])
    m = len(edge[i])

    for j in range(m):
        tmp = 0
        ctr = 0
        for h in range(j+1,m):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
        for h in range(j+1):
            ctr += 1
            tmp += edge[i][h]
            if ctr <= k:
                res = max(res,tmp,tmp+((k-ctr)//m)*s)
print(res)