import sys
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())


def main():
    n, k = mi()
    p = list(map(lambda x: int(x)-1, input().split()))
    c = li()
    ans = -10**18
    for i in range(n):
        v = i
        cycle_cnt = 0
        cycle_sum = 0
        while True:
            cycle_cnt += 1
            cycle_sum += c[v]
            v = p[v]
            if v == i:
                break
        
        cnt = 0
        tmp = 0
        while True:
            tmp += c[v]
            cnt += 1
            if cnt > k:
                break
            r = (k-cnt)//cycle_cnt
            score = tmp+r*max(0, cycle_sum)
            ans = max(ans, score)
            v = p[v]
            if v == i:
                break
    print(ans)



if __name__ == '__main__':
    main() import sys sys def input(): return sys.stdin.readline().rstrip() input def ii(): return int(input()) ii def mi(): return map(int, input().split()) mi def li(): return list(mi()) li def main():
    n, k = mi()
    p = list(map(lambda x: int(x)-1, input().split()))
    c = li()
    ans = -10**18
    for i in range(n):
        v = i
        cycle_cnt = 0
        cycle_sum = 0
        while True:
            cycle_cnt += 1
            cycle_sum += c[v]
            v = p[v]
            if v == i:
                break
        
        cnt = 0
        tmp = 0
        while True:
            tmp += c[v]
            cnt += 1
            if cnt > k:
                break
            r = (k-cnt)//cycle_cnt
            score = tmp+r*max(0, cycle_sum)
            ans = max(ans, score)
            v = p[v]
            if v == i:
                break
    print(ans) main n, k = mi() n mi() mi k p = list(map(lambda x: int(x)-1, input().split())) p list(map(lambda x: int(x)-1, input().split())) list map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split c = li() c li() li ans = -10**18 ans -10**18 -10 18 for i in range(n):
        v = i
        cycle_cnt = 0
        cycle_sum = 0
        while True:
            cycle_cnt += 1
            cycle_sum += c[v]
            v = p[v]
            if v == i:
                break
        
        cnt = 0
        tmp = 0
        while True:
            tmp += c[v]
            cnt += 1
            if cnt > k:
                break
            r = (k-cnt)//cycle_cnt
            score = tmp+r*max(0, cycle_sum)
            ans = max(ans, score)
            v = p[v]
            if v == i:
                break
     i range(n) range n v = i v i cycle_cnt = 0 cycle_cnt 0 cycle_sum = 0 cycle_sum 0 while True:
            cycle_cnt += 1
            cycle_sum += c[v]
            v = p[v]
            if v == i:
                break
        
         True cycle_cnt += 1 cycle_cnt 1 cycle_sum += c[v] cycle_sum c[v] c v v = p[v] v p[v] p v if v == i:
                break
        
         v == i v i break cnt = 0 cnt 0 tmp = 0 tmp 0 while True:
            tmp += c[v]
            cnt += 1
            if cnt > k:
                break
            r = (k-cnt)//cycle_cnt
            score = tmp+r*max(0, cycle_sum)
            ans = max(ans, score)
            v = p[v]
            if v == i:
                break
     True tmp += c[v] tmp c[v] c v cnt += 1 cnt 1 if cnt > k:
                break
             cnt > k cnt k break r = (k-cnt)//cycle_cnt r (k-cnt)//cycle_cnt k-cnt k cnt cycle_cnt score = tmp+r*max(0, cycle_sum) score tmp+r*max(0, cycle_sum) tmp r*max(0, cycle_sum) r max(0, cycle_sum) max 0 cycle_sum ans = max(ans, score) ans max(ans, score) max ans score v = p[v] v p[v] p v if v == i:
                break
     v == i v i break print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def ii(): return int(input()) def ii(): return int(input()) ii def input(): return sys.stdin.readline().rstrip() def input(): return sys.stdin.readline().rstrip() input def mi(): return map(int, input().split()) def mi(): return map(int, input().split()) mi def main():
    n, k = mi()
    p = list(map(lambda x: int(x)-1, input().split()))
    c = li()
    ans = -10**18
    for i in range(n):
        v = i
        cycle_cnt = 0
        cycle_sum = 0
        while True:
            cycle_cnt += 1
            cycle_sum += c[v]
            v = p[v]
            if v == i:
                break
        
        cnt = 0
        tmp = 0
        while True:
            tmp += c[v]
            cnt += 1
            if cnt > k:
                break
            r = (k-cnt)//cycle_cnt
            score = tmp+r*max(0, cycle_sum)
            ans = max(ans, score)
            v = p[v]
            if v == i:
                break
    print(ans) def main():
    n, k = mi()
    p = list(map(lambda x: int(x)-1, input().split()))
    c = li()
    ans = -10**18
    for i in range(n):
        v = i
        cycle_cnt = 0
        cycle_sum = 0
        while True:
            cycle_cnt += 1
            cycle_sum += c[v]
            v = p[v]
            if v == i:
                break
        
        cnt = 0
        tmp = 0
        while True:
            tmp += c[v]
            cnt += 1
            if cnt > k:
                break
            r = (k-cnt)//cycle_cnt
            score = tmp+r*max(0, cycle_sum)
            ans = max(ans, score)
            v = p[v]
            if v == i:
                break
    print(ans) main def li(): return list(mi()) def li(): return list(mi()) li
import sys
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())


def main():
    n, k = mi()
    p = list(map(lambda x: int(x)-1, input().split()))
    c = li()
    ans = -10**18
    for i in range(n):
        v = i
        cycle_cnt = 0
        cycle_sum = 0
        while True:
            cycle_cnt += 1
            cycle_sum += c[v]
            v = p[v]
            if v == i:
                break
        
        cnt = 0
        tmp = 0
        while True:
            tmp += c[v]
            cnt += 1
            if cnt > k:
                break
            r = (k-cnt)//cycle_cnt
            score = tmp+r*max(0, cycle_sum)
            ans = max(ans, score)
            v = p[v]
            if v == i:
                break
    print(ans)



if __name__ == '__main__':
    main()