def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N,K = LMIIS()
    P = list(map(lambda x: int(x)-1,input().split()))
    C = LMIIS()
    score_sets = []
    used = [False]*N
    tmp = P[0]
    for i in range(N):
        if not used[i]:
            tmp = i
            score_set = []
            while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
            score_sets.append(score_set)
        
    ans = -INF
    for score_set in score_sets:
        # 合計が最大になる区間を探す
        len_set = len(score_set)
        sum_score_set_real = sum(score_set)
        sum_score_set = max(0,sum_score_set_real)


        
        for i in range(len_set):
            cum_sum_1 = 0
            for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

        for i in range(1,len_set-1):
            cum_sum_2 = sum_score_set_real
            for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
            for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
    print(ans)

    
main() def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N,K = LMIIS()
    P = list(map(lambda x: int(x)-1,input().split()))
    C = LMIIS()
    score_sets = []
    used = [False]*N
    tmp = P[0]
    for i in range(N):
        if not used[i]:
            tmp = i
            score_set = []
            while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
            score_sets.append(score_set)
        
    ans = -INF
    for score_set in score_sets:
        # 合計が最大になる区間を探す
        len_set = len(score_set)
        sum_score_set_real = sum(score_set)
        sum_score_set = max(0,sum_score_set_real)


        
        for i in range(len_set):
            cum_sum_1 = 0
            for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

        for i in range(1,len_set-1):
            cum_sum_2 = sum_score_set_real
            for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
            for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
    print(ans)

     main from sys import setrecursionlimit, stdin, stderr from os import environ from collections import defaultdict, deque, Counter from math import ceil, floor from itertools import accumulate, combinations, combinations_with_replacement setrecursionlimit(10**6) setrecursionlimit 10**6 10 6 dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0 dbg (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0 'TERM_PROGRAM' in environ 'TERM_PROGRAM' environ lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n')) stderr.write("\033[92m{}\033[0m".format(str(something)+'\n')) stderr.write stderr write "\033[92m{}\033[0m".format(str(something)+'\n') "\033[92m{}\033[0m".format "\033[92m{}\033[0m" format str(something)+'\n' str(something) str something '\n' *something lambda *x: 0 0 *x input = lambda: stdin.readline().rstrip() input lambda: stdin.readline().rstrip() stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip LMIIS = lambda: list(map(int,input().split())) LMIIS lambda: list(map(int,input().split())) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split II = lambda: int(input()) II lambda: int(input()) int(input()) int input() input P = 10**9+7 P 10**9+7 10**9 10 9 7 INF = 10**18+10 INF 10**18+10 10**18 10 18 10 N,K = LMIIS() N LMIIS() LMIIS 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 = LMIIS() C LMIIS() LMIIS score_sets = [] score_sets [] used = [False]*N used [False]*N [False] False N tmp = P[0] tmp P[0] P 0 for i in range(N):
        if not used[i]:
            tmp = i
            score_set = []
            while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
            score_sets.append(score_set)
        
     i range(N) range N if not used[i]:
            tmp = i
            score_set = []
            while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
            score_sets.append(score_set)
        
     not used[i] used[i] used i tmp = i tmp i score_set = [] score_set [] while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
             not used[tmp] used[tmp] used tmp used[tmp] = True used[tmp] used tmp True tmp = P[tmp] tmp P[tmp] P tmp score_set.append(C[tmp]) score_set.append score_set append C[tmp] C tmp score_sets.append(score_set) score_sets.append score_sets append score_set ans = -INF ans -INF INF for score_set in score_sets:
        # 合計が最大になる区間を探す
        len_set = len(score_set)
        sum_score_set_real = sum(score_set)
        sum_score_set = max(0,sum_score_set_real)


        
        for i in range(len_set):
            cum_sum_1 = 0
            for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

        for i in range(1,len_set-1):
            cum_sum_2 = sum_score_set_real
            for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
            for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
     score_set score_sets len_set = len(score_set) len_set len(score_set) len score_set sum_score_set_real = sum(score_set) sum_score_set_real sum(score_set) sum score_set sum_score_set = max(0,sum_score_set_real) sum_score_set max(0,sum_score_set_real) max 0 sum_score_set_real for i in range(len_set):
            cum_sum_1 = 0
            for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

         i range(len_set) range len_set cum_sum_1 = 0 cum_sum_1 0 for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

         j range(i,min(len_set, i+K)) range i min(len_set, i+K) min len_set i+K i K cum_sum_1 += score_set[j] cum_sum_1 score_set[j] score_set j num_move = j-i+1 num_move j-i+1 j-i j i 1 tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set tmp_max cum_sum_1 + (K - num_move) // len_set * sum_score_set cum_sum_1 (K - num_move) // len_set * sum_score_set (K - num_move) // len_set K - num_move K num_move len_set sum_score_set ans = max(ans, tmp_max) ans max(ans, tmp_max) max ans tmp_max for i in range(1,len_set-1):
            cum_sum_2 = sum_score_set_real
            for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
            for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
     i range(1,len_set-1) range 1 len_set-1 len_set 1 cum_sum_2 = sum_score_set_real cum_sum_2 sum_score_set_real for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
             j range(i,min(len_set-1-(K-i),len_set-1)) range i min(len_set-1-(K-i),len_set-1) min len_set-1-(K-i) len_set-1 len_set 1 K-i K i len_set-1 len_set 1 cum_sum_2 -= score_set[j] cum_sum_2 score_set[j] score_set j for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
     j range(max(i,len_set-1-(K-i)),len_set-1) range max(i,len_set-1-(K-i)) max i len_set-1-(K-i) len_set-1 len_set 1 K-i K i len_set-1 len_set 1 cum_sum_2 -= score_set[j] cum_sum_2 score_set[j] score_set j num_move = len_set - (j-i+1) num_move len_set - (j-i+1) len_set j-i+1 j-i j i 1 tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set tmp_max cum_sum_2 + (K - num_move) // len_set * sum_score_set cum_sum_2 (K - num_move) // len_set * sum_score_set (K - num_move) // len_set K - num_move K num_move len_set sum_score_set ans = max(ans,tmp_max) ans max(ans,tmp_max) max ans tmp_max print(ans) print ans main() main def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N,K = LMIIS()
    P = list(map(lambda x: int(x)-1,input().split()))
    C = LMIIS()
    score_sets = []
    used = [False]*N
    tmp = P[0]
    for i in range(N):
        if not used[i]:
            tmp = i
            score_set = []
            while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
            score_sets.append(score_set)
        
    ans = -INF
    for score_set in score_sets:
        # 合計が最大になる区間を探す
        len_set = len(score_set)
        sum_score_set_real = sum(score_set)
        sum_score_set = max(0,sum_score_set_real)


        
        for i in range(len_set):
            cum_sum_1 = 0
            for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

        for i in range(1,len_set-1):
            cum_sum_2 = sum_score_set_real
            for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
            for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
    print(ans)

     def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N,K = LMIIS()
    P = list(map(lambda x: int(x)-1,input().split()))
    C = LMIIS()
    score_sets = []
    used = [False]*N
    tmp = P[0]
    for i in range(N):
        if not used[i]:
            tmp = i
            score_set = []
            while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
            score_sets.append(score_set)
        
    ans = -INF
    for score_set in score_sets:
        # 合計が最大になる区間を探す
        len_set = len(score_set)
        sum_score_set_real = sum(score_set)
        sum_score_set = max(0,sum_score_set_real)


        
        for i in range(len_set):
            cum_sum_1 = 0
            for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

        for i in range(1,len_set-1):
            cum_sum_2 = sum_score_set_real
            for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
            for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
    print(ans)

     main
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10
    N,K = LMIIS()
    P = list(map(lambda x: int(x)-1,input().split()))
    C = LMIIS()
    score_sets = []
    used = [False]*N
    tmp = P[0]
    for i in range(N):
        if not used[i]:
            tmp = i
            score_set = []
            while not used[tmp]:
                used[tmp] = True
                tmp = P[tmp]
                score_set.append(C[tmp])
            score_sets.append(score_set)
        
    ans = -INF
    for score_set in score_sets:
        # 合計が最大になる区間を探す
        len_set = len(score_set)
        sum_score_set_real = sum(score_set)
        sum_score_set = max(0,sum_score_set_real)


        
        for i in range(len_set):
            cum_sum_1 = 0
            for j in range(i,min(len_set, i+K)):
                cum_sum_1 += score_set[j]
                num_move = j-i+1
                tmp_max = cum_sum_1 + (K - num_move) // len_set * sum_score_set
                ans = max(ans, tmp_max)

        for i in range(1,len_set-1):
            cum_sum_2 = sum_score_set_real
            for j in range(i,min(len_set-1-(K-i),len_set-1)):
                cum_sum_2 -= score_set[j]
            for j in range(max(i,len_set-1-(K-i)),len_set-1):
                cum_sum_2 -= score_set[j]
                num_move = len_set - (j-i+1)
                tmp_max = cum_sum_2 + (K - num_move) // len_set * sum_score_set
                ans = max(ans,tmp_max)






        
                
    print(ans)

    
main()
