def solve_loop(loop, K):
    S = sum(loop)
    L = len(loop)
    if S < 0:
        acc = 0
        K = min(K, L)
        minL = 1
    elif K//L >= 2:
        acc = S * (K//L - 1)
        K = K - L * (K//L - 1)
        minL = 0
    else:
        acc = 0
        minL = 1

    ans = -10 ** 9 - 1
    for i in range(L):
        #print(loop[i:] + loop[:i], "acc=", acc, "min=", minL, "max=", K)
        tmp = 0
        if minL:
            tmp = loop[i]

        for l in range(minL, K):
            ans = max(tmp, ans)
            tmp += loop[(i+l) % L]
            #print(tmp)
            #print(i, l, tmp)
        ans = max(tmp, ans)

    #print(loop, ans, acc)
    return acc + ans
    if K >= L:
        return max(acc, acc + ans)
    else:
        return ans

def solve():
    N, K = map(int, input().split())
    P = list(map(lambda n: int(n)-1, input().split())) # 0-based index
    C = list(map(int, input().split()))

    flag = [False] * N
    ans = -10**9-1  # worst case is -10**9
    for i in range(N):
        if flag[i]:
            continue
        loop = []
        j = P[i]
        for _ in range(N):
            if flag[j]: # loop end
                break
            flag[j] = True
            loop += [C[j]]
            j = P[j]
        ans = max(ans, solve_loop(loop, K))
    print(ans)

solve() def solve_loop(loop, K):
    S = sum(loop)
    L = len(loop)
    if S < 0:
        acc = 0
        K = min(K, L)
        minL = 1
    elif K//L >= 2:
        acc = S * (K//L - 1)
        K = K - L * (K//L - 1)
        minL = 0
    else:
        acc = 0
        minL = 1

    ans = -10 ** 9 - 1
    for i in range(L):
        #print(loop[i:] + loop[:i], "acc=", acc, "min=", minL, "max=", K)
        tmp = 0
        if minL:
            tmp = loop[i]

        for l in range(minL, K):
            ans = max(tmp, ans)
            tmp += loop[(i+l) % L]
            #print(tmp)
            #print(i, l, tmp)
        ans = max(tmp, ans)

    #print(loop, ans, acc)
    return acc + ans
    if K >= L:
        return max(acc, acc + ans)
    else:
        return ans solve_loop S = sum(loop) S sum(loop) sum loop L = len(loop) L len(loop) len loop if S < 0:
        acc = 0
        K = min(K, L)
        minL = 1
    elif K//L >= 2:
        acc = S * (K//L - 1)
        K = K - L * (K//L - 1)
        minL = 0
    else:
        acc = 0
        minL = 1

     S < 0 S 0 acc = 0 acc 0 K = min(K, L) K min(K, L) min K L minL = 1 minL 1 elif K//L >= 2:
        acc = S * (K//L - 1)
        K = K - L * (K//L - 1)
        minL = 0
     K//L >= 2 K//L K L 2 acc = S * (K//L - 1) acc S * (K//L - 1) S K//L - 1 K//L K L 1 K = K - L * (K//L - 1) K K - L * (K//L - 1) K L * (K//L - 1) L K//L - 1 K//L K L 1 minL = 0 minL 0 acc = 0 acc 0 minL = 1 minL 1 ans = -10 ** 9 - 1 ans -10 ** 9 - 1 -10 ** 9 -10 9 1 for i in range(L):
        #print(loop[i:] + loop[:i], "acc=", acc, "min=", minL, "max=", K)
        tmp = 0
        if minL:
            tmp = loop[i]

        for l in range(minL, K):
            ans = max(tmp, ans)
            tmp += loop[(i+l) % L]
            #print(tmp)
            #print(i, l, tmp)
        ans = max(tmp, ans)

    #print(loop, ans, acc)
     i range(L) range L tmp = 0 tmp 0 if minL:
            tmp = loop[i]

         minL tmp = loop[i] tmp loop[i] loop i for l in range(minL, K):
            ans = max(tmp, ans)
            tmp += loop[(i+l) % L]
            #print(tmp)
            #print(i, l, tmp)
         l range(minL, K) range minL K ans = max(tmp, ans) ans max(tmp, ans) max tmp ans tmp += loop[(i+l) % L] tmp loop[(i+l) % L] loop (i+l) % L i+l i l L ans = max(tmp, ans) ans max(tmp, ans) max tmp ans return acc + ans acc + ans acc ans if K >= L:
        return max(acc, acc + ans)
    else:
        return ans K >= L K L return max(acc, acc + ans) max(acc, acc + ans) max acc acc + ans acc ans return ans ans loop loop K K def solve():
    N, K = map(int, input().split())
    P = list(map(lambda n: int(n)-1, input().split())) # 0-based index
    C = list(map(int, input().split()))

    flag = [False] * N
    ans = -10**9-1  # worst case is -10**9
    for i in range(N):
        if flag[i]:
            continue
        loop = []
        j = P[i]
        for _ in range(N):
            if flag[j]: # loop end
                break
            flag[j] = True
            loop += [C[j]]
            j = P[j]
        ans = max(ans, solve_loop(loop, K))
    print(ans) solve N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda n: int(n)-1, input().split())) P list(map(lambda n: int(n)-1, input().split())) list map(lambda n: int(n)-1, input().split()) map lambda n: int(n)-1 int(n)-1 int(n) int n 1 n 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 flag = [False] * N flag [False] * N [False] False N ans = -10**9-1 ans -10**9-1 -10**9 -10 9 1 for i in range(N):
        if flag[i]:
            continue
        loop = []
        j = P[i]
        for _ in range(N):
            if flag[j]: # loop end
                break
            flag[j] = True
            loop += [C[j]]
            j = P[j]
        ans = max(ans, solve_loop(loop, K))
     i range(N) range N if flag[i]:
            continue
         flag[i] flag i continue loop = [] loop [] j = P[i] j P[i] P i for _ in range(N):
            if flag[j]: # loop end
                break
            flag[j] = True
            loop += [C[j]]
            j = P[j]
         _ range(N) range N if flag[j]: # loop end
                break
             flag[j] flag j break flag[j] = True flag[j] flag j True loop += [C[j]] loop [C[j]] C[j] C j j = P[j] j P[j] P j ans = max(ans, solve_loop(loop, K)) ans max(ans, solve_loop(loop, K)) max ans solve_loop(loop, K) solve_loop loop K print(ans) print ans solve() solve def solve():
    N, K = map(int, input().split())
    P = list(map(lambda n: int(n)-1, input().split())) # 0-based index
    C = list(map(int, input().split()))

    flag = [False] * N
    ans = -10**9-1  # worst case is -10**9
    for i in range(N):
        if flag[i]:
            continue
        loop = []
        j = P[i]
        for _ in range(N):
            if flag[j]: # loop end
                break
            flag[j] = True
            loop += [C[j]]
            j = P[j]
        ans = max(ans, solve_loop(loop, K))
    print(ans) def solve():
    N, K = map(int, input().split())
    P = list(map(lambda n: int(n)-1, input().split())) # 0-based index
    C = list(map(int, input().split()))

    flag = [False] * N
    ans = -10**9-1  # worst case is -10**9
    for i in range(N):
        if flag[i]:
            continue
        loop = []
        j = P[i]
        for _ in range(N):
            if flag[j]: # loop end
                break
            flag[j] = True
            loop += [C[j]]
            j = P[j]
        ans = max(ans, solve_loop(loop, K))
    print(ans) solve def solve_loop(loop, K):
    S = sum(loop)
    L = len(loop)
    if S < 0:
        acc = 0
        K = min(K, L)
        minL = 1
    elif K//L >= 2:
        acc = S * (K//L - 1)
        K = K - L * (K//L - 1)
        minL = 0
    else:
        acc = 0
        minL = 1

    ans = -10 ** 9 - 1
    for i in range(L):
        #print(loop[i:] + loop[:i], "acc=", acc, "min=", minL, "max=", K)
        tmp = 0
        if minL:
            tmp = loop[i]

        for l in range(minL, K):
            ans = max(tmp, ans)
            tmp += loop[(i+l) % L]
            #print(tmp)
            #print(i, l, tmp)
        ans = max(tmp, ans)

    #print(loop, ans, acc)
    return acc + ans
    if K >= L:
        return max(acc, acc + ans)
    else:
        return ans def solve_loop(loop, K):
    S = sum(loop)
    L = len(loop)
    if S < 0:
        acc = 0
        K = min(K, L)
        minL = 1
    elif K//L >= 2:
        acc = S * (K//L - 1)
        K = K - L * (K//L - 1)
        minL = 0
    else:
        acc = 0
        minL = 1

    ans = -10 ** 9 - 1
    for i in range(L):
        #print(loop[i:] + loop[:i], "acc=", acc, "min=", minL, "max=", K)
        tmp = 0
        if minL:
            tmp = loop[i]

        for l in range(minL, K):
            ans = max(tmp, ans)
            tmp += loop[(i+l) % L]
            #print(tmp)
            #print(i, l, tmp)
        ans = max(tmp, ans)

    #print(loop, ans, acc)
    return acc + ans
    if K >= L:
        return max(acc, acc + ans)
    else:
        return ans solve_loop
def solve_loop(loop, K):
    S = sum(loop)
    L = len(loop)
    if S < 0:
        acc = 0
        K = min(K, L)
        minL = 1
    elif K//L >= 2:
        acc = S * (K//L - 1)
        K = K - L * (K//L - 1)
        minL = 0
    else:
        acc = 0
        minL = 1

    ans = -10 ** 9 - 1
    for i in range(L):
        #print(loop[i:] + loop[:i], "acc=", acc, "min=", minL, "max=", K)
        tmp = 0
        if minL:
            tmp = loop[i]

        for l in range(minL, K):
            ans = max(tmp, ans)
            tmp += loop[(i+l) % L]
            #print(tmp)
            #print(i, l, tmp)
        ans = max(tmp, ans)

    #print(loop, ans, acc)
    return acc + ans
    if K >= L:
        return max(acc, acc + ans)
    else:
        return ans

def solve():
    N, K = map(int, input().split())
    P = list(map(lambda n: int(n)-1, input().split())) # 0-based index
    C = list(map(int, input().split()))

    flag = [False] * N
    ans = -10**9-1  # worst case is -10**9
    for i in range(N):
        if flag[i]:
            continue
        loop = []
        j = P[i]
        for _ in range(N):
            if flag[j]: # loop end
                break
            flag[j] = True
            loop += [C[j]]
            j = P[j]
        ans = max(ans, solve_loop(loop, K))
    print(ans)

solve()