import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()


def main():
    n, k = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    ans = -10**18
    for i in range(n):
        start, count = i, 1
        val = c[start]
        while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]
        start = i
        if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = count + k % count
        else:
            a = 0
            num = min(k, count)
        for _ in range(num):
            a += c[start]
            start = p[start] - 1
            ans = max(a, ans)
    print(ans)


if __name__ == '__main__':
    main() import sys sys sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 def input(): return sys.stdin.readline().rstrip() input def main():
    n, k = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    ans = -10**18
    for i in range(n):
        start, count = i, 1
        val = c[start]
        while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]
        start = i
        if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = count + k % count
        else:
            a = 0
            num = min(k, count)
        for _ in range(num):
            a += c[start]
            start = p[start] - 1
            ans = max(a, ans)
    print(ans) main n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = tuple(map(int, input().split())) p tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split c = tuple(map(int, input().split())) c tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split ans = -10**18 ans -10**18 -10 18 for i in range(n):
        start, count = i, 1
        val = c[start]
        while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]
        start = i
        if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = count + k % count
        else:
            a = 0
            num = min(k, count)
        for _ in range(num):
            a += c[start]
            start = p[start] - 1
            ans = max(a, ans)
     i range(n) range n start, count = i, 1 start i count 1 val = c[start] val c[start] c start while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]
         p[start]-1 != i p[start]-1 p[start] p start 1 i start = p[start]-1 start p[start]-1 p[start] p start 1 count += 1 count 1 val += c[start] val c[start] c start start = i start i if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = count + k % count
        else:
            a = 0
            num = min(k, count)
         val > 0 val 0 a = (k//count-1)*val a (k//count-1)*val k//count-1 k//count k count 1 val ans = max(a, ans) ans max(a, ans) max a ans num = count + k % count num count + k % count count k % count k count a = 0 a 0 num = min(k, count) num min(k, count) min k count for _ in range(num):
            a += c[start]
            start = p[start] - 1
            ans = max(a, ans)
     _ range(num) range num a += c[start] a c[start] c start start = p[start] - 1 start p[start] - 1 p[start] p start 1 ans = max(a, ans) ans max(a, ans) max a ans print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    n, k = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    ans = -10**18
    for i in range(n):
        start, count = i, 1
        val = c[start]
        while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]
        start = i
        if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = count + k % count
        else:
            a = 0
            num = min(k, count)
        for _ in range(num):
            a += c[start]
            start = p[start] - 1
            ans = max(a, ans)
    print(ans) def main():
    n, k = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    ans = -10**18
    for i in range(n):
        start, count = i, 1
        val = c[start]
        while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]
        start = i
        if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = count + k % count
        else:
            a = 0
            num = min(k, count)
        for _ in range(num):
            a += c[start]
            start = p[start] - 1
            ans = max(a, ans)
    print(ans) main def input(): return sys.stdin.readline().rstrip() def input(): return sys.stdin.readline().rstrip() input
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()


def main():
    n, k = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))
    ans = -10**18
    for i in range(n):
        start, count = i, 1
        val = c[start]
        while p[start]-1 != i:
            start = p[start]-1
            count += 1
            val += c[start]
        start = i
        if val > 0:
            a = (k//count-1)*val
            ans = max(a, ans)
            num = count + k % count
        else:
            a = 0
            num = min(k, count)
        for _ in range(num):
            a += c[start]
            start = p[start] - 1
            ans = max(a, ans)
    print(ans)


if __name__ == '__main__':
    main()