N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

E=[]
color=["white" for _ in range(N)]
for i in range(N):
    if color[i]=="white":
        index,F=i,[]
        while 1:
            if color[index]=="white":
                color[index]="black"
                index=P[index]-1
                F.append(C[index])
            else:break
        E.append(F)

ans=-float("inf")
for A in E:
    length=len(A)
    if sum(A)<0:
        A=A*2
        for i in range(length):
            point=0
            for j in range(min(K,length)):
                point +=A[i+j]
                ans=max(ans,point)
    else:
        div,mod=divmod(K,length)
        base=(div-1)*sum(A)
        A=A*3
        for i in range(length):
            point=0
            for j in range(mod+length):
                point +=A[i+j]
                ans=max(ans,base+point)
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 E=[] E [] "white" for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) "white" color=["white" for _ in range(N)] color ["white" for _ in range(N)] for i in range(N):
    if color[i]=="white":
        index,F=i,[]
        while 1:
            if color[index]=="white":
                color[index]="black"
                index=P[index]-1
                F.append(C[index])
            else:break
        E.append(F) i range(N) range N if color[i]=="white":
        index,F=i,[]
        while 1:
            if color[index]=="white":
                color[index]="black"
                index=P[index]-1
                F.append(C[index])
            else:break
        E.append(F) color[i]=="white" color[i] color i "white" index,F=i,[] index i F [] while 1:
            if color[index]=="white":
                color[index]="black"
                index=P[index]-1
                F.append(C[index])
            else:break
         1 if color[index]=="white":
                color[index]="black"
                index=P[index]-1
                F.append(C[index])
            else:break
         color[index]=="white" color[index] color index "white" color[index]="black" color[index] color index "black" index=P[index]-1 index P[index]-1 P[index] P index 1 F.append(C[index]) F.append F append C[index] C index E.append(F) E.append E append F ans=-float("inf") ans -float("inf") float("inf") float "inf" for A in E:
    length=len(A)
    if sum(A)<0:
        A=A*2
        for i in range(length):
            point=0
            for j in range(min(K,length)):
                point +=A[i+j]
                ans=max(ans,point)
    else:
        div,mod=divmod(K,length)
        base=(div-1)*sum(A)
        A=A*3
        for i in range(length):
            point=0
            for j in range(mod+length):
                point +=A[i+j]
                ans=max(ans,base+point) A E length=len(A) length len(A) len A if sum(A)<0:
        A=A*2
        for i in range(length):
            point=0
            for j in range(min(K,length)):
                point +=A[i+j]
                ans=max(ans,point)
    else:
        div,mod=divmod(K,length)
        base=(div-1)*sum(A)
        A=A*3
        for i in range(length):
            point=0
            for j in range(mod+length):
                point +=A[i+j]
                ans=max(ans,base+point) sum(A)<0 sum(A) sum A 0 A=A*2 A A*2 A 2 for i in range(length):
            point=0
            for j in range(min(K,length)):
                point +=A[i+j]
                ans=max(ans,point)
     i range(length) range length point=0 point 0 for j in range(min(K,length)):
                point +=A[i+j]
                ans=max(ans,point)
     j range(min(K,length)) range min(K,length) min K length point +=A[i+j] point A[i+j] A i+j i j ans=max(ans,point) ans max(ans,point) max ans point div,mod=divmod(K,length) div divmod(K,length) divmod K length mod base=(div-1)*sum(A) base (div-1)*sum(A) div-1 div 1 sum(A) sum A A=A*3 A A*3 A 3 for i in range(length):
            point=0
            for j in range(mod+length):
                point +=A[i+j]
                ans=max(ans,base+point) i range(length) range length point=0 point 0 for j in range(mod+length):
                point +=A[i+j]
                ans=max(ans,base+point) j range(mod+length) range mod+length mod length point +=A[i+j] point A[i+j] A i+j i j ans=max(ans,base+point) ans max(ans,base+point) max ans base+point base point print(ans) print ans ans=max(ans,base+point) max(ans,base+point) ans C=list(map(int,input().split())) list(map(int,input().split())) C A=A*2 A*2 A E=[] [] E P=list(map(int,input().split())) list(map(int,input().split())) P point +=A[i+j] A[i+j] point index,F=i,[] i index base=(div-1)*sum(A) (div-1)*sum(A) base K=map(int,input().split()) map(int,input().split()) K ans=max(ans,point) max(ans,point) ans A=A*3 A*3 A point=0 0 point div,mod=divmod(K,length) divmod(K,length) div index=P[index]-1 P[index]-1 index F=i,[] [] F color=["white" for _ in range(N)] ["white" for _ in range(N)] color point +=A[i+j] A[i+j] point point=0 0 point length=len(A) len(A) length N,K=map(int,input().split()) map(int,input().split()) N ans=-float("inf") -float("inf") ans mod=divmod(K,length) divmod(K,length) mod
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

E=[]
color=["white" for _ in range(N)]
for i in range(N):
    if color[i]=="white":
        index,F=i,[]
        while 1:
            if color[index]=="white":
                color[index]="black"
                index=P[index]-1
                F.append(C[index])
            else:break
        E.append(F)

ans=-float("inf")
for A in E:
    length=len(A)
    if sum(A)<0:
        A=A*2
        for i in range(length):
            point=0
            for j in range(min(K,length)):
                point +=A[i+j]
                ans=max(ans,point)
    else:
        div,mod=divmod(K,length)
        base=(div-1)*sum(A)
        A=A*3
        for i in range(length):
            point=0
            for j in range(mod+length):
                point +=A[i+j]
                ans=max(ans,base+point)
print(ans)