import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))


def dfs(i):
    if 0 <= visited[i]:
        return
    visited[i] = idx
    cycle.append(i)
    global csum
    csum += C[i]
    dfs(P[i])


visited = [-1]*n
idx = 0
cycles = []
for i in range(n):
    if 0 <= visited[i]:
        continue
    cycle = []
    csum = 0
    dfs(i)
    cycles.append((cycle, csum))
    idx += 1


INF = 10**18
ans = -INF
for i in range(n):
    cycle, csum = cycles[visited[i]]
    cmem = len(cycle)

    if 0 < csum:
        a, b = divmod(k, cmem)
        scoreA = csum*(a-1)
        b += cmem
        score = 0
        scoreB = 0
        for _ in range(b):
            i = P[i]
            score += C[i]
            if scoreB < score:
                scoreB = score
        X = scoreA+scoreB
    else:
        b = min(k, cmem)
        score = 0
        INF = 10**18
        X = -INF
        for _ in range(b):
            i = P[i]
            score += C[i]
            if X < score:
                X = score

    if ans < X:
        ans = X
print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 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 def dfs(i):
    if 0 <= visited[i]:
        return
    visited[i] = idx
    cycle.append(i)
    global csum
    csum += C[i]
    dfs(P[i]) dfs if 0 <= visited[i]:
        return
     0 <= visited[i] 0 visited[i] visited i return visited[i] = idx visited[i] visited i idx cycle.append(i) cycle.append cycle append i global csum csum += C[i] csum C[i] C i dfs(P[i]) dfs P[i] P i i i visited = [-1]*n visited [-1]*n [-1] -1 n idx = 0 idx 0 cycles = [] cycles [] for i in range(n):
    if 0 <= visited[i]:
        continue
    cycle = []
    csum = 0
    dfs(i)
    cycles.append((cycle, csum))
    idx += 1 i range(n) range n if 0 <= visited[i]:
        continue
     0 <= visited[i] 0 visited[i] visited i continue cycle = [] cycle [] csum = 0 csum 0 dfs(i) dfs i cycles.append((cycle, csum)) cycles.append cycles append (cycle, csum) cycle csum idx += 1 idx 1 INF = 10**18 INF 10**18 10 18 ans = -INF ans -INF INF for i in range(n):
    cycle, csum = cycles[visited[i]]
    cmem = len(cycle)

    if 0 < csum:
        a, b = divmod(k, cmem)
        scoreA = csum*(a-1)
        b += cmem
        score = 0
        scoreB = 0
        for _ in range(b):
            i = P[i]
            score += C[i]
            if scoreB < score:
                scoreB = score
        X = scoreA+scoreB
    else:
        b = min(k, cmem)
        score = 0
        INF = 10**18
        X = -INF
        for _ in range(b):
            i = P[i]
            score += C[i]
            if X < score:
                X = score

    if ans < X:
        ans = X i range(n) range n cycle, csum = cycles[visited[i]] cycle cycles[visited[i]] cycles visited[i] visited i csum cmem = len(cycle) cmem len(cycle) len cycle if 0 < csum:
        a, b = divmod(k, cmem)
        scoreA = csum*(a-1)
        b += cmem
        score = 0
        scoreB = 0
        for _ in range(b):
            i = P[i]
            score += C[i]
            if scoreB < score:
                scoreB = score
        X = scoreA+scoreB
    else:
        b = min(k, cmem)
        score = 0
        INF = 10**18
        X = -INF
        for _ in range(b):
            i = P[i]
            score += C[i]
            if X < score:
                X = score

     0 < csum 0 csum a, b = divmod(k, cmem) a divmod(k, cmem) divmod k cmem b scoreA = csum*(a-1) scoreA csum*(a-1) csum a-1 a 1 b += cmem b cmem score = 0 score 0 scoreB = 0 scoreB 0 for _ in range(b):
            i = P[i]
            score += C[i]
            if scoreB < score:
                scoreB = score
         _ range(b) range b i = P[i] i P[i] P i score += C[i] score C[i] C i if scoreB < score:
                scoreB = score
         scoreB < score scoreB score scoreB = score scoreB score X = scoreA+scoreB X scoreA+scoreB scoreA scoreB b = min(k, cmem) b min(k, cmem) min k cmem score = 0 score 0 INF = 10**18 INF 10**18 10 18 X = -INF X -INF INF for _ in range(b):
            i = P[i]
            score += C[i]
            if X < score:
                X = score

     _ range(b) range b i = P[i] i P[i] P i score += C[i] score C[i] C i if X < score:
                X = score

     X < score X score X = score X score if ans < X:
        ans = X ans < X ans X ans = X ans X print(ans) print ans a, b = divmod(k, cmem) divmod(k, cmem) a P = list(map(lambda x: int(x)-1, input().split())) list(map(lambda x: int(x)-1, input().split())) P X = scoreA+scoreB scoreA+scoreB X k = map(int, input().split()) map(int, input().split()) k X = -INF -INF X csum = cycles[visited[i]] cycles[visited[i]] csum INF = 10**18 10**18 INF scoreB = 0 0 scoreB cycle = [] [] cycle cmem = len(cycle) len(cycle) cmem score += C[i] C[i] score cycle, csum = cycles[visited[i]] cycles[visited[i]] cycle i = P[i] P[i] i visited = [-1]*n [-1]*n visited ans = X X ans C = list(map(int, input().split())) list(map(int, input().split())) C b = min(k, cmem) min(k, cmem) b X = score score X idx = 0 0 idx cycles = [] [] cycles scoreA = csum*(a-1) csum*(a-1) scoreA input = sys.stdin.readline sys.stdin.readline input b = divmod(k, cmem) divmod(k, cmem) b ans = -INF -INF ans csum = 0 0 csum score = 0 0 score idx += 1 1 idx score += C[i] C[i] score def dfs(i):
    if 0 <= visited[i]:
        return
    visited[i] = idx
    cycle.append(i)
    global csum
    csum += C[i]
    dfs(P[i]) def dfs(i):
    if 0 <= visited[i]:
        return
    visited[i] = idx
    cycle.append(i)
    global csum
    csum += C[i]
    dfs(P[i]) dfs b += cmem cmem b INF = 10**18 10**18 INF scoreB = score score scoreB n, k = map(int, input().split()) map(int, input().split()) n i = P[i] P[i] i score = 0 0 score
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))


def dfs(i):
    if 0 <= visited[i]:
        return
    visited[i] = idx
    cycle.append(i)
    global csum
    csum += C[i]
    dfs(P[i])


visited = [-1]*n
idx = 0
cycles = []
for i in range(n):
    if 0 <= visited[i]:
        continue
    cycle = []
    csum = 0
    dfs(i)
    cycles.append((cycle, csum))
    idx += 1


INF = 10**18
ans = -INF
for i in range(n):
    cycle, csum = cycles[visited[i]]
    cmem = len(cycle)

    if 0 < csum:
        a, b = divmod(k, cmem)
        scoreA = csum*(a-1)
        b += cmem
        score = 0
        scoreB = 0
        for _ in range(b):
            i = P[i]
            score += C[i]
            if scoreB < score:
                scoreB = score
        X = scoreA+scoreB
    else:
        b = min(k, cmem)
        score = 0
        INF = 10**18
        X = -INF
        for _ in range(b):
            i = P[i]
            score += C[i]
            if X < score:
                X = score

    if ans < X:
        ans = X
print(ans)
