import sys

class SWAG:
    def __init__(self):
        self.fold_l = []
        self.r = []
        self.fold_r = ~(1 << 60)
    def push(self, a):
        if self.fold_r < a:
            self.fold_r = a
        self.r.append(a)
    def pop(self):
        if not self.fold_l:
            self.r.reverse()
            self.fold_l = self.r
            self.r = []
            self.fold_r = ~(1 << 60)
            fold_l = self.fold_l
            for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
        self.fold_l.pop()
    def get(self):
        if not self.fold_l:
            return self.fold_r
        elif not self.r:
            return self.fold_l[-1]
        else:
            return max(self.fold_l[-1], self.fold_r)

n, k = map(int, input().split())
p = [*map(int, input().split())]
c = [*map(int, input().split())]
for i in range(n):
    p[i] -= 1

if max(c) < 0:
    print(max(c))
    sys.exit()

ans = 0
used = [False] * n
for i in range(n):
    if used[i]:
        continue

    # ループを取り出す
    loop = []
    at = i
    while not used[at]:
        loop.append(c[at])
        used[at] = True
        at = p[at]

    siz = len(loop)
    cusum = [0] + loop * 3
    for i in range(siz * 3):
        cusum[i + 1] += cusum[i]
    sum = max(0, cusum[siz])

    # k % siz 回以下での最大 -> 累積和にスライド最大値
    d = k % siz
    swag = SWAG()
    for i in range(d):
        swag.push(cusum[i])
    for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz))
        swag.pop()

    if k < siz:
        continue

    # k % siz + siz 回以下での最大
    d = k % siz + siz
    swag = SWAG()
    for i in range(d):
        swag.push(cusum[i])
    for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz - 1))
        swag.pop()

print(ans) import sys sys class SWAG:
    def __init__(self):
        self.fold_l = []
        self.r = []
        self.fold_r = ~(1 << 60)
    def push(self, a):
        if self.fold_r < a:
            self.fold_r = a
        self.r.append(a)
    def pop(self):
        if not self.fold_l:
            self.r.reverse()
            self.fold_l = self.r
            self.r = []
            self.fold_r = ~(1 << 60)
            fold_l = self.fold_l
            for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
        self.fold_l.pop()
    def get(self):
        if not self.fold_l:
            return self.fold_r
        elif not self.r:
            return self.fold_l[-1]
        else:
            return max(self.fold_l[-1], self.fold_r) SWAG def __init__(self):
        self.fold_l = []
        self.r = []
        self.fold_r = ~(1 << 60)
     __init__ self.fold_l = [] self.fold_l self fold_l [] self.r = [] self.r self r [] self.fold_r = ~(1 << 60) self.fold_r self fold_r ~(1 << 60) 1 << 60 1 60 self self def push(self, a):
        if self.fold_r < a:
            self.fold_r = a
        self.r.append(a)
     push if self.fold_r < a:
            self.fold_r = a
         self.fold_r < a self.fold_r self fold_r a self.fold_r = a self.fold_r self fold_r a self.r.append(a) self.r.append self.r self r append a self self a a def pop(self):
        if not self.fold_l:
            self.r.reverse()
            self.fold_l = self.r
            self.r = []
            self.fold_r = ~(1 << 60)
            fold_l = self.fold_l
            for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
        self.fold_l.pop()
     pop if not self.fold_l:
            self.r.reverse()
            self.fold_l = self.r
            self.r = []
            self.fold_r = ~(1 << 60)
            fold_l = self.fold_l
            for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
         not self.fold_l self.fold_l self fold_l self.r.reverse() self.r.reverse self.r self r reverse self.fold_l = self.r self.fold_l self fold_l self.r self r self.r = [] self.r self r [] self.fold_r = ~(1 << 60) self.fold_r self fold_r ~(1 << 60) 1 << 60 1 60 fold_l = self.fold_l fold_l self.fold_l self fold_l for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
         i range(len(fold_l) - 1) range len(fold_l) - 1 len(fold_l) len fold_l 1 if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
         fold_l[i + 1] < fold_l[i] fold_l[i + 1] fold_l i + 1 i 1 fold_l[i] fold_l i fold_l[i + 1] = fold_l[i] fold_l[i + 1] fold_l i + 1 i 1 fold_l[i] fold_l i self.fold_l.pop() self.fold_l.pop self.fold_l self fold_l pop self self def get(self):
        if not self.fold_l:
            return self.fold_r
        elif not self.r:
            return self.fold_l[-1]
        else:
            return max(self.fold_l[-1], self.fold_r) get if not self.fold_l:
            return self.fold_r
        elif not self.r:
            return self.fold_l[-1]
        else:
            return max(self.fold_l[-1], self.fold_r) not self.fold_l self.fold_l self fold_l return self.fold_r self.fold_r self fold_r elif not self.r:
            return self.fold_l[-1]
         not self.r self.r self r return self.fold_l[-1] self.fold_l[-1] self.fold_l self fold_l -1 return max(self.fold_l[-1], self.fold_r) max(self.fold_l[-1], self.fold_r) max self.fold_l[-1] self.fold_l self fold_l -1 self.fold_r self fold_r self self 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 [*map(int, input().split())] *map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split c = [*map(int, input().split())] c [*map(int, input().split())] *map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split for i in range(n):
    p[i] -= 1 i range(n) range n p[i] -= 1 p[i] p i 1 if max(c) < 0:
    print(max(c))
    sys.exit() max(c) < 0 max(c) max c 0 print(max(c)) print max(c) max c sys.exit() sys.exit sys exit ans = 0 ans 0 used = [False] * n used [False] * n [False] False n for i in range(n):
    if used[i]:
        continue

    # ループを取り出す
    loop = []
    at = i
    while not used[at]:
        loop.append(c[at])
        used[at] = True
        at = p[at]

    siz = len(loop)
    cusum = [0] + loop * 3
    for i in range(siz * 3):
        cusum[i + 1] += cusum[i]
    sum = max(0, cusum[siz])

    # k % siz 回以下での最大 -> 累積和にスライド最大値
    d = k % siz
    swag = SWAG()
    for i in range(d):
        swag.push(cusum[i])
    for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz))
        swag.pop()

    if k < siz:
        continue

    # k % siz + siz 回以下での最大
    d = k % siz + siz
    swag = SWAG()
    for i in range(d):
        swag.push(cusum[i])
    for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz - 1))
        swag.pop() i range(n) range n if used[i]:
        continue

    # ループを取り出す
     used[i] used i continue loop = [] loop [] at = i at i while not used[at]:
        loop.append(c[at])
        used[at] = True
        at = p[at]

     not used[at] used[at] used at loop.append(c[at]) loop.append loop append c[at] c at used[at] = True used[at] used at True at = p[at] at p[at] p at siz = len(loop) siz len(loop) len loop cusum = [0] + loop * 3 cusum [0] + loop * 3 [0] 0 loop * 3 loop 3 for i in range(siz * 3):
        cusum[i + 1] += cusum[i]
     i range(siz * 3) range siz * 3 siz 3 cusum[i + 1] += cusum[i] cusum[i + 1] cusum i + 1 i 1 cusum[i] cusum i sum = max(0, cusum[siz]) sum max(0, cusum[siz]) max 0 cusum[siz] cusum siz d = k % siz d k % siz k siz swag = SWAG() swag SWAG() SWAG for i in range(d):
        swag.push(cusum[i])
     i range(d) range d swag.push(cusum[i]) swag.push swag push cusum[i] cusum i for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz))
        swag.pop()

     i range(siz) range siz swag.push(cusum[d + i]) swag.push swag push cusum[d + i] cusum d + i d i ans = max(ans, swag.get() - cusum[i] + sum * (k // siz)) ans max(ans, swag.get() - cusum[i] + sum * (k // siz)) max ans swag.get() - cusum[i] + sum * (k // siz) swag.get() - cusum[i] swag.get() swag.get swag get cusum[i] cusum i sum * (k // siz) sum k // siz k siz swag.pop() swag.pop swag pop if k < siz:
        continue

    # k % siz + siz 回以下での最大
     k < siz k siz continue d = k % siz + siz d k % siz + siz k % siz k siz siz swag = SWAG() swag SWAG() SWAG for i in range(d):
        swag.push(cusum[i])
     i range(d) range d swag.push(cusum[i]) swag.push swag push cusum[i] cusum i for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz - 1))
        swag.pop() i range(siz) range siz swag.push(cusum[d + i]) swag.push swag push cusum[d + i] cusum d + i d i ans = max(ans, swag.get() - cusum[i] + sum * (k // siz - 1)) ans max(ans, swag.get() - cusum[i] + sum * (k // siz - 1)) max ans swag.get() - cusum[i] + sum * (k // siz - 1) swag.get() - cusum[i] swag.get() swag.get swag get cusum[i] cusum i sum * (k // siz - 1) sum k // siz - 1 k // siz k siz 1 swag.pop() swag.pop swag pop print(ans) print ans class SWAG:
    def __init__(self):
        self.fold_l = []
        self.r = []
        self.fold_r = ~(1 << 60)
    def push(self, a):
        if self.fold_r < a:
            self.fold_r = a
        self.r.append(a)
    def pop(self):
        if not self.fold_l:
            self.r.reverse()
            self.fold_l = self.r
            self.r = []
            self.fold_r = ~(1 << 60)
            fold_l = self.fold_l
            for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
        self.fold_l.pop()
    def get(self):
        if not self.fold_l:
            return self.fold_r
        elif not self.r:
            return self.fold_l[-1]
        else:
            return max(self.fold_l[-1], self.fold_r) class SWAG:
    def __init__(self):
        self.fold_l = []
        self.r = []
        self.fold_r = ~(1 << 60)
    def push(self, a):
        if self.fold_r < a:
            self.fold_r = a
        self.r.append(a)
    def pop(self):
        if not self.fold_l:
            self.r.reverse()
            self.fold_l = self.r
            self.r = []
            self.fold_r = ~(1 << 60)
            fold_l = self.fold_l
            for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
        self.fold_l.pop()
    def get(self):
        if not self.fold_l:
            return self.fold_r
        elif not self.r:
            return self.fold_l[-1]
        else:
            return max(self.fold_l[-1], self.fold_r) SWAG sum = max(0, cusum[siz]) max(0, cusum[siz]) sum at = i i at d = k % siz + siz k % siz + siz d ans = 0 0 ans siz = len(loop) len(loop) siz at = p[at] p[at] at p = [*map(int, input().split())] [*map(int, input().split())] p c = [*map(int, input().split())] [*map(int, input().split())] c cusum = [0] + loop * 3 [0] + loop * 3 cusum ans = max(ans, swag.get() - cusum[i] + sum * (k // siz)) max(ans, swag.get() - cusum[i] + sum * (k // siz)) ans d = k % siz k % siz d swag = SWAG() SWAG() swag used = [False] * n [False] * n used ans = max(ans, swag.get() - cusum[i] + sum * (k // siz - 1)) max(ans, swag.get() - cusum[i] + sum * (k // siz - 1)) ans swag = SWAG() SWAG() swag k = map(int, input().split()) map(int, input().split()) k n, k = map(int, input().split()) map(int, input().split()) n loop = [] [] loop
import sys

class SWAG:
    def __init__(self):
        self.fold_l = []
        self.r = []
        self.fold_r = ~(1 << 60)
    def push(self, a):
        if self.fold_r < a:
            self.fold_r = a
        self.r.append(a)
    def pop(self):
        if not self.fold_l:
            self.r.reverse()
            self.fold_l = self.r
            self.r = []
            self.fold_r = ~(1 << 60)
            fold_l = self.fold_l
            for i in range(len(fold_l) - 1):
                if fold_l[i + 1] < fold_l[i]:
                    fold_l[i + 1] = fold_l[i]
        self.fold_l.pop()
    def get(self):
        if not self.fold_l:
            return self.fold_r
        elif not self.r:
            return self.fold_l[-1]
        else:
            return max(self.fold_l[-1], self.fold_r)

n, k = map(int, input().split())
p = [*map(int, input().split())]
c = [*map(int, input().split())]
for i in range(n):
    p[i] -= 1

if max(c) < 0:
    print(max(c))
    sys.exit()

ans = 0
used = [False] * n
for i in range(n):
    if used[i]:
        continue

    # ループを取り出す
    loop = []
    at = i
    while not used[at]:
        loop.append(c[at])
        used[at] = True
        at = p[at]

    siz = len(loop)
    cusum = [0] + loop * 3
    for i in range(siz * 3):
        cusum[i + 1] += cusum[i]
    sum = max(0, cusum[siz])

    # k % siz 回以下での最大 -> 累積和にスライド最大値
    d = k % siz
    swag = SWAG()
    for i in range(d):
        swag.push(cusum[i])
    for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz))
        swag.pop()

    if k < siz:
        continue

    # k % siz + siz 回以下での最大
    d = k % siz + siz
    swag = SWAG()
    for i in range(d):
        swag.push(cusum[i])
    for i in range(siz):
        swag.push(cusum[d + i])
        ans = max(ans, swag.get() - cusum[i] + sum * (k // siz - 1))
        swag.pop()

print(ans)
