from numba import njit
from sys import maxsize


@njit
def solve(n, k, p, c):
    used = [0] * n
    ss = []
    for i in range(n):
        if used[i]:
            continue
        now = i
        s = []
        while not used[now]:
            used[now] = 1
            s.append(c[now])
            now = p[now]
        ss.append(s)

    res = -maxsize
    for s in ss:
        s_len = len(s)
        cumsum = [0]
        # 2周分の累積和
        for i in range(2*s_len):
            cumsum.append(cumsum[-1] + s[i%s_len])

        max_sum = [-maxsize] * s_len
        for i in range(s_len):
            for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

        for i in range(s_len):
            if i > k:
                continue
            v = (k - i) // s_len
            if i == 0 and v == 0:
                continue
            if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
    print(res)


if __name__ == '__main__':
    n, k = map(int, input().split())
    p = list(map(lambda x: int(x)-1, input().split()))
    c = list(map(int, input().split()))
    solve(n, k, p, c) from numba import njit from sys import maxsize def solve(n, k, p, c):
    used = [0] * n
    ss = []
    for i in range(n):
        if used[i]:
            continue
        now = i
        s = []
        while not used[now]:
            used[now] = 1
            s.append(c[now])
            now = p[now]
        ss.append(s)

    res = -maxsize
    for s in ss:
        s_len = len(s)
        cumsum = [0]
        # 2周分の累積和
        for i in range(2*s_len):
            cumsum.append(cumsum[-1] + s[i%s_len])

        max_sum = [-maxsize] * s_len
        for i in range(s_len):
            for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

        for i in range(s_len):
            if i > k:
                continue
            v = (k - i) // s_len
            if i == 0 and v == 0:
                continue
            if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
    print(res) solve used = [0] * n used [0] * n [0] 0 n ss = [] ss [] for i in range(n):
        if used[i]:
            continue
        now = i
        s = []
        while not used[now]:
            used[now] = 1
            s.append(c[now])
            now = p[now]
        ss.append(s)

     i range(n) range n if used[i]:
            continue
         used[i] used i continue now = i now i s = [] s [] while not used[now]:
            used[now] = 1
            s.append(c[now])
            now = p[now]
         not used[now] used[now] used now used[now] = 1 used[now] used now 1 s.append(c[now]) s.append s append c[now] c now now = p[now] now p[now] p now ss.append(s) ss.append ss append s res = -maxsize res -maxsize maxsize for s in ss:
        s_len = len(s)
        cumsum = [0]
        # 2周分の累積和
        for i in range(2*s_len):
            cumsum.append(cumsum[-1] + s[i%s_len])

        max_sum = [-maxsize] * s_len
        for i in range(s_len):
            for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

        for i in range(s_len):
            if i > k:
                continue
            v = (k - i) // s_len
            if i == 0 and v == 0:
                continue
            if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
     s ss s_len = len(s) s_len len(s) len s cumsum = [0] cumsum [0] 0 for i in range(2*s_len):
            cumsum.append(cumsum[-1] + s[i%s_len])

         i range(2*s_len) range 2*s_len 2 s_len cumsum.append(cumsum[-1] + s[i%s_len]) cumsum.append cumsum append cumsum[-1] + s[i%s_len] cumsum[-1] cumsum -1 s[i%s_len] s i%s_len i s_len max_sum = [-maxsize] * s_len max_sum [-maxsize] * s_len [-maxsize] -maxsize maxsize s_len for i in range(s_len):
            for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

         i range(s_len) range s_len for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

         j range(s_len) range s_len max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i]) max_sum[j] max_sum j max(max_sum[j], cumsum[i+j] - cumsum[i]) max max_sum[j] max_sum j cumsum[i+j] - cumsum[i] cumsum[i+j] cumsum i+j i j cumsum[i] cumsum i for i in range(s_len):
            if i > k:
                continue
            v = (k - i) // s_len
            if i == 0 and v == 0:
                continue
            if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
     i range(s_len) range s_len if i > k:
                continue
             i > k i k continue v = (k - i) // s_len v (k - i) // s_len k - i k i s_len if i == 0 and v == 0:
                continue
             i == 0 and v == 0 i == 0 i 0 v == 0 v 0 continue if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
     cumsum[s_len] > 0 cumsum[s_len] cumsum s_len 0 res = max(res, max_sum[i] + cumsum[s_len] * v) res max(res, max_sum[i] + cumsum[s_len] * v) max res max_sum[i] + cumsum[s_len] * v max_sum[i] max_sum i cumsum[s_len] * v cumsum[s_len] cumsum s_len v elif i > 0:
                res = max(res, max_sum[i])
     i > 0 i 0 res = max(res, max_sum[i]) res max(res, max_sum[i]) max res max_sum[i] max_sum i print(res) print res n n k k p p c c @njit solve njit solve if __name__ == '__main__':
    n, k = map(int, input().split())
    p = list(map(lambda x: int(x)-1, input().split()))
    c = list(map(int, input().split()))
    solve(n, k, p, c) __name__ == '__main__' __name__ '__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 solve(n, k, p, c) solve n k p c c = list(map(int, input().split())) list(map(int, input().split())) c n, k = map(int, input().split()) map(int, input().split()) n p = list(map(lambda x: int(x)-1, input().split())) list(map(lambda x: int(x)-1, input().split())) p k = map(int, input().split()) map(int, input().split()) k def solve(n, k, p, c):
    used = [0] * n
    ss = []
    for i in range(n):
        if used[i]:
            continue
        now = i
        s = []
        while not used[now]:
            used[now] = 1
            s.append(c[now])
            now = p[now]
        ss.append(s)

    res = -maxsize
    for s in ss:
        s_len = len(s)
        cumsum = [0]
        # 2周分の累積和
        for i in range(2*s_len):
            cumsum.append(cumsum[-1] + s[i%s_len])

        max_sum = [-maxsize] * s_len
        for i in range(s_len):
            for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

        for i in range(s_len):
            if i > k:
                continue
            v = (k - i) // s_len
            if i == 0 and v == 0:
                continue
            if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
    print(res) def solve(n, k, p, c):
    used = [0] * n
    ss = []
    for i in range(n):
        if used[i]:
            continue
        now = i
        s = []
        while not used[now]:
            used[now] = 1
            s.append(c[now])
            now = p[now]
        ss.append(s)

    res = -maxsize
    for s in ss:
        s_len = len(s)
        cumsum = [0]
        # 2周分の累積和
        for i in range(2*s_len):
            cumsum.append(cumsum[-1] + s[i%s_len])

        max_sum = [-maxsize] * s_len
        for i in range(s_len):
            for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

        for i in range(s_len):
            if i > k:
                continue
            v = (k - i) // s_len
            if i == 0 and v == 0:
                continue
            if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
    print(res) solve
from numba import njit
from sys import maxsize


@njit
def solve(n, k, p, c):
    used = [0] * n
    ss = []
    for i in range(n):
        if used[i]:
            continue
        now = i
        s = []
        while not used[now]:
            used[now] = 1
            s.append(c[now])
            now = p[now]
        ss.append(s)

    res = -maxsize
    for s in ss:
        s_len = len(s)
        cumsum = [0]
        # 2周分の累積和
        for i in range(2*s_len):
            cumsum.append(cumsum[-1] + s[i%s_len])

        max_sum = [-maxsize] * s_len
        for i in range(s_len):
            for j in range(s_len):
                max_sum[j] = max(max_sum[j], cumsum[i+j] - cumsum[i])

        for i in range(s_len):
            if i > k:
                continue
            v = (k - i) // s_len
            if i == 0 and v == 0:
                continue
            if cumsum[s_len] > 0:
                res = max(res, max_sum[i] + cumsum[s_len] * v)
            elif i > 0:
                res = max(res, max_sum[i])
    print(res)


if __name__ == '__main__':
    n, k = map(int, input().split())
    p = list(map(lambda x: int(x)-1, input().split()))
    c = list(map(int, input().split()))
    solve(n, k, p, c)
