# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
INF = 10**10
def main(N, K, P, C):
    ans = max(C)
    for i in range(N):
        used = [False] * N
        now = 0
        mx = -INF
        cnt = 0
        while True:
            if used[i] or cnt >= K:
                break
            used[i] = True
            i = P[i] - 1
            now += C[i]
            mx = max(mx, now)
            cnt += 1
        tmp = max(mx, now)
        if now > 0:
            cycle, mod = divmod(K, cnt)
            if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
            t = now * cycle
            tmp = max(tmp, t)
            for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
        ans = max(ans, tmp)
    print(ans)

if __name__ == '__main__':
    input = sys.stdin.readline
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    main(N, K, P, C) import sys sys INF = 10**10 INF 10**10 10 10 def main(N, K, P, C):
    ans = max(C)
    for i in range(N):
        used = [False] * N
        now = 0
        mx = -INF
        cnt = 0
        while True:
            if used[i] or cnt >= K:
                break
            used[i] = True
            i = P[i] - 1
            now += C[i]
            mx = max(mx, now)
            cnt += 1
        tmp = max(mx, now)
        if now > 0:
            cycle, mod = divmod(K, cnt)
            if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
            t = now * cycle
            tmp = max(tmp, t)
            for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
        ans = max(ans, tmp)
    print(ans) main ans = max(C) ans max(C) max C for i in range(N):
        used = [False] * N
        now = 0
        mx = -INF
        cnt = 0
        while True:
            if used[i] or cnt >= K:
                break
            used[i] = True
            i = P[i] - 1
            now += C[i]
            mx = max(mx, now)
            cnt += 1
        tmp = max(mx, now)
        if now > 0:
            cycle, mod = divmod(K, cnt)
            if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
            t = now * cycle
            tmp = max(tmp, t)
            for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
        ans = max(ans, tmp)
     i range(N) range N used = [False] * N used [False] * N [False] False N now = 0 now 0 mx = -INF mx -INF INF cnt = 0 cnt 0 while True:
            if used[i] or cnt >= K:
                break
            used[i] = True
            i = P[i] - 1
            now += C[i]
            mx = max(mx, now)
            cnt += 1
         True if used[i] or cnt >= K:
                break
             used[i] or cnt >= K used[i] used i cnt >= K cnt K break used[i] = True used[i] used i True i = P[i] - 1 i P[i] - 1 P[i] P i 1 now += C[i] now C[i] C i mx = max(mx, now) mx max(mx, now) max mx now cnt += 1 cnt 1 tmp = max(mx, now) tmp max(mx, now) max mx now if now > 0:
            cycle, mod = divmod(K, cnt)
            if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
            t = now * cycle
            tmp = max(tmp, t)
            for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
         now > 0 now 0 cycle, mod = divmod(K, cnt) cycle divmod(K, cnt) divmod K cnt mod if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
             cycle > 1 cycle 1 tmp = max(tmp, now * (cycle - 1) + mx) tmp max(tmp, now * (cycle - 1) + mx) max tmp now * (cycle - 1) + mx now * (cycle - 1) now cycle - 1 cycle 1 mx t = now * cycle t now * cycle now cycle tmp = max(tmp, t) tmp max(tmp, t) max tmp t for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
         j range(mod) range mod i = P[i] - 1 i P[i] - 1 P[i] P i 1 t += C[i] t C[i] C i tmp = max(tmp, t) tmp max(tmp, t) max tmp t ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans N N K K P P C C if __name__ == '__main__':
    input = sys.stdin.readline
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    main(N, K, P, C) __name__ == '__main__' __name__ '__main__' input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K *P, = map(int, input().split()) *P P map(int, input().split()) map int input().split() ().split () input split *C, = map(int, input().split()) *C C map(int, input().split()) map int input().split() ().split () input split main(N, K, P, C) main N K P C K = map(int, input().split()) map(int, input().split()) K input = sys.stdin.readline sys.stdin.readline input N, K = map(int, input().split()) map(int, input().split()) N def main(N, K, P, C):
    ans = max(C)
    for i in range(N):
        used = [False] * N
        now = 0
        mx = -INF
        cnt = 0
        while True:
            if used[i] or cnt >= K:
                break
            used[i] = True
            i = P[i] - 1
            now += C[i]
            mx = max(mx, now)
            cnt += 1
        tmp = max(mx, now)
        if now > 0:
            cycle, mod = divmod(K, cnt)
            if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
            t = now * cycle
            tmp = max(tmp, t)
            for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
        ans = max(ans, tmp)
    print(ans) def main(N, K, P, C):
    ans = max(C)
    for i in range(N):
        used = [False] * N
        now = 0
        mx = -INF
        cnt = 0
        while True:
            if used[i] or cnt >= K:
                break
            used[i] = True
            i = P[i] - 1
            now += C[i]
            mx = max(mx, now)
            cnt += 1
        tmp = max(mx, now)
        if now > 0:
            cycle, mod = divmod(K, cnt)
            if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
            t = now * cycle
            tmp = max(tmp, t)
            for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
        ans = max(ans, tmp)
    print(ans) main INF = 10**10 10**10 INF
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
INF = 10**10
def main(N, K, P, C):
    ans = max(C)
    for i in range(N):
        used = [False] * N
        now = 0
        mx = -INF
        cnt = 0
        while True:
            if used[i] or cnt >= K:
                break
            used[i] = True
            i = P[i] - 1
            now += C[i]
            mx = max(mx, now)
            cnt += 1
        tmp = max(mx, now)
        if now > 0:
            cycle, mod = divmod(K, cnt)
            if cycle > 1:
                tmp = max(tmp, now * (cycle - 1) + mx)
            t = now * cycle
            tmp = max(tmp, t)
            for j in range(mod):
                i = P[i] - 1
                t += C[i]
                tmp = max(tmp, t)
        ans = max(ans, tmp)
    print(ans)

if __name__ == '__main__':
    input = sys.stdin.readline
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    main(N, K, P, C)
