# Date [ 2020-08-15 21:53:58 ]
# Problem [ d.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product, accumulate
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 6 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    n, k = read_ints()
    p = read_int_list()
    c = read_int_list()
    loop_list = []
    visit = [False] * n

    # 閉路を探してloop_listに格納
    for i in range(n):
        if visit[i]:
            continue

        visit[i] = True
        now = i
        loop_tmp = []

        while True:
            now = p[now] - 1
            visit[now] = True
            loop_tmp.append(c[now])
            if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
        loop_list.append(loop_tmp)

    #print(loop_list)
    ans = -10 ** 18

    #loopごとに全部試す
    for loop in loop_list:
        length = len(loop)
        score = sum(loop)
        if k > length:
            if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
        else:
            ans = max(ans, search_max_score(loop, k, length))
    
    print(ans)


def search_max_score(loop, rest, length):
    if rest == 0:
        ans = 0
    elif rest == 1:
        ans = max(loop)
    else:
        ans = -10**18
        loop += loop
        for i in range(length):
            tmp = list(accumulate(loop[i:i + rest]))
            #print(tmp)
            ans = max(ans, max(tmp))
    
    return ans

if __name__ ==  '__main__':
    Main() import sys sys from decimal import Decimal import math math from itertools import combinations, product, accumulate import bisect bisect from collections import Counter, deque, defaultdict MOD = 10 ** 6 + 7 MOD 10 ** 6 + 7 10 ** 6 10 6 7 INF = 10 ** 9 INF 10 ** 9 10 9 PI = 3.14159265358979323846 PI 3.14159265358979323846 def read_str():      return sys.stdin.readline().strip() read_str def read_int():      return int(sys.stdin.readline().strip()) read_int def read_ints():     return map(int, sys.stdin.readline().strip().split()) read_ints def read_str_list(): return list(sys.stdin.readline().strip().split()) read_str_list def read_int_list(): return list(map(int, sys.stdin.readline().strip().split())) read_int_list def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b) lcm a: int a b: int b def Main():
    n, k = read_ints()
    p = read_int_list()
    c = read_int_list()
    loop_list = []
    visit = [False] * n

    # 閉路を探してloop_listに格納
    for i in range(n):
        if visit[i]:
            continue

        visit[i] = True
        now = i
        loop_tmp = []

        while True:
            now = p[now] - 1
            visit[now] = True
            loop_tmp.append(c[now])
            if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
        loop_list.append(loop_tmp)

    #print(loop_list)
    ans = -10 ** 18

    #loopごとに全部試す
    for loop in loop_list:
        length = len(loop)
        score = sum(loop)
        if k > length:
            if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
        else:
            ans = max(ans, search_max_score(loop, k, length))
    
    print(ans) Main n, k = read_ints() n read_ints() read_ints k p = read_int_list() p read_int_list() read_int_list c = read_int_list() c read_int_list() read_int_list loop_list = [] loop_list [] visit = [False] * n visit [False] * n [False] False n for i in range(n):
        if visit[i]:
            continue

        visit[i] = True
        now = i
        loop_tmp = []

        while True:
            now = p[now] - 1
            visit[now] = True
            loop_tmp.append(c[now])
            if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
        loop_list.append(loop_tmp)

    #print(loop_list)
     i range(n) range n if visit[i]:
            continue

         visit[i] visit i continue visit[i] = True visit[i] visit i True now = i now i loop_tmp = [] loop_tmp [] while True:
            now = p[now] - 1
            visit[now] = True
            loop_tmp.append(c[now])
            if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
         True now = p[now] - 1 now p[now] - 1 p[now] p now 1 visit[now] = True visit[now] visit now True loop_tmp.append(c[now]) loop_tmp.append loop_tmp append c[now] c now if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
         visit[p[now] - 1] visit p[now] - 1 p[now] p now 1 loop_tmp.append(c[p[now] - 1]) loop_tmp.append loop_tmp append c[p[now] - 1] c p[now] - 1 p[now] p now 1 break loop_list.append(loop_tmp) loop_list.append loop_list append loop_tmp ans = -10 ** 18 ans -10 ** 18 -10 18 for loop in loop_list:
        length = len(loop)
        score = sum(loop)
        if k > length:
            if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
        else:
            ans = max(ans, search_max_score(loop, k, length))
    
     loop loop_list length = len(loop) length len(loop) len loop score = sum(loop) score sum(loop) sum loop if k > length:
            if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
        else:
            ans = max(ans, search_max_score(loop, k, length))
    
     k > length k length if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
         score > 0 score 0 ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length)) ans max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length)) max ans (k // length) * score + search_max_score(loop, k % length, length) (k // length) * score k // length k length score search_max_score(loop, k % length, length) search_max_score loop k % length k length length (k // length - 1) * score + search_max_score(loop, length, length) (k // length - 1) * score k // length - 1 k // length k length 1 score search_max_score(loop, length, length) search_max_score loop length length ans = max(ans, search_max_score(loop, length, length)) ans max(ans, search_max_score(loop, length, length)) max ans search_max_score(loop, length, length) search_max_score loop length length ans = max(ans, search_max_score(loop, k, length)) ans max(ans, search_max_score(loop, k, length)) max ans search_max_score(loop, k, length) search_max_score loop k length print(ans) print ans def search_max_score(loop, rest, length):
    if rest == 0:
        ans = 0
    elif rest == 1:
        ans = max(loop)
    else:
        ans = -10**18
        loop += loop
        for i in range(length):
            tmp = list(accumulate(loop[i:i + rest]))
            #print(tmp)
            ans = max(ans, max(tmp))
    
    return ans search_max_score if rest == 0:
        ans = 0
    elif rest == 1:
        ans = max(loop)
    else:
        ans = -10**18
        loop += loop
        for i in range(length):
            tmp = list(accumulate(loop[i:i + rest]))
            #print(tmp)
            ans = max(ans, max(tmp))
    
     rest == 0 rest 0 ans = 0 ans 0 elif rest == 1:
        ans = max(loop)
     rest == 1 rest 1 ans = max(loop) ans max(loop) max loop ans = -10**18 ans -10**18 -10 18 loop += loop loop loop for i in range(length):
            tmp = list(accumulate(loop[i:i + rest]))
            #print(tmp)
            ans = max(ans, max(tmp))
    
     i range(length) range length tmp = list(accumulate(loop[i:i + rest])) tmp list(accumulate(loop[i:i + rest])) list accumulate(loop[i:i + rest]) accumulate loop[i:i + rest] loop i:i + rest i i + rest i rest ans = max(ans, max(tmp)) ans max(ans, max(tmp)) max ans max(tmp) max tmp return ans ans loop loop rest rest length length if __name__ ==  '__main__':
    Main() __name__ ==  '__main__' __name__ '__main__' Main() Main INF = 10 ** 9 10 ** 9 INF PI = 3.14159265358979323846 3.14159265358979323846 PI def read_str_list(): return list(sys.stdin.readline().strip().split()) def read_str_list(): return list(sys.stdin.readline().strip().split()) read_str_list def read_str():      return sys.stdin.readline().strip() def read_str():      return sys.stdin.readline().strip() read_str def search_max_score(loop, rest, length):
    if rest == 0:
        ans = 0
    elif rest == 1:
        ans = max(loop)
    else:
        ans = -10**18
        loop += loop
        for i in range(length):
            tmp = list(accumulate(loop[i:i + rest]))
            #print(tmp)
            ans = max(ans, max(tmp))
    
    return ans def search_max_score(loop, rest, length):
    if rest == 0:
        ans = 0
    elif rest == 1:
        ans = max(loop)
    else:
        ans = -10**18
        loop += loop
        for i in range(length):
            tmp = list(accumulate(loop[i:i + rest]))
            #print(tmp)
            ans = max(ans, max(tmp))
    
    return ans search_max_score def read_int():      return int(sys.stdin.readline().strip()) def read_int():      return int(sys.stdin.readline().strip()) read_int MOD = 10 ** 6 + 7 10 ** 6 + 7 MOD def read_int_list(): return list(map(int, sys.stdin.readline().strip().split())) def read_int_list(): return list(map(int, sys.stdin.readline().strip().split())) read_int_list def Main():
    n, k = read_ints()
    p = read_int_list()
    c = read_int_list()
    loop_list = []
    visit = [False] * n

    # 閉路を探してloop_listに格納
    for i in range(n):
        if visit[i]:
            continue

        visit[i] = True
        now = i
        loop_tmp = []

        while True:
            now = p[now] - 1
            visit[now] = True
            loop_tmp.append(c[now])
            if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
        loop_list.append(loop_tmp)

    #print(loop_list)
    ans = -10 ** 18

    #loopごとに全部試す
    for loop in loop_list:
        length = len(loop)
        score = sum(loop)
        if k > length:
            if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
        else:
            ans = max(ans, search_max_score(loop, k, length))
    
    print(ans) def Main():
    n, k = read_ints()
    p = read_int_list()
    c = read_int_list()
    loop_list = []
    visit = [False] * n

    # 閉路を探してloop_listに格納
    for i in range(n):
        if visit[i]:
            continue

        visit[i] = True
        now = i
        loop_tmp = []

        while True:
            now = p[now] - 1
            visit[now] = True
            loop_tmp.append(c[now])
            if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
        loop_list.append(loop_tmp)

    #print(loop_list)
    ans = -10 ** 18

    #loopごとに全部試す
    for loop in loop_list:
        length = len(loop)
        score = sum(loop)
        if k > length:
            if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
        else:
            ans = max(ans, search_max_score(loop, k, length))
    
    print(ans) Main def read_ints():     return map(int, sys.stdin.readline().strip().split()) def read_ints():     return map(int, sys.stdin.readline().strip().split()) read_ints def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b) def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b) lcm
# Date [ 2020-08-15 21:53:58 ]
# Problem [ d.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product, accumulate
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 6 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    n, k = read_ints()
    p = read_int_list()
    c = read_int_list()
    loop_list = []
    visit = [False] * n

    # 閉路を探してloop_listに格納
    for i in range(n):
        if visit[i]:
            continue

        visit[i] = True
        now = i
        loop_tmp = []

        while True:
            now = p[now] - 1
            visit[now] = True
            loop_tmp.append(c[now])
            if visit[p[now] - 1]:
                loop_tmp.append(c[p[now] - 1])
                break
        loop_list.append(loop_tmp)

    #print(loop_list)
    ans = -10 ** 18

    #loopごとに全部試す
    for loop in loop_list:
        length = len(loop)
        score = sum(loop)
        if k > length:
            if score > 0:
                ans = max(ans, (k // length) * score + search_max_score(loop, k % length, length), (k // length - 1) * score + search_max_score(loop, length, length))
            else:
                ans = max(ans, search_max_score(loop, length, length))
        else:
            ans = max(ans, search_max_score(loop, k, length))
    
    print(ans)


def search_max_score(loop, rest, length):
    if rest == 0:
        ans = 0
    elif rest == 1:
        ans = max(loop)
    else:
        ans = -10**18
        loop += loop
        for i in range(length):
            tmp = list(accumulate(loop[i:i + rest]))
            #print(tmp)
            ans = max(ans, max(tmp))
    
    return ans

if __name__ ==  '__main__':
    Main()