import sys
readline = sys.stdin.readline

def solve():
    N, K = map(int, readline().split())
    P = [v - 1 for v in map(int, readline().split())]
    C = list(map(int, readline().split()))


    ans = -10**10

    for i in range(N):
        moves = 1
        cur = P[i]
        score = C[cur]
        ans = max(score, ans)
        visited = {cur: (moves, score)}

        while P[cur] not in visited and moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            visited[cur] = (moves, score)

            ans = max(ans, score)
        
        if moves == K:
            continue
        
        cur = P[cur]
        moves += 1
        score += C[cur]

        T = moves - visited[cur][0]
        D = score - visited[cur][1]

        if D < 0:
            continue
        
        nloops = K - moves
        moves += T * (nloops // T)
        score += D * (nloops // T)

        ans = max(ans, score)

        while moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            ans = max(ans, score)
    
    print(ans)

solve() import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline def solve():
    N, K = map(int, readline().split())
    P = [v - 1 for v in map(int, readline().split())]
    C = list(map(int, readline().split()))


    ans = -10**10

    for i in range(N):
        moves = 1
        cur = P[i]
        score = C[cur]
        ans = max(score, ans)
        visited = {cur: (moves, score)}

        while P[cur] not in visited and moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            visited[cur] = (moves, score)

            ans = max(ans, score)
        
        if moves == K:
            continue
        
        cur = P[cur]
        moves += 1
        score += C[cur]

        T = moves - visited[cur][0]
        D = score - visited[cur][1]

        if D < 0:
            continue
        
        nloops = K - moves
        moves += T * (nloops // T)
        score += D * (nloops // T)

        ans = max(ans, score)

        while moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            ans = max(ans, score)
    
    print(ans) solve N, K = map(int, readline().split()) N map(int, readline().split()) map int readline().split() ().split () readline split K v - 1 for v in map(int, readline().split()) for v in map(int, readline().split()) v map(int, readline().split()) map int readline().split() ().split () readline split for v in map(int, readline().split()) v - 1 v 1 P = [v - 1 for v in map(int, readline().split())] P [v - 1 for v in map(int, readline().split())] C = list(map(int, readline().split())) C list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split ans = -10**10 ans -10**10 -10 10 for i in range(N):
        moves = 1
        cur = P[i]
        score = C[cur]
        ans = max(score, ans)
        visited = {cur: (moves, score)}

        while P[cur] not in visited and moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            visited[cur] = (moves, score)

            ans = max(ans, score)
        
        if moves == K:
            continue
        
        cur = P[cur]
        moves += 1
        score += C[cur]

        T = moves - visited[cur][0]
        D = score - visited[cur][1]

        if D < 0:
            continue
        
        nloops = K - moves
        moves += T * (nloops // T)
        score += D * (nloops // T)

        ans = max(ans, score)

        while moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            ans = max(ans, score)
    
     i range(N) range N moves = 1 moves 1 cur = P[i] cur P[i] P i score = C[cur] score C[cur] C cur ans = max(score, ans) ans max(score, ans) max score ans visited = {cur: (moves, score)} visited {cur: (moves, score)} cur cur (moves, score) moves score while P[cur] not in visited and moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            visited[cur] = (moves, score)

            ans = max(ans, score)
        
         P[cur] not in visited and moves < K P[cur] not in visited P[cur] P cur visited moves < K moves K cur = P[cur] cur P[cur] P cur moves += 1 moves 1 score += C[cur] score C[cur] C cur visited[cur] = (moves, score) visited[cur] visited cur (moves, score) moves score ans = max(ans, score) ans max(ans, score) max ans score if moves == K:
            continue
        
         moves == K moves K continue cur = P[cur] cur P[cur] P cur moves += 1 moves 1 score += C[cur] score C[cur] C cur T = moves - visited[cur][0] T moves - visited[cur][0] moves visited[cur][0] [cur] visited cur 0 D = score - visited[cur][1] D score - visited[cur][1] score visited[cur][1] [cur] visited cur 1 if D < 0:
            continue
        
         D < 0 D 0 continue nloops = K - moves nloops K - moves K moves moves += T * (nloops // T) moves T * (nloops // T) T nloops // T nloops T score += D * (nloops // T) score D * (nloops // T) D nloops // T nloops T ans = max(ans, score) ans max(ans, score) max ans score while moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            ans = max(ans, score)
    
     moves < K moves K cur = P[cur] cur P[cur] P cur moves += 1 moves 1 score += C[cur] score C[cur] C cur ans = max(ans, score) ans max(ans, score) max ans score print(ans) print ans solve() solve def solve():
    N, K = map(int, readline().split())
    P = [v - 1 for v in map(int, readline().split())]
    C = list(map(int, readline().split()))


    ans = -10**10

    for i in range(N):
        moves = 1
        cur = P[i]
        score = C[cur]
        ans = max(score, ans)
        visited = {cur: (moves, score)}

        while P[cur] not in visited and moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            visited[cur] = (moves, score)

            ans = max(ans, score)
        
        if moves == K:
            continue
        
        cur = P[cur]
        moves += 1
        score += C[cur]

        T = moves - visited[cur][0]
        D = score - visited[cur][1]

        if D < 0:
            continue
        
        nloops = K - moves
        moves += T * (nloops // T)
        score += D * (nloops // T)

        ans = max(ans, score)

        while moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            ans = max(ans, score)
    
    print(ans) def solve():
    N, K = map(int, readline().split())
    P = [v - 1 for v in map(int, readline().split())]
    C = list(map(int, readline().split()))


    ans = -10**10

    for i in range(N):
        moves = 1
        cur = P[i]
        score = C[cur]
        ans = max(score, ans)
        visited = {cur: (moves, score)}

        while P[cur] not in visited and moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            visited[cur] = (moves, score)

            ans = max(ans, score)
        
        if moves == K:
            continue
        
        cur = P[cur]
        moves += 1
        score += C[cur]

        T = moves - visited[cur][0]
        D = score - visited[cur][1]

        if D < 0:
            continue
        
        nloops = K - moves
        moves += T * (nloops // T)
        score += D * (nloops // T)

        ans = max(ans, score)

        while moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            ans = max(ans, score)
    
    print(ans) solve readline = sys.stdin.readline sys.stdin.readline readline
import sys
readline = sys.stdin.readline

def solve():
    N, K = map(int, readline().split())
    P = [v - 1 for v in map(int, readline().split())]
    C = list(map(int, readline().split()))


    ans = -10**10

    for i in range(N):
        moves = 1
        cur = P[i]
        score = C[cur]
        ans = max(score, ans)
        visited = {cur: (moves, score)}

        while P[cur] not in visited and moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            visited[cur] = (moves, score)

            ans = max(ans, score)
        
        if moves == K:
            continue
        
        cur = P[cur]
        moves += 1
        score += C[cur]

        T = moves - visited[cur][0]
        D = score - visited[cur][1]

        if D < 0:
            continue
        
        nloops = K - moves
        moves += T * (nloops // T)
        score += D * (nloops // T)

        ans = max(ans, score)

        while moves < K:
            cur = P[cur]
            moves += 1
            score += C[cur]
            ans = max(ans, score)
    
    print(ans)

solve()