n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
p.insert(0, 0)
c.insert(0, 0)
visit = [0] * (n + 1)
cyclelist = []
for i in range(1, n + 1):
    if visit[i] == 0:
        a = [c[i]]
        visit[i] = 1
        while visit[p[i]] == 0:
            i = p[i]
            visit[i] = 1
            a.append(c[i])
        cyclelist.append(a)
ans = -1145141919810
for cycle in cyclelist:
    s = sum(cycle)
    l = len(cycle)
    a = 0
    if l < k and s > 0:
        a = s * ((k - 1) // l)
    now = 0
    x = k % l
    if x == 0:
        x = l
    for i in range(min(k, l)):
        now += cycle[i]
        for j in range(l):
            now += cycle[(i + j + 1) % l] - cycle[j]
            if i < x:
                ans = max(ans, a + now)
            else:
                ans = max(ans, now)
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 p.insert(0, 0) p.insert p insert 0 0 c.insert(0, 0) c.insert c insert 0 0 visit = [0] * (n + 1) visit [0] * (n + 1) [0] 0 n + 1 n 1 cyclelist = [] cyclelist [] for i in range(1, n + 1):
    if visit[i] == 0:
        a = [c[i]]
        visit[i] = 1
        while visit[p[i]] == 0:
            i = p[i]
            visit[i] = 1
            a.append(c[i])
        cyclelist.append(a) i range(1, n + 1) range 1 n + 1 n 1 if visit[i] == 0:
        a = [c[i]]
        visit[i] = 1
        while visit[p[i]] == 0:
            i = p[i]
            visit[i] = 1
            a.append(c[i])
        cyclelist.append(a) visit[i] == 0 visit[i] visit i 0 a = [c[i]] a [c[i]] c[i] c i visit[i] = 1 visit[i] visit i 1 while visit[p[i]] == 0:
            i = p[i]
            visit[i] = 1
            a.append(c[i])
         visit[p[i]] == 0 visit[p[i]] visit p[i] p i 0 i = p[i] i p[i] p i visit[i] = 1 visit[i] visit i 1 a.append(c[i]) a.append a append c[i] c i cyclelist.append(a) cyclelist.append cyclelist append a ans = -1145141919810 ans -1145141919810 for cycle in cyclelist:
    s = sum(cycle)
    l = len(cycle)
    a = 0
    if l < k and s > 0:
        a = s * ((k - 1) // l)
    now = 0
    x = k % l
    if x == 0:
        x = l
    for i in range(min(k, l)):
        now += cycle[i]
        for j in range(l):
            now += cycle[(i + j + 1) % l] - cycle[j]
            if i < x:
                ans = max(ans, a + now)
            else:
                ans = max(ans, now) cycle cyclelist s = sum(cycle) s sum(cycle) sum cycle l = len(cycle) l len(cycle) len cycle a = 0 a 0 if l < k and s > 0:
        a = s * ((k - 1) // l)
     l < k and s > 0 l < k l k s > 0 s 0 a = s * ((k - 1) // l) a s * ((k - 1) // l) s (k - 1) // l k - 1 k 1 l now = 0 now 0 x = k % l x k % l k l if x == 0:
        x = l
     x == 0 x 0 x = l x l for i in range(min(k, l)):
        now += cycle[i]
        for j in range(l):
            now += cycle[(i + j + 1) % l] - cycle[j]
            if i < x:
                ans = max(ans, a + now)
            else:
                ans = max(ans, now) i range(min(k, l)) range min(k, l) min k l now += cycle[i] now cycle[i] cycle i for j in range(l):
            now += cycle[(i + j + 1) % l] - cycle[j]
            if i < x:
                ans = max(ans, a + now)
            else:
                ans = max(ans, now) j range(l) range l now += cycle[(i + j + 1) % l] - cycle[j] now cycle[(i + j + 1) % l] - cycle[j] cycle[(i + j + 1) % l] cycle (i + j + 1) % l i + j + 1 i + j i j 1 l cycle[j] cycle j if i < x:
                ans = max(ans, a + now)
            else:
                ans = max(ans, now) i < x i x ans = max(ans, a + now) ans max(ans, a + now) max ans a + now a now ans = max(ans, now) ans max(ans, now) max ans now print(ans) print ans cyclelist = [] [] cyclelist i = p[i] p[i] i x = l l x now += cycle[(i + j + 1) % l] - cycle[j] cycle[(i + j + 1) % l] - cycle[j] now ans = max(ans, now) max(ans, now) ans now += cycle[i] cycle[i] now k = map(int, input().split()) map(int, input().split()) k p = list(map(int, input().split())) list(map(int, input().split())) p l = len(cycle) len(cycle) l ans = -1145141919810 -1145141919810 ans c = list(map(int, input().split())) list(map(int, input().split())) c n, k = map(int, input().split()) map(int, input().split()) n a = s * ((k - 1) // l) s * ((k - 1) // l) a now = 0 0 now a = [c[i]] [c[i]] a x = k % l k % l x a = 0 0 a ans = max(ans, a + now) max(ans, a + now) ans s = sum(cycle) sum(cycle) s visit = [0] * (n + 1) [0] * (n + 1) visit
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
p.insert(0, 0)
c.insert(0, 0)
visit = [0] * (n + 1)
cyclelist = []
for i in range(1, n + 1):
    if visit[i] == 0:
        a = [c[i]]
        visit[i] = 1
        while visit[p[i]] == 0:
            i = p[i]
            visit[i] = 1
            a.append(c[i])
        cyclelist.append(a)
ans = -1145141919810
for cycle in cyclelist:
    s = sum(cycle)
    l = len(cycle)
    a = 0
    if l < k and s > 0:
        a = s * ((k - 1) // l)
    now = 0
    x = k % l
    if x == 0:
        x = l
    for i in range(min(k, l)):
        now += cycle[i]
        for j in range(l):
            now += cycle[(i + j + 1) % l] - cycle[j]
            if i < x:
                ans = max(ans, a + now)
            else:
                ans = max(ans, now)
print(ans)