def main():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x)-1, input().split()))
    C = list(map(int, input().split()))
    loop = [0] * N
    loopsum = [0] * N
    for i in range(N):
        if loop[i] > 0:
            continue
        cnt = 0
        cur = i
        visited = set()
        score = 0
        while cur not in visited:
            visited.add(cur)
            cnt += 1
            cur = P[cur]
            score += C[cur]
        for v in visited:
            loop[v] = cnt
            loopsum[v] = score
    ans = max(C)
    for i in range(N):
        if loopsum[i] >= 0 and loop[i] <= K:
            score = loopsum[i] * (K // loop[i] - 1)
            limit = K % loop[i] + loop[i]
        else:
            score = 0
            limit = min(loop[i], K)
        cur = i
        for _ in range(limit):
            cur = P[cur]
            score += C[cur]
            ans = max(ans, score)
    print(ans)


if __name__ == "__main__":
    main() def main():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x)-1, input().split()))
    C = list(map(int, input().split()))
    loop = [0] * N
    loopsum = [0] * N
    for i in range(N):
        if loop[i] > 0:
            continue
        cnt = 0
        cur = i
        visited = set()
        score = 0
        while cur not in visited:
            visited.add(cur)
            cnt += 1
            cur = P[cur]
            score += C[cur]
        for v in visited:
            loop[v] = cnt
            loopsum[v] = score
    ans = max(C)
    for i in range(N):
        if loopsum[i] >= 0 and loop[i] <= K:
            score = loopsum[i] * (K // loop[i] - 1)
            limit = K % loop[i] + loop[i]
        else:
            score = 0
            limit = min(loop[i], K)
        cur = i
        for _ in range(limit):
            cur = P[cur]
            score += C[cur]
            ans = max(ans, score)
    print(ans) main 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 loop = [0] * N loop [0] * N [0] 0 N loopsum = [0] * N loopsum [0] * N [0] 0 N for i in range(N):
        if loop[i] > 0:
            continue
        cnt = 0
        cur = i
        visited = set()
        score = 0
        while cur not in visited:
            visited.add(cur)
            cnt += 1
            cur = P[cur]
            score += C[cur]
        for v in visited:
            loop[v] = cnt
            loopsum[v] = score
     i range(N) range N if loop[i] > 0:
            continue
         loop[i] > 0 loop[i] loop i 0 continue cnt = 0 cnt 0 cur = i cur i visited = set() visited set() set score = 0 score 0 while cur not in visited:
            visited.add(cur)
            cnt += 1
            cur = P[cur]
            score += C[cur]
         cur not in visited cur visited visited.add(cur) visited.add visited add cur cnt += 1 cnt 1 cur = P[cur] cur P[cur] P cur score += C[cur] score C[cur] C cur for v in visited:
            loop[v] = cnt
            loopsum[v] = score
     v visited loop[v] = cnt loop[v] loop v cnt loopsum[v] = score loopsum[v] loopsum v score ans = max(C) ans max(C) max C for i in range(N):
        if loopsum[i] >= 0 and loop[i] <= K:
            score = loopsum[i] * (K // loop[i] - 1)
            limit = K % loop[i] + loop[i]
        else:
            score = 0
            limit = min(loop[i], K)
        cur = i
        for _ in range(limit):
            cur = P[cur]
            score += C[cur]
            ans = max(ans, score)
     i range(N) range N if loopsum[i] >= 0 and loop[i] <= K:
            score = loopsum[i] * (K // loop[i] - 1)
            limit = K % loop[i] + loop[i]
        else:
            score = 0
            limit = min(loop[i], K)
         loopsum[i] >= 0 and loop[i] <= K loopsum[i] >= 0 loopsum[i] loopsum i 0 loop[i] <= K loop[i] loop i K score = loopsum[i] * (K // loop[i] - 1) score loopsum[i] * (K // loop[i] - 1) loopsum[i] loopsum i K // loop[i] - 1 K // loop[i] K loop[i] loop i 1 limit = K % loop[i] + loop[i] limit K % loop[i] + loop[i] K % loop[i] K loop[i] loop i loop[i] loop i score = 0 score 0 limit = min(loop[i], K) limit min(loop[i], K) min loop[i] loop i K cur = i cur i for _ in range(limit):
            cur = P[cur]
            score += C[cur]
            ans = max(ans, score)
     _ range(limit) range limit cur = P[cur] cur P[cur] P cur score += C[cur] score C[cur] C cur ans = max(ans, score) ans max(ans, score) max ans score print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x)-1, input().split()))
    C = list(map(int, input().split()))
    loop = [0] * N
    loopsum = [0] * N
    for i in range(N):
        if loop[i] > 0:
            continue
        cnt = 0
        cur = i
        visited = set()
        score = 0
        while cur not in visited:
            visited.add(cur)
            cnt += 1
            cur = P[cur]
            score += C[cur]
        for v in visited:
            loop[v] = cnt
            loopsum[v] = score
    ans = max(C)
    for i in range(N):
        if loopsum[i] >= 0 and loop[i] <= K:
            score = loopsum[i] * (K // loop[i] - 1)
            limit = K % loop[i] + loop[i]
        else:
            score = 0
            limit = min(loop[i], K)
        cur = i
        for _ in range(limit):
            cur = P[cur]
            score += C[cur]
            ans = max(ans, score)
    print(ans) def main():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x)-1, input().split()))
    C = list(map(int, input().split()))
    loop = [0] * N
    loopsum = [0] * N
    for i in range(N):
        if loop[i] > 0:
            continue
        cnt = 0
        cur = i
        visited = set()
        score = 0
        while cur not in visited:
            visited.add(cur)
            cnt += 1
            cur = P[cur]
            score += C[cur]
        for v in visited:
            loop[v] = cnt
            loopsum[v] = score
    ans = max(C)
    for i in range(N):
        if loopsum[i] >= 0 and loop[i] <= K:
            score = loopsum[i] * (K // loop[i] - 1)
            limit = K % loop[i] + loop[i]
        else:
            score = 0
            limit = min(loop[i], K)
        cur = i
        for _ in range(limit):
            cur = P[cur]
            score += C[cur]
            ans = max(ans, score)
    print(ans) main
def main():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x)-1, input().split()))
    C = list(map(int, input().split()))
    loop = [0] * N
    loopsum = [0] * N
    for i in range(N):
        if loop[i] > 0:
            continue
        cnt = 0
        cur = i
        visited = set()
        score = 0
        while cur not in visited:
            visited.add(cur)
            cnt += 1
            cur = P[cur]
            score += C[cur]
        for v in visited:
            loop[v] = cnt
            loopsum[v] = score
    ans = max(C)
    for i in range(N):
        if loopsum[i] >= 0 and loop[i] <= K:
            score = loopsum[i] * (K // loop[i] - 1)
            limit = K % loop[i] + loop[i]
        else:
            score = 0
            limit = min(loop[i], K)
        cur = i
        for _ in range(limit):
            cur = P[cur]
            score += C[cur]
            ans = max(ans, score)
    print(ans)


if __name__ == "__main__":
    main()
