import numpy as np


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

    seen = set()
    loop_list = []
    for start in range(n):
        v = start
        loop = []
        while v not in seen:
            seen.add(v)
            loop.append(c[v])
            v = p[v]
        seen.add(v)
        if loop:
            loop_list.append(np.array(loop))

    ans = min(c)
    for loop in loop_list:
        loop_len = len(loop)
        loop_num = k // loop_len
        loop_mod = k % loop_len
        loop_sum = sum(loop)
        table = np.zeros((loop_len, loop_len), dtype=int)
        table[0] = loop.cumsum()
        table[:, -1] = loop_sum
        for i in range(loop_len - 1):
            table[i + 1, :-1] = (table[i] - loop[i])[1:]
        ans = max(table[:, :min(loop_len, k)].max(), ans)
        if loop_num > 0:
            ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans)
            if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
    print(ans)


if __name__ == '__main__':
    main() import numpy as np numpy def main():
    n, k = map(int, input().split())
    p = [int(i) - 1 for i in input().split()]
    c = [int(i) for i in input().split()]

    seen = set()
    loop_list = []
    for start in range(n):
        v = start
        loop = []
        while v not in seen:
            seen.add(v)
            loop.append(c[v])
            v = p[v]
        seen.add(v)
        if loop:
            loop_list.append(np.array(loop))

    ans = min(c)
    for loop in loop_list:
        loop_len = len(loop)
        loop_num = k // loop_len
        loop_mod = k % loop_len
        loop_sum = sum(loop)
        table = np.zeros((loop_len, loop_len), dtype=int)
        table[0] = loop.cumsum()
        table[:, -1] = loop_sum
        for i in range(loop_len - 1):
            table[i + 1, :-1] = (table[i] - loop[i])[1:]
        ans = max(table[:, :min(loop_len, k)].max(), ans)
        if loop_num > 0:
            ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans)
            if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
    print(ans) main n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k int(i) - 1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) - 1 int(i) int i 1 p = [int(i) - 1 for i in input().split()] p [int(i) - 1 for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i c = [int(i) for i in input().split()] c [int(i) for i in input().split()] seen = set() seen set() set loop_list = [] loop_list [] for start in range(n):
        v = start
        loop = []
        while v not in seen:
            seen.add(v)
            loop.append(c[v])
            v = p[v]
        seen.add(v)
        if loop:
            loop_list.append(np.array(loop))

     start range(n) range n v = start v start loop = [] loop [] while v not in seen:
            seen.add(v)
            loop.append(c[v])
            v = p[v]
         v not in seen v seen seen.add(v) seen.add seen add v loop.append(c[v]) loop.append loop append c[v] c v v = p[v] v p[v] p v seen.add(v) seen.add seen add v if loop:
            loop_list.append(np.array(loop))

     loop loop_list.append(np.array(loop)) loop_list.append loop_list append np.array(loop) np.array np array loop ans = min(c) ans min(c) min c for loop in loop_list:
        loop_len = len(loop)
        loop_num = k // loop_len
        loop_mod = k % loop_len
        loop_sum = sum(loop)
        table = np.zeros((loop_len, loop_len), dtype=int)
        table[0] = loop.cumsum()
        table[:, -1] = loop_sum
        for i in range(loop_len - 1):
            table[i + 1, :-1] = (table[i] - loop[i])[1:]
        ans = max(table[:, :min(loop_len, k)].max(), ans)
        if loop_num > 0:
            ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans)
            if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
     loop loop_list loop_len = len(loop) loop_len len(loop) len loop loop_num = k // loop_len loop_num k // loop_len k loop_len loop_mod = k % loop_len loop_mod k % loop_len k loop_len loop_sum = sum(loop) loop_sum sum(loop) sum loop table = np.zeros((loop_len, loop_len), dtype=int) table np.zeros((loop_len, loop_len), dtype=int) np.zeros np zeros (loop_len, loop_len) loop_len loop_len dtype=int dtype int table[0] = loop.cumsum() table[0] table 0 loop.cumsum() loop.cumsum loop cumsum table[:, -1] = loop_sum table[:, -1] table : loop_sum for i in range(loop_len - 1):
            table[i + 1, :-1] = (table[i] - loop[i])[1:]
         i range(loop_len - 1) range loop_len - 1 loop_len 1 table[i + 1, :-1] = (table[i] - loop[i])[1:] table[i + 1, :-1] table i + 1 i 1 (table[i] - loop[i])[1:] table[i] - loop[i] table[i] table i loop[i] loop i 1: 1 ans = max(table[:, :min(loop_len, k)].max(), ans) ans max(table[:, :min(loop_len, k)].max(), ans) max table[:, :min(loop_len, k)].max() [:, :min(loop_len, k)].max [:, :min(loop_len, k)] table : max ans if loop_num > 0:
            ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans)
            if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
     loop_num > 0 loop_num 0 ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans) ans max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans) max loop_sum * (loop_num - 1) + table[:, :loop_len].max() loop_sum * (loop_num - 1) loop_sum loop_num - 1 loop_num 1 table[:, :loop_len].max() [:, :loop_len].max [:, :loop_len] table : max ans if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
     loop_mod > 0 loop_mod 0 ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans) ans max(loop_sum * loop_num + table[:, :loop_mod].max(), ans) max loop_sum * loop_num + table[:, :loop_mod].max() loop_sum * loop_num loop_sum loop_num table[:, :loop_mod].max() [:, :loop_mod].max [:, :loop_mod] table : max ans print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    n, k = map(int, input().split())
    p = [int(i) - 1 for i in input().split()]
    c = [int(i) for i in input().split()]

    seen = set()
    loop_list = []
    for start in range(n):
        v = start
        loop = []
        while v not in seen:
            seen.add(v)
            loop.append(c[v])
            v = p[v]
        seen.add(v)
        if loop:
            loop_list.append(np.array(loop))

    ans = min(c)
    for loop in loop_list:
        loop_len = len(loop)
        loop_num = k // loop_len
        loop_mod = k % loop_len
        loop_sum = sum(loop)
        table = np.zeros((loop_len, loop_len), dtype=int)
        table[0] = loop.cumsum()
        table[:, -1] = loop_sum
        for i in range(loop_len - 1):
            table[i + 1, :-1] = (table[i] - loop[i])[1:]
        ans = max(table[:, :min(loop_len, k)].max(), ans)
        if loop_num > 0:
            ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans)
            if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
    print(ans) def main():
    n, k = map(int, input().split())
    p = [int(i) - 1 for i in input().split()]
    c = [int(i) for i in input().split()]

    seen = set()
    loop_list = []
    for start in range(n):
        v = start
        loop = []
        while v not in seen:
            seen.add(v)
            loop.append(c[v])
            v = p[v]
        seen.add(v)
        if loop:
            loop_list.append(np.array(loop))

    ans = min(c)
    for loop in loop_list:
        loop_len = len(loop)
        loop_num = k // loop_len
        loop_mod = k % loop_len
        loop_sum = sum(loop)
        table = np.zeros((loop_len, loop_len), dtype=int)
        table[0] = loop.cumsum()
        table[:, -1] = loop_sum
        for i in range(loop_len - 1):
            table[i + 1, :-1] = (table[i] - loop[i])[1:]
        ans = max(table[:, :min(loop_len, k)].max(), ans)
        if loop_num > 0:
            ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans)
            if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
    print(ans) main
import numpy as np


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

    seen = set()
    loop_list = []
    for start in range(n):
        v = start
        loop = []
        while v not in seen:
            seen.add(v)
            loop.append(c[v])
            v = p[v]
        seen.add(v)
        if loop:
            loop_list.append(np.array(loop))

    ans = min(c)
    for loop in loop_list:
        loop_len = len(loop)
        loop_num = k // loop_len
        loop_mod = k % loop_len
        loop_sum = sum(loop)
        table = np.zeros((loop_len, loop_len), dtype=int)
        table[0] = loop.cumsum()
        table[:, -1] = loop_sum
        for i in range(loop_len - 1):
            table[i + 1, :-1] = (table[i] - loop[i])[1:]
        ans = max(table[:, :min(loop_len, k)].max(), ans)
        if loop_num > 0:
            ans = max(loop_sum * (loop_num - 1) + table[:, :loop_len].max(), ans)
            if loop_mod > 0:
                ans = max(loop_sum * loop_num + table[:, :loop_mod].max(), ans)
    print(ans)


if __name__ == '__main__':
    main()
