'''
4 4
4 1 2 3
-19 4 10 6

4 5
4 1 2 3
-19 4 10 6

4 8
4 1 2 3
-19 4 10 6
'''
n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [i-1 for i in p]
c = list(map(int, input().split()))

minusOnly = True
for i in range(n):
    if c[i] > 0:
        minusOnly = False
        break

if minusOnly:
    print(max(c))
    exit()

ans = 0
used = [False]*n

for st in range(n):
    if not used[st]:
        gr = []
        now = st
        gr.append(st)
        used[st] = True
        while True:
            now = p[now]
            gr.append(now)
            used[now] = True
            if now == st:
                break
        gr.pop()

        cs = [c[i] for i in gr]
        s = sum(cs)
        point = 0
        r = k
        if r > 2*len(gr):
            if s > 0:
                point += s*(k//len(gr)-1)
                r -= len(gr)*(k//len(gr)-1)
            else:
                r = 2*len(gr)
        
        cs = cs + cs + cs
        plus = 0
        for i in range(len(gr)):
            nowSum = 0
            for g in range(r):
                nowSum += cs[i+g]
                plus = max(plus, nowSum)
        
        point += plus
        ans = max(ans, point)

print(ans) '''
4 4
4 1 2 3
-19 4 10 6

4 5
4 1 2 3
-19 4 10 6

4 8
4 1 2 3
-19 4 10 6
''' 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 i-1 for i in p for i in p i p for i in p i-1 i 1 p = [i-1 for i in p] p [i-1 for i in p] c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split minusOnly = True minusOnly True for i in range(n):
    if c[i] > 0:
        minusOnly = False
        break i range(n) range n if c[i] > 0:
        minusOnly = False
        break c[i] > 0 c[i] c i 0 minusOnly = False minusOnly False break if minusOnly:
    print(max(c))
    exit() minusOnly print(max(c)) print max(c) max c exit() exit ans = 0 ans 0 used = [False]*n used [False]*n [False] False n for st in range(n):
    if not used[st]:
        gr = []
        now = st
        gr.append(st)
        used[st] = True
        while True:
            now = p[now]
            gr.append(now)
            used[now] = True
            if now == st:
                break
        gr.pop()

        cs = [c[i] for i in gr]
        s = sum(cs)
        point = 0
        r = k
        if r > 2*len(gr):
            if s > 0:
                point += s*(k//len(gr)-1)
                r -= len(gr)*(k//len(gr)-1)
            else:
                r = 2*len(gr)
        
        cs = cs + cs + cs
        plus = 0
        for i in range(len(gr)):
            nowSum = 0
            for g in range(r):
                nowSum += cs[i+g]
                plus = max(plus, nowSum)
        
        point += plus
        ans = max(ans, point) st range(n) range n if not used[st]:
        gr = []
        now = st
        gr.append(st)
        used[st] = True
        while True:
            now = p[now]
            gr.append(now)
            used[now] = True
            if now == st:
                break
        gr.pop()

        cs = [c[i] for i in gr]
        s = sum(cs)
        point = 0
        r = k
        if r > 2*len(gr):
            if s > 0:
                point += s*(k//len(gr)-1)
                r -= len(gr)*(k//len(gr)-1)
            else:
                r = 2*len(gr)
        
        cs = cs + cs + cs
        plus = 0
        for i in range(len(gr)):
            nowSum = 0
            for g in range(r):
                nowSum += cs[i+g]
                plus = max(plus, nowSum)
        
        point += plus
        ans = max(ans, point) not used[st] used[st] used st gr = [] gr [] now = st now st gr.append(st) gr.append gr append st used[st] = True used[st] used st True while True:
            now = p[now]
            gr.append(now)
            used[now] = True
            if now == st:
                break
         True now = p[now] now p[now] p now gr.append(now) gr.append gr append now used[now] = True used[now] used now True if now == st:
                break
         now == st now st break gr.pop() gr.pop gr pop c[i] for i in gr for i in gr i gr for i in gr c[i] c i cs = [c[i] for i in gr] cs [c[i] for i in gr] s = sum(cs) s sum(cs) sum cs point = 0 point 0 r = k r k if r > 2*len(gr):
            if s > 0:
                point += s*(k//len(gr)-1)
                r -= len(gr)*(k//len(gr)-1)
            else:
                r = 2*len(gr)
        
         r > 2*len(gr) r 2*len(gr) 2 len(gr) len gr if s > 0:
                point += s*(k//len(gr)-1)
                r -= len(gr)*(k//len(gr)-1)
            else:
                r = 2*len(gr)
        
         s > 0 s 0 point += s*(k//len(gr)-1) point s*(k//len(gr)-1) s k//len(gr)-1 k//len(gr) k len(gr) len gr 1 r -= len(gr)*(k//len(gr)-1) r len(gr)*(k//len(gr)-1) len(gr) len gr k//len(gr)-1 k//len(gr) k len(gr) len gr 1 r = 2*len(gr) r 2*len(gr) 2 len(gr) len gr cs = cs + cs + cs cs cs + cs + cs cs + cs cs cs cs plus = 0 plus 0 for i in range(len(gr)):
            nowSum = 0
            for g in range(r):
                nowSum += cs[i+g]
                plus = max(plus, nowSum)
        
         i range(len(gr)) range len(gr) len gr nowSum = 0 nowSum 0 for g in range(r):
                nowSum += cs[i+g]
                plus = max(plus, nowSum)
        
         g range(r) range r nowSum += cs[i+g] nowSum cs[i+g] cs i+g i g plus = max(plus, nowSum) plus max(plus, nowSum) max plus nowSum point += plus point plus ans = max(ans, point) ans max(ans, point) max ans point print(ans) print ans n, k = map(int, input().split()) map(int, input().split()) n ans = 0 0 ans ans = max(ans, point) max(ans, point) ans minusOnly = True True minusOnly r = 2*len(gr) 2*len(gr) r now = st st now k = map(int, input().split()) map(int, input().split()) k point = 0 0 point gr = [] [] gr p = [i-1 for i in p] [i-1 for i in p] p point += s*(k//len(gr)-1) s*(k//len(gr)-1) point minusOnly = False False minusOnly cs = [c[i] for i in gr] [c[i] for i in gr] cs point += plus plus point nowSum = 0 0 nowSum plus = max(plus, nowSum) max(plus, nowSum) plus p = list(map(int, input().split())) list(map(int, input().split())) p cs = cs + cs + cs cs + cs + cs cs c = list(map(int, input().split())) list(map(int, input().split())) c nowSum += cs[i+g] cs[i+g] nowSum s = sum(cs) sum(cs) s r = k k r now = p[now] p[now] now plus = 0 0 plus r -= len(gr)*(k//len(gr)-1) len(gr)*(k//len(gr)-1) r used = [False]*n [False]*n used
'''
4 4
4 1 2 3
-19 4 10 6

4 5
4 1 2 3
-19 4 10 6

4 8
4 1 2 3
-19 4 10 6
'''
n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [i-1 for i in p]
c = list(map(int, input().split()))

minusOnly = True
for i in range(n):
    if c[i] > 0:
        minusOnly = False
        break

if minusOnly:
    print(max(c))
    exit()

ans = 0
used = [False]*n

for st in range(n):
    if not used[st]:
        gr = []
        now = st
        gr.append(st)
        used[st] = True
        while True:
            now = p[now]
            gr.append(now)
            used[now] = True
            if now == st:
                break
        gr.pop()

        cs = [c[i] for i in gr]
        s = sum(cs)
        point = 0
        r = k
        if r > 2*len(gr):
            if s > 0:
                point += s*(k//len(gr)-1)
                r -= len(gr)*(k//len(gr)-1)
            else:
                r = 2*len(gr)
        
        cs = cs + cs + cs
        plus = 0
        for i in range(len(gr)):
            nowSum = 0
            for g in range(r):
                nowSum += cs[i+g]
                plus = max(plus, nowSum)
        
        point += plus
        ans = max(ans, point)

print(ans)