import queue

N, K = map(int, input().split())
P = list(map(lambda v: v-1, map(int, input().split())))
C = list(map(int, input().split()))

visited = [False] * N
q = queue.Queue()
lists = list()

for i in range(N):
    q.put(dict(index=i, l=None))
    while not q.empty():
        d = q.get()
        if visited[d['index']]:
            continue
        visited[d['index']] = True
        l = d['l']
        if l is None:
            l = list()
            lists.append(l)
        l.append(C[d['index']])
        q.put(dict(index=P[d['index']], l=l))

maxv = -float('inf')

for l in lists:
    summ = sum(l)
    if summ > 0:
        score = summ * (K//len(l))
        rest = K % len(l)
        if rest == 0:
            score -= summ
            rest = len(l)
        for i in range(len(l)):
            v = 0
            for j in range(rest):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, score+v)
    else:
        for i in range(len(l)):
            v = 0
            for j in range(len(l)):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, v)
print(maxv) import queue queue N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda v: v-1, map(int, input().split()))) P list(map(lambda v: v-1, map(int, input().split()))) list map(lambda v: v-1, map(int, input().split())) map lambda v: v-1 v-1 v 1 v 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 visited = [False] * N visited [False] * N [False] False N q = queue.Queue() q queue.Queue() queue.Queue queue Queue lists = list() lists list() list for i in range(N):
    q.put(dict(index=i, l=None))
    while not q.empty():
        d = q.get()
        if visited[d['index']]:
            continue
        visited[d['index']] = True
        l = d['l']
        if l is None:
            l = list()
            lists.append(l)
        l.append(C[d['index']])
        q.put(dict(index=P[d['index']], l=l)) i range(N) range N q.put(dict(index=i, l=None)) q.put q put dict(index=i, l=None) dict index=i index i l=None l None while not q.empty():
        d = q.get()
        if visited[d['index']]:
            continue
        visited[d['index']] = True
        l = d['l']
        if l is None:
            l = list()
            lists.append(l)
        l.append(C[d['index']])
        q.put(dict(index=P[d['index']], l=l)) not q.empty() q.empty() q.empty q empty d = q.get() d q.get() q.get q get if visited[d['index']]:
            continue
         visited[d['index']] visited d['index'] d 'index' continue visited[d['index']] = True visited[d['index']] visited d['index'] d 'index' True l = d['l'] l d['l'] d 'l' if l is None:
            l = list()
            lists.append(l)
         l is None l None l = list() l list() list lists.append(l) lists.append lists append l l.append(C[d['index']]) l.append l append C[d['index']] C d['index'] d 'index' q.put(dict(index=P[d['index']], l=l)) q.put q put dict(index=P[d['index']], l=l) dict index=P[d['index']] index P[d['index']] P d['index'] d 'index' l=l l l maxv = -float('inf') maxv -float('inf') float('inf') float 'inf' for l in lists:
    summ = sum(l)
    if summ > 0:
        score = summ * (K//len(l))
        rest = K % len(l)
        if rest == 0:
            score -= summ
            rest = len(l)
        for i in range(len(l)):
            v = 0
            for j in range(rest):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, score+v)
    else:
        for i in range(len(l)):
            v = 0
            for j in range(len(l)):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, v) l lists summ = sum(l) summ sum(l) sum l if summ > 0:
        score = summ * (K//len(l))
        rest = K % len(l)
        if rest == 0:
            score -= summ
            rest = len(l)
        for i in range(len(l)):
            v = 0
            for j in range(rest):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, score+v)
    else:
        for i in range(len(l)):
            v = 0
            for j in range(len(l)):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, v) summ > 0 summ 0 score = summ * (K//len(l)) score summ * (K//len(l)) summ K//len(l) K len(l) len l rest = K % len(l) rest K % len(l) K len(l) len l if rest == 0:
            score -= summ
            rest = len(l)
         rest == 0 rest 0 score -= summ score summ rest = len(l) rest len(l) len l for i in range(len(l)):
            v = 0
            for j in range(rest):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, score+v)
     i range(len(l)) range len(l) len l v = 0 v 0 for j in range(rest):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, score+v)
     j range(rest) range rest v += l[i+j if i+j < len(l) else i+j-len(l)] v l[i+j if i+j < len(l) else i+j-len(l)] l i+j if i+j < len(l) else i+j-len(l) i+j < len(l) i+j i j len(l) len l i+j i j i+j-len(l) i+j i j len(l) len l maxv = max(maxv, score+v) maxv max(maxv, score+v) max maxv score+v score v for i in range(len(l)):
            v = 0
            for j in range(len(l)):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, v) i range(len(l)) range len(l) len l v = 0 v 0 for j in range(len(l)):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, v) j range(len(l)) range len(l) len l v += l[i+j if i+j < len(l) else i+j-len(l)] v l[i+j if i+j < len(l) else i+j-len(l)] l i+j if i+j < len(l) else i+j-len(l) i+j < len(l) i+j i j len(l) len l i+j i j i+j-len(l) i+j i j len(l) len l maxv = max(maxv, v) maxv max(maxv, v) max maxv v print(maxv) print maxv score = summ * (K//len(l)) summ * (K//len(l)) score P = list(map(lambda v: v-1, map(int, input().split()))) list(map(lambda v: v-1, map(int, input().split()))) P C = list(map(int, input().split())) list(map(int, input().split())) C score -= summ summ score v = 0 0 v maxv = max(maxv, v) max(maxv, v) maxv K = map(int, input().split()) map(int, input().split()) K d = q.get() q.get() d N, K = map(int, input().split()) map(int, input().split()) N lists = list() list() lists visited = [False] * N [False] * N visited v += l[i+j if i+j < len(l) else i+j-len(l)] l[i+j if i+j < len(l) else i+j-len(l)] v q = queue.Queue() queue.Queue() q rest = len(l) len(l) rest summ = sum(l) sum(l) summ maxv = max(maxv, score+v) max(maxv, score+v) maxv l = d['l'] d['l'] l maxv = -float('inf') -float('inf') maxv v += l[i+j if i+j < len(l) else i+j-len(l)] l[i+j if i+j < len(l) else i+j-len(l)] v l = list() list() l rest = K % len(l) K % len(l) rest v = 0 0 v
import queue

N, K = map(int, input().split())
P = list(map(lambda v: v-1, map(int, input().split())))
C = list(map(int, input().split()))

visited = [False] * N
q = queue.Queue()
lists = list()

for i in range(N):
    q.put(dict(index=i, l=None))
    while not q.empty():
        d = q.get()
        if visited[d['index']]:
            continue
        visited[d['index']] = True
        l = d['l']
        if l is None:
            l = list()
            lists.append(l)
        l.append(C[d['index']])
        q.put(dict(index=P[d['index']], l=l))

maxv = -float('inf')

for l in lists:
    summ = sum(l)
    if summ > 0:
        score = summ * (K//len(l))
        rest = K % len(l)
        if rest == 0:
            score -= summ
            rest = len(l)
        for i in range(len(l)):
            v = 0
            for j in range(rest):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, score+v)
    else:
        for i in range(len(l)):
            v = 0
            for j in range(len(l)):
                v += l[i+j if i+j < len(l) else i+j-len(l)]
                maxv = max(maxv, v)
print(maxv)
