class UnionFind():
    def __init__(self,n):
        self.n=n
        self.root=[-1]*n
        self.rank=[-1]*n

    def Find_Root(self,x):
        if self.root[x]<0:return x
        else:
            self.root[x]=self.Find_Root(self.root[x])
            return self.root[x]

    def Unite(self,x,y):
        x=self.Find_Root(x)
        y=self.Find_Root(y)

        if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

    def isSameGroup(self,x,y):
        return self.Find_Root(x)==self.Find_Root(y)

    def size(self,x):
        return -self.root[self.Find_Root(x)]

    def members(self,x):
        root=self.Find_Root(x)
        return [i for i in range(self.n) if self.Find_Root(i)==root]

    def roots(self):
        return[i for i,j in enumerate(self.root) if j<0]

    def group_members(self):
        return {i:self.members(i) for i in self.roots()}

    def group_count(self):
        return len(self.roots())


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

UnionFind=UnionFind(N+1)
for i in range(N):
    UnionFind.Unite(i+1,P[i])

D=UnionFind.group_members()
E=[]
for i in D:
    A,length=D[i],len(D[i])
    if A[0]==0:continue
    index,F=A[0]-1,[]
    while len(F)!=length:
        index=P[index]-1
        F.append(C[index])
    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) class UnionFind():
    def __init__(self,n):
        self.n=n
        self.root=[-1]*n
        self.rank=[-1]*n

    def Find_Root(self,x):
        if self.root[x]<0:return x
        else:
            self.root[x]=self.Find_Root(self.root[x])
            return self.root[x]

    def Unite(self,x,y):
        x=self.Find_Root(x)
        y=self.Find_Root(y)

        if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

    def isSameGroup(self,x,y):
        return self.Find_Root(x)==self.Find_Root(y)

    def size(self,x):
        return -self.root[self.Find_Root(x)]

    def members(self,x):
        root=self.Find_Root(x)
        return [i for i in range(self.n) if self.Find_Root(i)==root]

    def roots(self):
        return[i for i,j in enumerate(self.root) if j<0]

    def group_members(self):
        return {i:self.members(i) for i in self.roots()}

    def group_count(self):
        return len(self.roots()) UnionFind def __init__(self,n):
        self.n=n
        self.root=[-1]*n
        self.rank=[-1]*n

     __init__ self.n=n self.n self n n self.root=[-1]*n self.root self root [-1]*n [-1] -1 n self.rank=[-1]*n self.rank self rank [-1]*n [-1] -1 n self self n n def Find_Root(self,x):
        if self.root[x]<0:return x
        else:
            self.root[x]=self.Find_Root(self.root[x])
            return self.root[x]

     Find_Root if self.root[x]<0:return x
        else:
            self.root[x]=self.Find_Root(self.root[x])
            return self.root[x]

     self.root[x]<0 self.root[x] self.root self root x 0 self.root[x]=self.Find_Root(self.root[x]) self.root[x] self.root self root x self.Find_Root(self.root[x]) self.Find_Root self Find_Root self.root[x] self.root self root x return self.root[x] self.root[x] self.root self root x self self x x def Unite(self,x,y):
        x=self.Find_Root(x)
        y=self.Find_Root(y)

        if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

     Unite x=self.Find_Root(x) x self.Find_Root(x) self.Find_Root self Find_Root x y=self.Find_Root(y) y self.Find_Root(y) self.Find_Root self Find_Root y if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

     x==y x y elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
         self.rank[x]>self.rank[y] self.rank[x] self.rank self rank x self.rank[y] self.rank self rank y self.root[x] +=self.root[y] self.root[x] self.root self root x self.root[y] self.root self root y self.root[y]=x self.root[y] self.root self root y x self.root[y] +=self.root[x] self.root[y] self.root self root y self.root[x] self.root self root x self.root[x]=y self.root[x] self.root self root x y if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

     self.rank[x]==self.rank[y] self.rank[x] self.rank self rank x self.rank[y] self.rank self rank y self.rank[y] +=1 self.rank[y] self.rank self rank y 1 self self x x y y def isSameGroup(self,x,y):
        return self.Find_Root(x)==self.Find_Root(y)

     isSameGroup return self.Find_Root(x)==self.Find_Root(y) self.Find_Root(x)==self.Find_Root(y) self.Find_Root(x) self.Find_Root self Find_Root x self.Find_Root(y) self.Find_Root self Find_Root y self self x x y y def size(self,x):
        return -self.root[self.Find_Root(x)]

     size return -self.root[self.Find_Root(x)] -self.root[self.Find_Root(x)] self.root[self.Find_Root(x)] self.root self root self.Find_Root(x) self.Find_Root self Find_Root x self self x x def members(self,x):
        root=self.Find_Root(x)
        return [i for i in range(self.n) if self.Find_Root(i)==root]

     members root=self.Find_Root(x) root self.Find_Root(x) self.Find_Root self Find_Root x i for i in range(self.n) if self.Find_Root(i)==root for i in range(self.n) if self.Find_Root(i)==root i range(self.n) range self.n self n self.Find_Root(i)==root self.Find_Root(i) self.Find_Root self Find_Root i root if self.Find_Root(i)==root i return [i for i in range(self.n) if self.Find_Root(i)==root] [i for i in range(self.n) if self.Find_Root(i)==root] self self x x def roots(self):
        return[i for i,j in enumerate(self.root) if j<0]

     roots i for i,j in enumerate(self.root) if j<0 for i,j in enumerate(self.root) if j<0 i j enumerate(self.root) enumerate self.root self root j<0 j 0 if j<0 i return[i for i,j in enumerate(self.root) if j<0] [i for i,j in enumerate(self.root) if j<0] self self def group_members(self):
        return {i:self.members(i) for i in self.roots()}

     group_members i:self.members(i) for i in self.roots() for i in self.roots() i self.roots() self.roots self roots for i in self.roots() i self.members(i) self.members self members i return {i:self.members(i) for i in self.roots()} {i:self.members(i) for i in self.roots()} self self def group_count(self):
        return len(self.roots()) group_count return len(self.roots()) len(self.roots()) len self.roots() self.roots self roots self self 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 UnionFind=UnionFind(N+1) UnionFind UnionFind(N+1) UnionFind N+1 N 1 for i in range(N):
    UnionFind.Unite(i+1,P[i]) i range(N) range N UnionFind.Unite(i+1,P[i]) UnionFind.Unite UnionFind Unite i+1 i 1 P[i] P i D=UnionFind.group_members() D UnionFind.group_members() UnionFind.group_members UnionFind group_members E=[] E [] for i in D:
    A,length=D[i],len(D[i])
    if A[0]==0:continue
    index,F=A[0]-1,[]
    while len(F)!=length:
        index=P[index]-1
        F.append(C[index])
    E.append(F) i D A,length=D[i],len(D[i]) A D[i] D i length len(D[i]) len D[i] D i if A[0]==0:continue
     A[0]==0 A[0] A 0 0 index,F=A[0]-1,[] index A[0]-1 A[0] A 0 1 F [] while len(F)!=length:
        index=P[index]-1
        F.append(C[index])
     len(F)!=length len(F) len F length 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,point) max(ans,point) ans index,F=A[0]-1,[] A[0]-1 index length=D[i],len(D[i]) len(D[i]) length point +=A[i+j] A[i+j] point ans=max(ans,base+point) max(ans,base+point) ans class UnionFind():
    def __init__(self,n):
        self.n=n
        self.root=[-1]*n
        self.rank=[-1]*n

    def Find_Root(self,x):
        if self.root[x]<0:return x
        else:
            self.root[x]=self.Find_Root(self.root[x])
            return self.root[x]

    def Unite(self,x,y):
        x=self.Find_Root(x)
        y=self.Find_Root(y)

        if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

    def isSameGroup(self,x,y):
        return self.Find_Root(x)==self.Find_Root(y)

    def size(self,x):
        return -self.root[self.Find_Root(x)]

    def members(self,x):
        root=self.Find_Root(x)
        return [i for i in range(self.n) if self.Find_Root(i)==root]

    def roots(self):
        return[i for i,j in enumerate(self.root) if j<0]

    def group_members(self):
        return {i:self.members(i) for i in self.roots()}

    def group_count(self):
        return len(self.roots()) class UnionFind():
    def __init__(self,n):
        self.n=n
        self.root=[-1]*n
        self.rank=[-1]*n

    def Find_Root(self,x):
        if self.root[x]<0:return x
        else:
            self.root[x]=self.Find_Root(self.root[x])
            return self.root[x]

    def Unite(self,x,y):
        x=self.Find_Root(x)
        y=self.Find_Root(y)

        if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

    def isSameGroup(self,x,y):
        return self.Find_Root(x)==self.Find_Root(y)

    def size(self,x):
        return -self.root[self.Find_Root(x)]

    def members(self,x):
        root=self.Find_Root(x)
        return [i for i in range(self.n) if self.Find_Root(i)==root]

    def roots(self):
        return[i for i,j in enumerate(self.root) if j<0]

    def group_members(self):
        return {i:self.members(i) for i in self.roots()}

    def group_count(self):
        return len(self.roots()) UnionFind point=0 0 point P=list(map(int,input().split())) list(map(int,input().split())) P index=P[index]-1 P[index]-1 index base=(div-1)*sum(A) (div-1)*sum(A) base ans=-float("inf") -float("inf") ans A=A*2 A*2 A A=A*3 A*3 A A,length=D[i],len(D[i]) D[i] A point=0 0 point mod=divmod(K,length) divmod(K,length) mod div,mod=divmod(K,length) divmod(K,length) div N,K=map(int,input().split()) map(int,input().split()) N K=map(int,input().split()) map(int,input().split()) K E=[] [] E F=A[0]-1,[] [] F UnionFind=UnionFind(N+1) UnionFind(N+1) UnionFind length=len(A) len(A) length point +=A[i+j] A[i+j] point C=list(map(int,input().split())) list(map(int,input().split())) C D=UnionFind.group_members() UnionFind.group_members() D
class UnionFind():
    def __init__(self,n):
        self.n=n
        self.root=[-1]*n
        self.rank=[-1]*n

    def Find_Root(self,x):
        if self.root[x]<0:return x
        else:
            self.root[x]=self.Find_Root(self.root[x])
            return self.root[x]

    def Unite(self,x,y):
        x=self.Find_Root(x)
        y=self.Find_Root(y)

        if x==y:return
        elif self.rank[x]>self.rank[y]:
            self.root[x] +=self.root[y]
            self.root[y]=x
        else:
            self.root[y] +=self.root[x]
            self.root[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y] +=1

    def isSameGroup(self,x,y):
        return self.Find_Root(x)==self.Find_Root(y)

    def size(self,x):
        return -self.root[self.Find_Root(x)]

    def members(self,x):
        root=self.Find_Root(x)
        return [i for i in range(self.n) if self.Find_Root(i)==root]

    def roots(self):
        return[i for i,j in enumerate(self.root) if j<0]

    def group_members(self):
        return {i:self.members(i) for i in self.roots()}

    def group_count(self):
        return len(self.roots())


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

UnionFind=UnionFind(N+1)
for i in range(N):
    UnionFind.Unite(i+1,P[i])

D=UnionFind.group_members()
E=[]
for i in D:
    A,length=D[i],len(D[i])
    if A[0]==0:continue
    index,F=A[0]-1,[]
    while len(F)!=length:
        index=P[index]-1
        F.append(C[index])
    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)