import math


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, K = read_ints()
    P = [p-1 for p in read_ints()]
    C = read_ints()

    max_score = -math.inf
    for i in range(N):
        cycle_score = C[i]
        scores = [0, C[i]]
        j = P[i]
        k = K
        while k > 1 and i != j:
            cycle_score += C[j]
            scores.append(cycle_score)
            j = P[j]
            k -= 1
        cycle_size = len(scores)-1
        cycle_count = K//cycle_size
        max_score = max(max_score, max(scores[1:]))
        if cycle_count != 0:
            max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:]))
    return max_score


if __name__ == '__main__':
    print(solve()) import math math def read_int():
    return int(input().strip()) read_int return int(input().strip()) int(input().strip()) int input().strip() ().strip () input strip def read_ints():
    return list(map(int, input().strip().split(' '))) read_ints return list(map(int, input().strip().split(' '))) list(map(int, input().strip().split(' '))) list map(int, input().strip().split(' ')) map int input().strip().split(' ') ().strip().split ().strip() ().strip () input strip split ' ' def solve():
    N, K = read_ints()
    P = [p-1 for p in read_ints()]
    C = read_ints()

    max_score = -math.inf
    for i in range(N):
        cycle_score = C[i]
        scores = [0, C[i]]
        j = P[i]
        k = K
        while k > 1 and i != j:
            cycle_score += C[j]
            scores.append(cycle_score)
            j = P[j]
            k -= 1
        cycle_size = len(scores)-1
        cycle_count = K//cycle_size
        max_score = max(max_score, max(scores[1:]))
        if cycle_count != 0:
            max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:]))
    return max_score solve N, K = read_ints() N read_ints() read_ints K p-1 for p in read_ints() for p in read_ints() p read_ints() read_ints for p in read_ints() p-1 p 1 P = [p-1 for p in read_ints()] P [p-1 for p in read_ints()] C = read_ints() C read_ints() read_ints max_score = -math.inf max_score -math.inf math.inf math inf for i in range(N):
        cycle_score = C[i]
        scores = [0, C[i]]
        j = P[i]
        k = K
        while k > 1 and i != j:
            cycle_score += C[j]
            scores.append(cycle_score)
            j = P[j]
            k -= 1
        cycle_size = len(scores)-1
        cycle_count = K//cycle_size
        max_score = max(max_score, max(scores[1:]))
        if cycle_count != 0:
            max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:]))
     i range(N) range N cycle_score = C[i] cycle_score C[i] C i scores = [0, C[i]] scores [0, C[i]] 0 C[i] C i j = P[i] j P[i] P i k = K k K while k > 1 and i != j:
            cycle_score += C[j]
            scores.append(cycle_score)
            j = P[j]
            k -= 1
         k > 1 and i != j k > 1 k 1 i != j i j cycle_score += C[j] cycle_score C[j] C j scores.append(cycle_score) scores.append scores append cycle_score j = P[j] j P[j] P j k -= 1 k 1 cycle_size = len(scores)-1 cycle_size len(scores)-1 len(scores) len scores 1 cycle_count = K//cycle_size cycle_count K//cycle_size K cycle_size max_score = max(max_score, max(scores[1:])) max_score max(max_score, max(scores[1:])) max max_score max(scores[1:]) max scores[1:] scores 1: 1 if cycle_count != 0:
            max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:]))
     cycle_count != 0 cycle_count 0 max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:])) max_score max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:])) max max_score cycle_score*cycle_count+max(scores[:K%cycle_size+1]) cycle_score*cycle_count cycle_score cycle_count max(scores[:K%cycle_size+1]) max scores[:K%cycle_size+1] scores :K%cycle_size+1 K%cycle_size+1 K%cycle_size K cycle_size 1 cycle_score*(cycle_count-1)+max(scores[1:]) cycle_score*(cycle_count-1) cycle_score cycle_count-1 cycle_count 1 max(scores[1:]) max scores[1:] scores 1: 1 return max_score max_score if __name__ == '__main__':
    print(solve()) __name__ == '__main__' __name__ '__main__' print(solve()) print solve() solve def read_int():
    return int(input().strip()) def read_int():
    return int(input().strip()) read_int def solve():
    N, K = read_ints()
    P = [p-1 for p in read_ints()]
    C = read_ints()

    max_score = -math.inf
    for i in range(N):
        cycle_score = C[i]
        scores = [0, C[i]]
        j = P[i]
        k = K
        while k > 1 and i != j:
            cycle_score += C[j]
            scores.append(cycle_score)
            j = P[j]
            k -= 1
        cycle_size = len(scores)-1
        cycle_count = K//cycle_size
        max_score = max(max_score, max(scores[1:]))
        if cycle_count != 0:
            max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:]))
    return max_score def solve():
    N, K = read_ints()
    P = [p-1 for p in read_ints()]
    C = read_ints()

    max_score = -math.inf
    for i in range(N):
        cycle_score = C[i]
        scores = [0, C[i]]
        j = P[i]
        k = K
        while k > 1 and i != j:
            cycle_score += C[j]
            scores.append(cycle_score)
            j = P[j]
            k -= 1
        cycle_size = len(scores)-1
        cycle_count = K//cycle_size
        max_score = max(max_score, max(scores[1:]))
        if cycle_count != 0:
            max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:]))
    return max_score solve def read_ints():
    return list(map(int, input().strip().split(' '))) def read_ints():
    return list(map(int, input().strip().split(' '))) read_ints
import math


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, K = read_ints()
    P = [p-1 for p in read_ints()]
    C = read_ints()

    max_score = -math.inf
    for i in range(N):
        cycle_score = C[i]
        scores = [0, C[i]]
        j = P[i]
        k = K
        while k > 1 and i != j:
            cycle_score += C[j]
            scores.append(cycle_score)
            j = P[j]
            k -= 1
        cycle_size = len(scores)-1
        cycle_count = K//cycle_size
        max_score = max(max_score, max(scores[1:]))
        if cycle_count != 0:
            max_score = max(max_score,
                            cycle_score*cycle_count+max(scores[:K%cycle_size+1]),
                            cycle_score*(cycle_count-1)+max(scores[1:]))
    return max_score


if __name__ == '__main__':
    print(solve())
