from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
        self.size = [1] * n
        # self.group_num = n

    def root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

    def get_size(self, x):
        r = self.root(x)
        return self.size[r]

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        r1 = self.root(x)
        r2 = self.root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        # self.group_num -= 1

n, k = LI()
P = LI()
C = LI()
U = UnionFind(n)
for i in range(n):
    U.union(P[i] - 1, i)

D = defaultdict(int)
for i in range(n):
    D[U.root(i)] += C[i]

ans = -INF
k -= 1
for j in range(n):
    now = P[j] - 1
    r = D[U.root(now)]
    m = U.get_size(now)
    ret = C[now]
    ans = max(ans, ret)
    if k // m and r >= 0:
        ret += k // m * r
        ans = max(ans, ret)
        for l in range(k % m):
            now = P[now] - 1
            ret += C[now]
            ans = max(ans, ret)
    ret = C[now]
    for l in range(min(m, k)):
        now = P[now] - 1
        ret += C[now]
        ans = max(ans, ret)

print(ans)




 from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math math import bisect bisect import random random from itertools import permutations, accumulate, combinations, product import sys sys from pprint import pprint from copy import deepcopy import string string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce from pprint import pprint sys.setrecursionlimit(2147483647) sys.setrecursionlimit sys setrecursionlimit 2147483647 INF = 10 ** 20 INF 10 ** 20 10 20 def LI(): return list(map(int, sys.stdin.readline().split())) LI def I(): return int(sys.stdin.readline()) I def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() LS def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') S I() for i in range(n) for i in range(n) i range(n) range n for i in range(n) I() I def IR(n): return [I() for i in range(n)] IR n n LI() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LI() LI def LIR(n): return [LI() for i in range(n)] LIR n n S() for i in range(n) for i in range(n) i range(n) range n for i in range(n) S() S def SR(n): return [S() for i in range(n)] SR n n LS() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LS() LS def LSR(n): return [LS() for i in range(n)] LSR n n list(S()) for i in range(n) for i in range(n) i range(n) range n for i in range(n) list(S()) list S() S def SRL(n): return [list(S()) for i in range(n)] SRL n n [int(j) for j in list(S())] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [int(j) for j in list(S())] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] MSRL n n mod = 1000000007 mod 1000000007 class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
        self.size = [1] * n
        # self.group_num = n

    def root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

    def get_size(self, x):
        r = self.root(x)
        return self.size[r]

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        r1 = self.root(x)
        r2 = self.root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        # self.group_num -= 1 UnionFind def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
        self.size = [1] * n
        # self.group_num = n

     __init__ self.table = [-1] * n self.table self table [-1] * n [-1] -1 n self.size = [1] * n self.size self size [1] * n [1] 1 n self self n n def root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

     root if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

     self.table[x] < 0 self.table[x] self.table self table x 0 return x x self.table[x] = self.root(self.table[x]) self.table[x] self.table self table x self.root(self.table[x]) self.root self root self.table[x] self.table self table x return self.table[x] self.table[x] self.table self table x self self x x def get_size(self, x):
        r = self.root(x)
        return self.size[r]

     get_size r = self.root(x) r self.root(x) self.root self root x return self.size[r] self.size[r] self.size self size r self self x x def is_same(self, x, y):
        return self.root(x) == self.root(y)

     is_same return self.root(x) == self.root(y) self.root(x) == self.root(y) self.root(x) self.root self root x self.root(y) self.root self root y self self x x y y def union(self, x, y):
        r1 = self.root(x)
        r2 = self.root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        # self.group_num -= 1 union r1 = self.root(x) r1 self.root(x) self.root self root x r2 = self.root(y) r2 self.root(y) self.root self root y if r1 == r2:
            return
        # ランクの取得
         r1 == r2 r1 r2 return d1 = self.table[r1] d1 self.table[r1] self.table self table r1 d2 = self.table[r2] d2 self.table[r2] self.table self table r2 if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        # self.group_num -= 1 d1 <= d2 d1 d2 self.table[r2] = r1 self.table[r2] self.table self table r2 r1 self.size[r1] += self.size[r2] self.size[r1] self.size self size r1 self.size[r2] self.size self size r2 if d1 == d2:
                self.table[r1] -= 1
         d1 == d2 d1 d2 self.table[r1] -= 1 self.table[r1] self.table self table r1 1 self.table[r1] = r2 self.table[r1] self.table self table r1 r2 self.size[r2] += self.size[r1] self.size[r2] self.size self size r2 self.size[r1] self.size self size r1 self self x x y y n, k = LI() n LI() LI k P = LI() P LI() LI C = LI() C LI() LI U = UnionFind(n) U UnionFind(n) UnionFind n for i in range(n):
    U.union(P[i] - 1, i) i range(n) range n U.union(P[i] - 1, i) U.union U union P[i] - 1 P[i] P i 1 i D = defaultdict(int) D defaultdict(int) defaultdict int for i in range(n):
    D[U.root(i)] += C[i] i range(n) range n D[U.root(i)] += C[i] D[U.root(i)] D U.root(i) U.root U root i C[i] C i ans = -INF ans -INF INF k -= 1 k 1 for j in range(n):
    now = P[j] - 1
    r = D[U.root(now)]
    m = U.get_size(now)
    ret = C[now]
    ans = max(ans, ret)
    if k // m and r >= 0:
        ret += k // m * r
        ans = max(ans, ret)
        for l in range(k % m):
            now = P[now] - 1
            ret += C[now]
            ans = max(ans, ret)
    ret = C[now]
    for l in range(min(m, k)):
        now = P[now] - 1
        ret += C[now]
        ans = max(ans, ret) j range(n) range n now = P[j] - 1 now P[j] - 1 P[j] P j 1 r = D[U.root(now)] r D[U.root(now)] D U.root(now) U.root U root now m = U.get_size(now) m U.get_size(now) U.get_size U get_size now ret = C[now] ret C[now] C now ans = max(ans, ret) ans max(ans, ret) max ans ret if k // m and r >= 0:
        ret += k // m * r
        ans = max(ans, ret)
        for l in range(k % m):
            now = P[now] - 1
            ret += C[now]
            ans = max(ans, ret)
     k // m and r >= 0 k // m k m r >= 0 r 0 ret += k // m * r ret k // m * r k // m k m r ans = max(ans, ret) ans max(ans, ret) max ans ret for l in range(k % m):
            now = P[now] - 1
            ret += C[now]
            ans = max(ans, ret)
     l range(k % m) range k % m k m now = P[now] - 1 now P[now] - 1 P[now] P now 1 ret += C[now] ret C[now] C now ans = max(ans, ret) ans max(ans, ret) max ans ret ret = C[now] ret C[now] C now for l in range(min(m, k)):
        now = P[now] - 1
        ret += C[now]
        ans = max(ans, ret) l range(min(m, k)) range min(m, k) min m k now = P[now] - 1 now P[now] - 1 P[now] P now 1 ret += C[now] ret C[now] C now ans = max(ans, ret) ans max(ans, ret) max ans ret print(ans) print ans def LIR(n): return [LI() for i in range(n)] def LIR(n): return [LI() for i in range(n)] LIR k -= 1 1 k def I(): return int(sys.stdin.readline()) def I(): return int(sys.stdin.readline()) I def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') S class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
        self.size = [1] * n
        # self.group_num = n

    def root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

    def get_size(self, x):
        r = self.root(x)
        return self.size[r]

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        r1 = self.root(x)
        r2 = self.root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        # self.group_num -= 1 class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
        self.size = [1] * n
        # self.group_num = n

    def root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

    def get_size(self, x):
        r = self.root(x)
        return self.size[r]

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        r1 = self.root(x)
        r2 = self.root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        # self.group_num -= 1 UnionFind now = P[now] - 1 P[now] - 1 now ans = max(ans, ret) max(ans, ret) ans def LSR(n): return [LS() for i in range(n)] def LSR(n): return [LS() for i in range(n)] LSR D = defaultdict(int) defaultdict(int) D m = U.get_size(now) U.get_size(now) m P = LI() LI() P def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] MSRL U = UnionFind(n) UnionFind(n) U mod = 1000000007 1000000007 mod ret += C[now] C[now] ret def SRL(n): return [list(S()) for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] SRL ret = C[now] C[now] ret def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() LS ans = max(ans, ret) max(ans, ret) ans ret += C[now] C[now] ret now = P[now] - 1 P[now] - 1 now ans = -INF -INF ans ans = max(ans, ret) max(ans, ret) ans ans = max(ans, ret) max(ans, ret) ans def SR(n): return [S() for i in range(n)] def SR(n): return [S() for i in range(n)] SR ret += k // m * r k // m * r ret def LI(): return list(map(int, sys.stdin.readline().split())) def LI(): return list(map(int, sys.stdin.readline().split())) LI n, k = LI() LI() n now = P[j] - 1 P[j] - 1 now INF = 10 ** 20 10 ** 20 INF C = LI() LI() C r = D[U.root(now)] D[U.root(now)] r ret = C[now] C[now] ret def IR(n): return [I() for i in range(n)] def IR(n): return [I() for i in range(n)] IR k = LI() LI() k
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
        self.size = [1] * n
        # self.group_num = n

    def root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

    def get_size(self, x):
        r = self.root(x)
        return self.size[r]

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        r1 = self.root(x)
        r2 = self.root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        # self.group_num -= 1

n, k = LI()
P = LI()
C = LI()
U = UnionFind(n)
for i in range(n):
    U.union(P[i] - 1, i)

D = defaultdict(int)
for i in range(n):
    D[U.root(i)] += C[i]

ans = -INF
k -= 1
for j in range(n):
    now = P[j] - 1
    r = D[U.root(now)]
    m = U.get_size(now)
    ret = C[now]
    ans = max(ans, ret)
    if k // m and r >= 0:
        ret += k // m * r
        ans = max(ans, ret)
        for l in range(k % m):
            now = P[now] - 1
            ret += C[now]
            ans = max(ans, ret)
    ret = C[now]
    for l in range(min(m, k)):
        now = P[now] - 1
        ret += C[now]
        ans = max(ans, ret)

print(ans)





