from collections import Counter
from heapq import heapify, heappop, heappush
from itertools import accumulate


def solve(n, k, ppp, ccc):
    NINF = -(10 ** 18)
    ans = NINF

    checked = [False] * n
    for s in range(n):
        if checked[s] == True:
            continue
        checked[s] = True
        scores = [ccc[ppp[s]]]
        v = ppp[s]
        while v != s:
            scores.append(ccc[ppp[v]])
            checked[v] = True
            v = ppp[v]

        l = len(scores)
        d, m = divmod(k, l)
        loop = sum(scores)

        if d > 0:
            d -= 1
            m += l

        scores += scores * 2
        scores.insert(0, 0)
        acc = list(accumulate(scores))

        base = max(0, loop * d)

        range_max = [-a for a in acc[1:m + 1]]
        available_max = Counter(range_max)
        heapify(range_max)
        for i in range(l):
            while available_max[range_max[0]] == 0:
                heappop(range_max)
            ans = max(ans, base - range_max[0] - acc[i])
            old = -acc[i + 1]
            new = -acc[i + m + 1]
            available_max[old] -= 1
            available_max[new] += 1
            heappush(range_max, new)

    return ans


n, k = map(int, input().split())
ppp = list(map(int, input().split()))
ppp = [p - 1 for p in ppp]
ccc = list(map(int, input().split()))

print(solve(n, k, ppp, ccc)) from collections import Counter from heapq import heapify, heappop, heappush from itertools import accumulate def solve(n, k, ppp, ccc):
    NINF = -(10 ** 18)
    ans = NINF

    checked = [False] * n
    for s in range(n):
        if checked[s] == True:
            continue
        checked[s] = True
        scores = [ccc[ppp[s]]]
        v = ppp[s]
        while v != s:
            scores.append(ccc[ppp[v]])
            checked[v] = True
            v = ppp[v]

        l = len(scores)
        d, m = divmod(k, l)
        loop = sum(scores)

        if d > 0:
            d -= 1
            m += l

        scores += scores * 2
        scores.insert(0, 0)
        acc = list(accumulate(scores))

        base = max(0, loop * d)

        range_max = [-a for a in acc[1:m + 1]]
        available_max = Counter(range_max)
        heapify(range_max)
        for i in range(l):
            while available_max[range_max[0]] == 0:
                heappop(range_max)
            ans = max(ans, base - range_max[0] - acc[i])
            old = -acc[i + 1]
            new = -acc[i + m + 1]
            available_max[old] -= 1
            available_max[new] += 1
            heappush(range_max, new)

    return ans solve NINF = -(10 ** 18) NINF -(10 ** 18) 10 ** 18 10 18 ans = NINF ans NINF checked = [False] * n checked [False] * n [False] False n for s in range(n):
        if checked[s] == True:
            continue
        checked[s] = True
        scores = [ccc[ppp[s]]]
        v = ppp[s]
        while v != s:
            scores.append(ccc[ppp[v]])
            checked[v] = True
            v = ppp[v]

        l = len(scores)
        d, m = divmod(k, l)
        loop = sum(scores)

        if d > 0:
            d -= 1
            m += l

        scores += scores * 2
        scores.insert(0, 0)
        acc = list(accumulate(scores))

        base = max(0, loop * d)

        range_max = [-a for a in acc[1:m + 1]]
        available_max = Counter(range_max)
        heapify(range_max)
        for i in range(l):
            while available_max[range_max[0]] == 0:
                heappop(range_max)
            ans = max(ans, base - range_max[0] - acc[i])
            old = -acc[i + 1]
            new = -acc[i + m + 1]
            available_max[old] -= 1
            available_max[new] += 1
            heappush(range_max, new)

     s range(n) range n if checked[s] == True:
            continue
         checked[s] == True checked[s] checked s True continue checked[s] = True checked[s] checked s True scores = [ccc[ppp[s]]] scores [ccc[ppp[s]]] ccc[ppp[s]] ccc ppp[s] ppp s v = ppp[s] v ppp[s] ppp s while v != s:
            scores.append(ccc[ppp[v]])
            checked[v] = True
            v = ppp[v]

         v != s v s scores.append(ccc[ppp[v]]) scores.append scores append ccc[ppp[v]] ccc ppp[v] ppp v checked[v] = True checked[v] checked v True v = ppp[v] v ppp[v] ppp v l = len(scores) l len(scores) len scores d, m = divmod(k, l) d divmod(k, l) divmod k l m loop = sum(scores) loop sum(scores) sum scores if d > 0:
            d -= 1
            m += l

         d > 0 d 0 d -= 1 d 1 m += l m l scores += scores * 2 scores scores * 2 scores 2 scores.insert(0, 0) scores.insert scores insert 0 0 acc = list(accumulate(scores)) acc list(accumulate(scores)) list accumulate(scores) accumulate scores base = max(0, loop * d) base max(0, loop * d) max 0 loop * d loop d -a for a in acc[1:m + 1] for a in acc[1:m + 1] a acc[1:m + 1] acc 1:m + 1 1 m + 1 m 1 for a in acc[1:m + 1] -a a range_max = [-a for a in acc[1:m + 1]] range_max [-a for a in acc[1:m + 1]] available_max = Counter(range_max) available_max Counter(range_max) Counter range_max heapify(range_max) heapify range_max for i in range(l):
            while available_max[range_max[0]] == 0:
                heappop(range_max)
            ans = max(ans, base - range_max[0] - acc[i])
            old = -acc[i + 1]
            new = -acc[i + m + 1]
            available_max[old] -= 1
            available_max[new] += 1
            heappush(range_max, new)

     i range(l) range l while available_max[range_max[0]] == 0:
                heappop(range_max)
             available_max[range_max[0]] == 0 available_max[range_max[0]] available_max range_max[0] range_max 0 0 heappop(range_max) heappop range_max ans = max(ans, base - range_max[0] - acc[i]) ans max(ans, base - range_max[0] - acc[i]) max ans base - range_max[0] - acc[i] base - range_max[0] base range_max[0] range_max 0 acc[i] acc i old = -acc[i + 1] old -acc[i + 1] acc[i + 1] acc i + 1 i 1 new = -acc[i + m + 1] new -acc[i + m + 1] acc[i + m + 1] acc i + m + 1 i + m i m 1 available_max[old] -= 1 available_max[old] available_max old 1 available_max[new] += 1 available_max[new] available_max new 1 heappush(range_max, new) heappush range_max new return ans ans n n k k ppp ppp ccc ccc n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k ppp = list(map(int, input().split())) ppp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split p - 1 for p in ppp for p in ppp p ppp for p in ppp p - 1 p 1 ppp = [p - 1 for p in ppp] ppp [p - 1 for p in ppp] ccc = list(map(int, input().split())) ccc list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split print(solve(n, k, ppp, ccc)) print solve(n, k, ppp, ccc) solve n k ppp ccc ppp = list(map(int, input().split())) list(map(int, input().split())) ppp ccc = list(map(int, input().split())) list(map(int, input().split())) ccc k = map(int, input().split()) map(int, input().split()) k n, k = map(int, input().split()) map(int, input().split()) n def solve(n, k, ppp, ccc):
    NINF = -(10 ** 18)
    ans = NINF

    checked = [False] * n
    for s in range(n):
        if checked[s] == True:
            continue
        checked[s] = True
        scores = [ccc[ppp[s]]]
        v = ppp[s]
        while v != s:
            scores.append(ccc[ppp[v]])
            checked[v] = True
            v = ppp[v]

        l = len(scores)
        d, m = divmod(k, l)
        loop = sum(scores)

        if d > 0:
            d -= 1
            m += l

        scores += scores * 2
        scores.insert(0, 0)
        acc = list(accumulate(scores))

        base = max(0, loop * d)

        range_max = [-a for a in acc[1:m + 1]]
        available_max = Counter(range_max)
        heapify(range_max)
        for i in range(l):
            while available_max[range_max[0]] == 0:
                heappop(range_max)
            ans = max(ans, base - range_max[0] - acc[i])
            old = -acc[i + 1]
            new = -acc[i + m + 1]
            available_max[old] -= 1
            available_max[new] += 1
            heappush(range_max, new)

    return ans def solve(n, k, ppp, ccc):
    NINF = -(10 ** 18)
    ans = NINF

    checked = [False] * n
    for s in range(n):
        if checked[s] == True:
            continue
        checked[s] = True
        scores = [ccc[ppp[s]]]
        v = ppp[s]
        while v != s:
            scores.append(ccc[ppp[v]])
            checked[v] = True
            v = ppp[v]

        l = len(scores)
        d, m = divmod(k, l)
        loop = sum(scores)

        if d > 0:
            d -= 1
            m += l

        scores += scores * 2
        scores.insert(0, 0)
        acc = list(accumulate(scores))

        base = max(0, loop * d)

        range_max = [-a for a in acc[1:m + 1]]
        available_max = Counter(range_max)
        heapify(range_max)
        for i in range(l):
            while available_max[range_max[0]] == 0:
                heappop(range_max)
            ans = max(ans, base - range_max[0] - acc[i])
            old = -acc[i + 1]
            new = -acc[i + m + 1]
            available_max[old] -= 1
            available_max[new] += 1
            heappush(range_max, new)

    return ans solve ppp = [p - 1 for p in ppp] [p - 1 for p in ppp] ppp
from collections import Counter
from heapq import heapify, heappop, heappush
from itertools import accumulate


def solve(n, k, ppp, ccc):
    NINF = -(10 ** 18)
    ans = NINF

    checked = [False] * n
    for s in range(n):
        if checked[s] == True:
            continue
        checked[s] = True
        scores = [ccc[ppp[s]]]
        v = ppp[s]
        while v != s:
            scores.append(ccc[ppp[v]])
            checked[v] = True
            v = ppp[v]

        l = len(scores)
        d, m = divmod(k, l)
        loop = sum(scores)

        if d > 0:
            d -= 1
            m += l

        scores += scores * 2
        scores.insert(0, 0)
        acc = list(accumulate(scores))

        base = max(0, loop * d)

        range_max = [-a for a in acc[1:m + 1]]
        available_max = Counter(range_max)
        heapify(range_max)
        for i in range(l):
            while available_max[range_max[0]] == 0:
                heappop(range_max)
            ans = max(ans, base - range_max[0] - acc[i])
            old = -acc[i + 1]
            new = -acc[i + m + 1]
            available_max[old] -= 1
            available_max[new] += 1
            heappush(range_max, new)

    return ans


n, k = map(int, input().split())
ppp = list(map(int, input().split()))
ppp = [p - 1 for p in ppp]
ccc = list(map(int, input().split()))

print(solve(n, k, ppp, ccc))
