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

ans = -float('inf')
for i in range(n):
    s = []
    j = p[i]-1 
    s.append(c[j])
    while j != i:
        j = p[j]-1
        s.append(s[-1]+c[j])
    l = len(s)
    if k <= l:
        a = max(s[:k])
    elif s[-1] <= 0:
        a = max(s)
    else:
        w, r = divmod(k, l)
        a1 = s[-1]*(w-1) + max(s)
        a2 = s[-1]*w
        if r != 0:
            a2 += max(0, max(s[:r]))
        a = max(a1, a2)
    ans = max(ans, a)
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 ans = -float('inf') ans -float('inf') float('inf') float 'inf' for i in range(n):
    s = []
    j = p[i]-1 
    s.append(c[j])
    while j != i:
        j = p[j]-1
        s.append(s[-1]+c[j])
    l = len(s)
    if k <= l:
        a = max(s[:k])
    elif s[-1] <= 0:
        a = max(s)
    else:
        w, r = divmod(k, l)
        a1 = s[-1]*(w-1) + max(s)
        a2 = s[-1]*w
        if r != 0:
            a2 += max(0, max(s[:r]))
        a = max(a1, a2)
    ans = max(ans, a) i range(n) range n s = [] s [] j = p[i]-1 j p[i]-1 p[i] p i 1 s.append(c[j]) s.append s append c[j] c j while j != i:
        j = p[j]-1
        s.append(s[-1]+c[j])
     j != i j i j = p[j]-1 j p[j]-1 p[j] p j 1 s.append(s[-1]+c[j]) s.append s append s[-1]+c[j] s[-1] s -1 c[j] c j l = len(s) l len(s) len s if k <= l:
        a = max(s[:k])
    elif s[-1] <= 0:
        a = max(s)
    else:
        w, r = divmod(k, l)
        a1 = s[-1]*(w-1) + max(s)
        a2 = s[-1]*w
        if r != 0:
            a2 += max(0, max(s[:r]))
        a = max(a1, a2)
     k <= l k l a = max(s[:k]) a max(s[:k]) max s[:k] s :k k elif s[-1] <= 0:
        a = max(s)
     s[-1] <= 0 s[-1] s -1 0 a = max(s) a max(s) max s w, r = divmod(k, l) w divmod(k, l) divmod k l r a1 = s[-1]*(w-1) + max(s) a1 s[-1]*(w-1) + max(s) s[-1]*(w-1) s[-1] s -1 w-1 w 1 max(s) max s a2 = s[-1]*w a2 s[-1]*w s[-1] s -1 w if r != 0:
            a2 += max(0, max(s[:r]))
         r != 0 r 0 a2 += max(0, max(s[:r])) a2 max(0, max(s[:r])) max 0 max(s[:r]) max s[:r] s :r r a = max(a1, a2) a max(a1, a2) max a1 a2 ans = max(ans, a) ans max(ans, a) max ans a print(ans) print ans a = max(s[:k]) max(s[:k]) a a1 = s[-1]*(w-1) + max(s) s[-1]*(w-1) + max(s) a1 l = len(s) len(s) l r = divmod(k, l) divmod(k, l) r a = max(s) max(s) a ans = -float('inf') -float('inf') ans a = max(a1, a2) max(a1, a2) a j = p[j]-1 p[j]-1 j n, k = map(int, input().split()) map(int, input().split()) n c = list(map(int, input().split())) list(map(int, input().split())) c j = p[i]-1 p[i]-1 j a2 += max(0, max(s[:r])) max(0, max(s[:r])) a2 w, r = divmod(k, l) divmod(k, l) w s = [] [] s p = list(map(int, input().split())) list(map(int, input().split())) p a2 = s[-1]*w s[-1]*w a2 ans = max(ans, a) max(ans, a) ans k = map(int, input().split()) map(int, input().split()) k
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = -float('inf')
for i in range(n):
    s = []
    j = p[i]-1 
    s.append(c[j])
    while j != i:
        j = p[j]-1
        s.append(s[-1]+c[j])
    l = len(s)
    if k <= l:
        a = max(s[:k])
    elif s[-1] <= 0:
        a = max(s)
    else:
        w, r = divmod(k, l)
        a1 = s[-1]*(w-1) + max(s)
        a2 = s[-1]*w
        if r != 0:
            a2 += max(0, max(s[:r]))
        a = max(a1, a2)
    ans = max(ans, a)
print(ans)