n , k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
loop = []
visit = [(-1,-1) for i in range(n)]
cou = -1
for i in range(n):
    if visit[i] != (-1,-1):
        continue
    cou += 1
    tyu  = [i]
    now = i
    visit[i] = (cou,0)
    t = 1
    while True:
        nex = p[now] - 1
        if nex == i:
            break
        visit[nex] = (cou,t)
        tyu.append(nex)
        now = nex
        t += 1
    loop.append(tyu)

ans = []

for i in range(n):
    p = loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]]
    kar = [0]
    kouho = []
    losum = 0
    lokz = len(p)
    for j in p:
        losum += c[j]
        kar.append(kar[-1] + c[j])
    if losum >= 0:
        for l in range(lokz+1):
            if l > k:
                break
            cou = losum*((k-l)//lokz) + kar[l]
            kouho.append(cou)
    elif losum < 0:
        for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
    ans.append(max(kouho))
print(max(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 loop = [] loop [] (-1,-1) for i in range(n) for i in range(n) i range(n) range n for i in range(n) (-1,-1) -1 -1 visit = [(-1,-1) for i in range(n)] visit [(-1,-1) for i in range(n)] cou = -1 cou -1 for i in range(n):
    if visit[i] != (-1,-1):
        continue
    cou += 1
    tyu  = [i]
    now = i
    visit[i] = (cou,0)
    t = 1
    while True:
        nex = p[now] - 1
        if nex == i:
            break
        visit[nex] = (cou,t)
        tyu.append(nex)
        now = nex
        t += 1
    loop.append(tyu) i range(n) range n if visit[i] != (-1,-1):
        continue
     visit[i] != (-1,-1) visit[i] visit i (-1,-1) -1 -1 continue cou += 1 cou 1 tyu  = [i] tyu [i] i now = i now i visit[i] = (cou,0) visit[i] visit i (cou,0) cou 0 t = 1 t 1 while True:
        nex = p[now] - 1
        if nex == i:
            break
        visit[nex] = (cou,t)
        tyu.append(nex)
        now = nex
        t += 1
     True nex = p[now] - 1 nex p[now] - 1 p[now] p now 1 if nex == i:
            break
         nex == i nex i break visit[nex] = (cou,t) visit[nex] visit nex (cou,t) cou t tyu.append(nex) tyu.append tyu append nex now = nex now nex t += 1 t 1 loop.append(tyu) loop.append loop append tyu ans = [] ans [] for i in range(n):
    p = loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]]
    kar = [0]
    kouho = []
    losum = 0
    lokz = len(p)
    for j in p:
        losum += c[j]
        kar.append(kar[-1] + c[j])
    if losum >= 0:
        for l in range(lokz+1):
            if l > k:
                break
            cou = losum*((k-l)//lokz) + kar[l]
            kouho.append(cou)
    elif losum < 0:
        for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
    ans.append(max(kouho)) i range(n) range n p = loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]] p loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]] loop[visit[i][0]][visit[i][1]:] [visit[i][0]] loop visit[i][0] [i] visit i 0 visit[i][1]: visit[i][1] [i] visit i 1 loop[visit[i][0]][:visit[i][1]] [visit[i][0]] loop visit[i][0] [i] visit i 0 :visit[i][1] visit[i][1] [i] visit i 1 kar = [0] kar [0] 0 kouho = [] kouho [] losum = 0 losum 0 lokz = len(p) lokz len(p) len p for j in p:
        losum += c[j]
        kar.append(kar[-1] + c[j])
     j p losum += c[j] losum c[j] c j kar.append(kar[-1] + c[j]) kar.append kar append kar[-1] + c[j] kar[-1] kar -1 c[j] c j if losum >= 0:
        for l in range(lokz+1):
            if l > k:
                break
            cou = losum*((k-l)//lokz) + kar[l]
            kouho.append(cou)
    elif losum < 0:
        for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
     losum >= 0 losum 0 for l in range(lokz+1):
            if l > k:
                break
            cou = losum*((k-l)//lokz) + kar[l]
            kouho.append(cou)
     l range(lokz+1) range lokz+1 lokz 1 if l > k:
                break
             l > k l k break cou = losum*((k-l)//lokz) + kar[l] cou losum*((k-l)//lokz) + kar[l] losum*((k-l)//lokz) losum (k-l)//lokz k-l k l lokz kar[l] kar l kouho.append(cou) kouho.append kouho append cou elif losum < 0:
        for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
     losum < 0 losum 0 for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
     l range(1,lokz+1) range 1 lokz+1 lokz 1 if l > k:
                break
             l > k l k break cou = kar[l] cou kar[l] kar l kouho.append(cou) kouho.append kouho append cou ans.append(max(kouho)) ans.append ans append max(kouho) max kouho print(max(ans)) print max(ans) max ans p = loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]] loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]] p n , k = map(int,input().split()) map(int,input().split()) n cou += 1 1 cou nex = p[now] - 1 p[now] - 1 nex tyu  = [i] [i] tyu k = map(int,input().split()) map(int,input().split()) k ans = [] [] ans losum += c[j] c[j] losum t += 1 1 t loop = [] [] loop t = 1 1 t kar = [0] [0] kar kouho = [] [] kouho cou = kar[l] kar[l] cou visit = [(-1,-1) for i in range(n)] [(-1,-1) for i in range(n)] visit cou = -1 -1 cou p = list(map(int,input().split())) list(map(int,input().split())) p lokz = len(p) len(p) lokz losum = 0 0 losum now = i i now now = nex nex now cou = losum*((k-l)//lokz) + kar[l] losum*((k-l)//lokz) + kar[l] cou c = list(map(int,input().split())) list(map(int,input().split())) c
n , k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
loop = []
visit = [(-1,-1) for i in range(n)]
cou = -1
for i in range(n):
    if visit[i] != (-1,-1):
        continue
    cou += 1
    tyu  = [i]
    now = i
    visit[i] = (cou,0)
    t = 1
    while True:
        nex = p[now] - 1
        if nex == i:
            break
        visit[nex] = (cou,t)
        tyu.append(nex)
        now = nex
        t += 1
    loop.append(tyu)

ans = []

for i in range(n):
    p = loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]]
    kar = [0]
    kouho = []
    losum = 0
    lokz = len(p)
    for j in p:
        losum += c[j]
        kar.append(kar[-1] + c[j])
    if losum >= 0:
        for l in range(lokz+1):
            if l > k:
                break
            cou = losum*((k-l)//lokz) + kar[l]
            kouho.append(cou)
    elif losum < 0:
        for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
    ans.append(max(kouho))
print(max(ans))
