n,k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

for i in range(n):
    p[i] -= 1

v = [False] * n
roop = []

for i in range(n):
    if not v[i]:
        tmp = [i]
        while True:
            t = p[tmp[-1]]
            v[t] = True
            if tmp[0] == t:
                break
            else:
                tmp.append(t)
        roop.append(tmp)

s = []

for li in roop:
    s1 = 0
    for i in li:
        s1 += c[i]
    s.append(s1)

s2 = []

for li in roop:
    tmp = [0]
    for i in li:
        tmp.append(tmp[-1] + c[i])
    for i in li:
        tmp.append(tmp[-1] + c[i])
    s2.append(tmp)

v2 = [-1] * n

for i,li in enumerate(roop):
    for j in li:
        v2[j] = i

ans = -10**20

for i,li in enumerate(roop):
    t = -10**20
    if s[i] <= 0:
        for j in range(len(li)):
            for k1 in range(j+1, j + 1 + min(k, len(li))):
                t = max(t, s2[i][k1] - s2[i][j])
        ans = max(ans, t)
    else:
        for cnt in range(min(len(li), k)+1):
            for j in range(len(li)):
                if k >= cnt:
                    t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)))
        ans = max(ans, t)

print(ans) n,k = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k p = list(map(int,input().split())) p list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () 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 for i in range(n):
    p[i] -= 1 i range(n) range n p[i] -= 1 p[i] p i 1 v = [False] * n v [False] * n [False] False n roop = [] roop [] for i in range(n):
    if not v[i]:
        tmp = [i]
        while True:
            t = p[tmp[-1]]
            v[t] = True
            if tmp[0] == t:
                break
            else:
                tmp.append(t)
        roop.append(tmp) i range(n) range n if not v[i]:
        tmp = [i]
        while True:
            t = p[tmp[-1]]
            v[t] = True
            if tmp[0] == t:
                break
            else:
                tmp.append(t)
        roop.append(tmp) not v[i] v[i] v i tmp = [i] tmp [i] i while True:
            t = p[tmp[-1]]
            v[t] = True
            if tmp[0] == t:
                break
            else:
                tmp.append(t)
         True t = p[tmp[-1]] t p[tmp[-1]] p tmp[-1] tmp -1 v[t] = True v[t] v t True if tmp[0] == t:
                break
            else:
                tmp.append(t)
         tmp[0] == t tmp[0] tmp 0 t break tmp.append(t) tmp.append tmp append t roop.append(tmp) roop.append roop append tmp s = [] s [] for li in roop:
    s1 = 0
    for i in li:
        s1 += c[i]
    s.append(s1) li roop s1 = 0 s1 0 for i in li:
        s1 += c[i]
     i li s1 += c[i] s1 c[i] c i s.append(s1) s.append s append s1 s2 = [] s2 [] for li in roop:
    tmp = [0]
    for i in li:
        tmp.append(tmp[-1] + c[i])
    for i in li:
        tmp.append(tmp[-1] + c[i])
    s2.append(tmp) li roop tmp = [0] tmp [0] 0 for i in li:
        tmp.append(tmp[-1] + c[i])
     i li tmp.append(tmp[-1] + c[i]) tmp.append tmp append tmp[-1] + c[i] tmp[-1] tmp -1 c[i] c i for i in li:
        tmp.append(tmp[-1] + c[i])
     i li tmp.append(tmp[-1] + c[i]) tmp.append tmp append tmp[-1] + c[i] tmp[-1] tmp -1 c[i] c i s2.append(tmp) s2.append s2 append tmp v2 = [-1] * n v2 [-1] * n [-1] -1 n for i,li in enumerate(roop):
    for j in li:
        v2[j] = i i li enumerate(roop) enumerate roop for j in li:
        v2[j] = i j li v2[j] = i v2[j] v2 j i ans = -10**20 ans -10**20 -10 20 for i,li in enumerate(roop):
    t = -10**20
    if s[i] <= 0:
        for j in range(len(li)):
            for k1 in range(j+1, j + 1 + min(k, len(li))):
                t = max(t, s2[i][k1] - s2[i][j])
        ans = max(ans, t)
    else:
        for cnt in range(min(len(li), k)+1):
            for j in range(len(li)):
                if k >= cnt:
                    t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)))
        ans = max(ans, t) i li enumerate(roop) enumerate roop t = -10**20 t -10**20 -10 20 if s[i] <= 0:
        for j in range(len(li)):
            for k1 in range(j+1, j + 1 + min(k, len(li))):
                t = max(t, s2[i][k1] - s2[i][j])
        ans = max(ans, t)
    else:
        for cnt in range(min(len(li), k)+1):
            for j in range(len(li)):
                if k >= cnt:
                    t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)))
        ans = max(ans, t) s[i] <= 0 s[i] s i 0 for j in range(len(li)):
            for k1 in range(j+1, j + 1 + min(k, len(li))):
                t = max(t, s2[i][k1] - s2[i][j])
         j range(len(li)) range len(li) len li for k1 in range(j+1, j + 1 + min(k, len(li))):
                t = max(t, s2[i][k1] - s2[i][j])
         k1 range(j+1, j + 1 + min(k, len(li))) range j+1 j 1 j + 1 + min(k, len(li)) j + 1 j 1 min(k, len(li)) min k len(li) len li t = max(t, s2[i][k1] - s2[i][j]) t max(t, s2[i][k1] - s2[i][j]) max t s2[i][k1] - s2[i][j] s2[i][k1] [i] s2 i k1 s2[i][j] [i] s2 i j ans = max(ans, t) ans max(ans, t) max ans t for cnt in range(min(len(li), k)+1):
            for j in range(len(li)):
                if k >= cnt:
                    t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)))
         cnt range(min(len(li), k)+1) range min(len(li), k)+1 min(len(li), k) min len(li) len li k 1 for j in range(len(li)):
                if k >= cnt:
                    t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)))
         j range(len(li)) range len(li) len li if k >= cnt:
                    t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)))
         k >= cnt k cnt t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li))) t max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li))) max t s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)) s2[i][cnt+j] - s2[i][j] s2[i][cnt+j] [i] s2 i cnt+j cnt j s2[i][j] [i] s2 i j s[i] * ((k - cnt) // len(li)) s[i] s i (k - cnt) // len(li) k - cnt k cnt len(li) len li ans = max(ans, t) ans max(ans, t) max ans t print(ans) print ans tmp = [0] [0] tmp s2 = [] [] s2 v = [False] * n [False] * n v s1 += c[i] c[i] s1 n,k = map(int,input().split()) map(int,input().split()) n v2 = [-1] * n [-1] * n v2 t = -10**20 -10**20 t t = p[tmp[-1]] p[tmp[-1]] t tmp = [i] [i] tmp s = [] [] s ans = max(ans, t) max(ans, t) ans s1 = 0 0 s1 ans = max(ans, t) max(ans, t) ans k = map(int,input().split()) map(int,input().split()) k p = list(map(int,input().split())) list(map(int,input().split())) p roop = [] [] roop ans = -10**20 -10**20 ans t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li))) max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li))) t c = list(map(int,input().split())) list(map(int,input().split())) c t = max(t, s2[i][k1] - s2[i][j]) max(t, s2[i][k1] - s2[i][j]) t
n,k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

for i in range(n):
    p[i] -= 1

v = [False] * n
roop = []

for i in range(n):
    if not v[i]:
        tmp = [i]
        while True:
            t = p[tmp[-1]]
            v[t] = True
            if tmp[0] == t:
                break
            else:
                tmp.append(t)
        roop.append(tmp)

s = []

for li in roop:
    s1 = 0
    for i in li:
        s1 += c[i]
    s.append(s1)

s2 = []

for li in roop:
    tmp = [0]
    for i in li:
        tmp.append(tmp[-1] + c[i])
    for i in li:
        tmp.append(tmp[-1] + c[i])
    s2.append(tmp)

v2 = [-1] * n

for i,li in enumerate(roop):
    for j in li:
        v2[j] = i

ans = -10**20

for i,li in enumerate(roop):
    t = -10**20
    if s[i] <= 0:
        for j in range(len(li)):
            for k1 in range(j+1, j + 1 + min(k, len(li))):
                t = max(t, s2[i][k1] - s2[i][j])
        ans = max(ans, t)
    else:
        for cnt in range(min(len(li), k)+1):
            for j in range(len(li)):
                if k >= cnt:
                    t = max(t, s2[i][cnt+j] - s2[i][j] + s[i] * ((k - cnt) // len(li)))
        ans = max(ans, t)

print(ans)
