#!/usr/bin python3
# -*- coding: utf-8 -*-

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

rc = [0] * n
rp = [0] * n
for i in range(n):
    if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = np
ret = - 10 ** 18
for i in range(n):
    if rc[i] == 1:
        if rp[i]>=0:
            ret = max(ret, k*rp[i])
        else:
            ret = max(ret, rp[i])
        continue
    pseq = [0]
    nx = p[i]
    for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
    pseq = pseq[1:]
    if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        if k%rc[i]!=0:
            tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp)
print(ret) 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()] c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split rc = [0] * n rc [0] * n [0] 0 n rp = [0] * n rp [0] * n [0] 0 n for i in range(n):
    if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = np i range(n) range n if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = np rc[i]!=0 rc[i] rc i 0 continue nw = set([i]) nw set([i]) set [i] i np = c[i] np c[i] c i nx = p[i] nx p[i] p i while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
         not nx in nw nx in nw nx nw nw.add(nx) nw.add nw add nx np += c[nx] np c[nx] c nx nx = p[nx] nx p[nx] p nx cnt = len(nw) cnt len(nw) len nw for x in nw:
            rc[x] = cnt
            rp[x] = np x nw rc[x] = cnt rc[x] rc x cnt rp[x] = np rp[x] rp x np ret = - 10 ** 18 ret - 10 ** 18 - 10 18 for i in range(n):
    if rc[i] == 1:
        if rp[i]>=0:
            ret = max(ret, k*rp[i])
        else:
            ret = max(ret, rp[i])
        continue
    pseq = [0]
    nx = p[i]
    for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
    pseq = pseq[1:]
    if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        if k%rc[i]!=0:
            tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp) i range(n) range n if rc[i] == 1:
        if rp[i]>=0:
            ret = max(ret, k*rp[i])
        else:
            ret = max(ret, rp[i])
        continue
     rc[i] == 1 rc[i] rc i 1 if rp[i]>=0:
            ret = max(ret, k*rp[i])
        else:
            ret = max(ret, rp[i])
         rp[i]>=0 rp[i] rp i 0 ret = max(ret, k*rp[i]) ret max(ret, k*rp[i]) max ret k*rp[i] k rp[i] rp i ret = max(ret, rp[i]) ret max(ret, rp[i]) max ret rp[i] rp i continue pseq = [0] pseq [0] 0 nx = p[i] nx p[i] p i for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
     j range(1,rc[i]+1) range 1 rc[i]+1 rc[i] rc i 1 pseq.append(pseq[-1]+c[nx]) pseq.append pseq append pseq[-1]+c[nx] pseq[-1] pseq -1 c[nx] c nx nx = p[nx] nx p[nx] p nx pseq = pseq[1:] pseq pseq[1:] pseq 1: 1 if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        if k%rc[i]!=0:
            tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp) k <= rc[i] k rc[i] rc i ret = max(ret, max(pseq[:k])) ret max(ret, max(pseq[:k])) max ret max(pseq[:k]) max pseq[:k] pseq :k k elif rp[i] < 0:
        ret = max(ret, max(pseq))
     rp[i] < 0 rp[i] rp i 0 ret = max(ret, max(pseq)) ret max(ret, max(pseq)) max ret max(pseq) max pseq tmp = max(pseq) tmp max(pseq) max pseq if k%rc[i]!=0:
            tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
         k%rc[i]!=0 k%rc[i] k rc[i] rc i 0 tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]])) tmp max(tmp, rp[i] + max(pseq[:k%rc[i]])) max tmp rp[i] + max(pseq[:k%rc[i]]) rp[i] rp i max(pseq[:k%rc[i]]) max pseq[:k%rc[i]] pseq :k%rc[i] k%rc[i] k rc[i] rc i tmp += rp[i]*(k//rc[i]-1) tmp rp[i]*(k//rc[i]-1) rp[i] rp i k//rc[i]-1 k//rc[i] k rc[i] rc i 1 ret = max(ret, tmp) ret max(ret, tmp) max ret tmp print(ret) print ret c = list(map(int, input().split())) list(map(int, input().split())) c rp = [0] * n [0] * n rp ret = - 10 ** 18 - 10 ** 18 ret ret = max(ret, tmp) max(ret, tmp) ret nx = p[i] p[i] nx ret = max(ret, max(pseq)) max(ret, max(pseq)) ret nw = set([i]) set([i]) nw ret = max(ret, k*rp[i]) max(ret, k*rp[i]) ret nx = p[nx] p[nx] nx np = c[i] c[i] np pseq = [0] [0] pseq cnt = len(nw) len(nw) cnt n, k = map(int, input().split()) map(int, input().split()) n tmp = max(pseq) max(pseq) tmp pseq = pseq[1:] pseq[1:] pseq np += c[nx] c[nx] np nx = p[i] p[i] nx ret = max(ret, rp[i]) max(ret, rp[i]) ret k = map(int, input().split()) map(int, input().split()) k nx = p[nx] p[nx] nx tmp += rp[i]*(k//rc[i]-1) rp[i]*(k//rc[i]-1) tmp rc = [0] * n [0] * n rc tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]])) max(tmp, rp[i] + max(pseq[:k%rc[i]])) tmp ret = max(ret, max(pseq[:k])) max(ret, max(pseq[:k])) ret p = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] p
#!/usr/bin python3
# -*- coding: utf-8 -*-

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

rc = [0] * n
rp = [0] * n
for i in range(n):
    if rc[i]!=0:
        continue
    else:
        nw = set([i])
        np = c[i]
        nx = p[i]
        while not nx in nw:
            nw.add(nx)
            np += c[nx]
            nx = p[nx]
        cnt = len(nw)
        for x in nw:
            rc[x] = cnt
            rp[x] = np
ret = - 10 ** 18
for i in range(n):
    if rc[i] == 1:
        if rp[i]>=0:
            ret = max(ret, k*rp[i])
        else:
            ret = max(ret, rp[i])
        continue
    pseq = [0]
    nx = p[i]
    for j in range(1,rc[i]+1):
        pseq.append(pseq[-1]+c[nx])
        nx = p[nx]
    pseq = pseq[1:]
    if k <= rc[i]:
        ret = max(ret, max(pseq[:k]))
    elif rp[i] < 0:
        ret = max(ret, max(pseq))
    else:
        tmp = max(pseq)
        if k%rc[i]!=0:
            tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
        tmp += rp[i]*(k//rc[i]-1)
        ret = max(ret, tmp)
print(ret)
