class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2)

N,K=map(int,input().split())
P=[int(i)-1 for i in input().split()]
C=[int(i) for i in input().split()]
G=UnionFind(N)
for i in range(N):
    G.unite(i,P[i])
D=dict()
for i in range(N):
    if G.find(i) in D:
        D[G.find(i)]+=1
    else:
        D[G.find(i)]=1

dp=[[0 for j in range(N)] for i in range(N+1)]
for j in range(N):
    dp[0][j]=j
for i in range(N):
    for j in range(N):
        dp[i+1][j]=P[dp[i][j]]
#1 3 4 0 2
#0->1->3->0, 2->4->2
score=[[0 for j in range(N)] for i in range(N)]
for i in range(N):
    for j in range(N):
        score[i][j]=score[i-1][j]+C[dp[i+1][j]]
def maxpoint(j,L):
    if L<=N:
        return max([score[i][j] for i in range(L)])
    else:
        roop=D[G.find(j)]
        cyclepoint=score[roop-1][j]
        if cyclepoint<0:
            return maxpoint(j,roop)
        else:
            if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint
ans=[maxpoint(j,K) for j in range(N)]
print(max(ans)) class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2) UnionFind n=1 n 1 par=[0] par [0] 0 rnk=[0] rnk [0] 0 def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
     __init__ self.n=size self.n self n size i for i in range(self.n) for i in range(self.n) i range(self.n) range self.n self n for i in range(self.n) i self.par=[i for i in range(self.n)] self.par self par [i for i in range(self.n)] 0 for i in range(self.n) for i in range(self.n) i range(self.n) range self.n self n for i in range(self.n) 0 self.rnk=[0 for i in range(self.n)] self.rnk self rnk [0 for i in range(self.n)] self self size size def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
     find if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
     self.par[vertex1]==vertex1 self.par[vertex1] self.par self par vertex1 vertex1 return vertex1 vertex1 self.par[vertex1]=self.find(self.par[vertex1]) self.par[vertex1] self.par self par vertex1 self.find(self.par[vertex1]) self.find self find self.par[vertex1] self.par self par vertex1 return self.par[vertex1] self.par[vertex1] self.par self par vertex1 self self vertex1 vertex1 def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
     unite vertex1=self.find(vertex1) vertex1 self.find(vertex1) self.find self find vertex1 vertex2=self.find(vertex2) vertex2 self.find(vertex2) self.find self find vertex2 if vertex1==vertex2:
            return
         vertex1==vertex2 vertex1 vertex2 return if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
     self.rnk[vertex1]<self.rnk[vertex2] self.rnk[vertex1] self.rnk self rnk vertex1 self.rnk[vertex2] self.rnk self rnk vertex2 self.par[vertex1]=vertex2 self.par[vertex1] self.par self par vertex1 vertex2 self.par[vertex2]=vertex1 self.par[vertex2] self.par self par vertex2 vertex1 if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
     self.rnk[vertex1]==self.rnk[vertex2] self.rnk[vertex1] self.rnk self rnk vertex1 self.rnk[vertex2] self.rnk self rnk vertex2 self.rnk[vertex1]+=1 self.rnk[vertex1] self.rnk self rnk vertex1 1 self self vertex1 vertex1 vertex2 vertex2 def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2) same return self.find(vetrex1)==self.find(vertex2) self.find(vetrex1)==self.find(vertex2) self.find(vetrex1) self.find self find vetrex1 self.find(vertex2) self.find self find vertex2 self self vetrex1 vetrex1 vertex2 vertex2 N,K=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 P=[int(i)-1 for i in input().split()] P [int(i)-1 for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i C=[int(i) for i in input().split()] C [int(i) for i in input().split()] G=UnionFind(N) G UnionFind(N) UnionFind N for i in range(N):
    G.unite(i,P[i]) i range(N) range N G.unite(i,P[i]) G.unite G unite i P[i] P i D=dict() D dict() dict for i in range(N):
    if G.find(i) in D:
        D[G.find(i)]+=1
    else:
        D[G.find(i)]=1 i range(N) range N if G.find(i) in D:
        D[G.find(i)]+=1
    else:
        D[G.find(i)]=1 G.find(i) in D G.find(i) G.find G find i D D[G.find(i)]+=1 D[G.find(i)] D G.find(i) G.find G find i 1 D[G.find(i)]=1 D[G.find(i)] D G.find(i) G.find G find i 1 [0 for j in range(N)] for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) [0 for j in range(N)] dp=[[0 for j in range(N)] for i in range(N+1)] dp [[0 for j in range(N)] for i in range(N+1)] for j in range(N):
    dp[0][j]=j j range(N) range N dp[0][j]=j dp[0][j] [0] dp 0 j j for i in range(N):
    for j in range(N):
        dp[i+1][j]=P[dp[i][j]]
#1 3 4 0 2
#0->1->3->0, 2->4->2 i range(N) range N for j in range(N):
        dp[i+1][j]=P[dp[i][j]]
#1 3 4 0 2
#0->1->3->0, 2->4->2 j range(N) range N dp[i+1][j]=P[dp[i][j]] dp[i+1][j] [i+1] dp i+1 i 1 j P[dp[i][j]] P dp[i][j] [i] dp i j [0 for j in range(N)] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [0 for j in range(N)] score=[[0 for j in range(N)] for i in range(N)] score [[0 for j in range(N)] for i in range(N)] for i in range(N):
    for j in range(N):
        score[i][j]=score[i-1][j]+C[dp[i+1][j]] i range(N) range N for j in range(N):
        score[i][j]=score[i-1][j]+C[dp[i+1][j]] j range(N) range N score[i][j]=score[i-1][j]+C[dp[i+1][j]] score[i][j] [i] score i j score[i-1][j]+C[dp[i+1][j]] score[i-1][j] [i-1] score i-1 i 1 j C[dp[i+1][j]] C dp[i+1][j] [i+1] dp i+1 i 1 j def maxpoint(j,L):
    if L<=N:
        return max([score[i][j] for i in range(L)])
    else:
        roop=D[G.find(j)]
        cyclepoint=score[roop-1][j]
        if cyclepoint<0:
            return maxpoint(j,roop)
        else:
            if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint maxpoint if L<=N:
        return max([score[i][j] for i in range(L)])
    else:
        roop=D[G.find(j)]
        cyclepoint=score[roop-1][j]
        if cyclepoint<0:
            return maxpoint(j,roop)
        else:
            if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint L<=N L N score[i][j] for i in range(L) for i in range(L) i range(L) range L for i in range(L) score[i][j] [i] score i j return max([score[i][j] for i in range(L)]) max([score[i][j] for i in range(L)]) max [score[i][j] for i in range(L)] roop=D[G.find(j)] roop D[G.find(j)] D G.find(j) G.find G find j cyclepoint=score[roop-1][j] cyclepoint score[roop-1][j] [roop-1] score roop-1 roop 1 j if cyclepoint<0:
            return maxpoint(j,roop)
        else:
            if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint cyclepoint<0 cyclepoint 0 return maxpoint(j,roop) maxpoint(j,roop) maxpoint j roop if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint L%roop!=0 L%roop L roop 0 return maxpoint(j,L%roop)+(L//roop)*cyclepoint maxpoint(j,L%roop)+(L//roop)*cyclepoint maxpoint(j,L%roop) maxpoint j L%roop L roop (L//roop)*cyclepoint L//roop L roop cyclepoint return maxpoint(j,roop)+(-1+L//roop)*cyclepoint maxpoint(j,roop)+(-1+L//roop)*cyclepoint maxpoint(j,roop) maxpoint j roop (-1+L//roop)*cyclepoint -1+L//roop -1 L//roop L roop cyclepoint j j L L maxpoint(j,K) for j in range(N) for j in range(N) j range(N) range N for j in range(N) maxpoint(j,K) maxpoint j K ans=[maxpoint(j,K) for j in range(N)] ans [maxpoint(j,K) for j in range(N)] print(max(ans)) print max(ans) max ans D=dict() dict() D P=[int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] P C=[int(i) for i in input().split()] [int(i) for i in input().split()] C def maxpoint(j,L):
    if L<=N:
        return max([score[i][j] for i in range(L)])
    else:
        roop=D[G.find(j)]
        cyclepoint=score[roop-1][j]
        if cyclepoint<0:
            return maxpoint(j,roop)
        else:
            if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint def maxpoint(j,L):
    if L<=N:
        return max([score[i][j] for i in range(L)])
    else:
        roop=D[G.find(j)]
        cyclepoint=score[roop-1][j]
        if cyclepoint<0:
            return maxpoint(j,roop)
        else:
            if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint maxpoint score=[[0 for j in range(N)] for i in range(N)] [[0 for j in range(N)] for i in range(N)] score ans=[maxpoint(j,K) for j in range(N)] [maxpoint(j,K) for j in range(N)] ans class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2) class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2) UnionFind N,K=map(int,input().split()) map(int,input().split()) N G=UnionFind(N) UnionFind(N) G dp=[[0 for j in range(N)] for i in range(N+1)] [[0 for j in range(N)] for i in range(N+1)] dp K=map(int,input().split()) map(int,input().split()) K
class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2)

N,K=map(int,input().split())
P=[int(i)-1 for i in input().split()]
C=[int(i) for i in input().split()]
G=UnionFind(N)
for i in range(N):
    G.unite(i,P[i])
D=dict()
for i in range(N):
    if G.find(i) in D:
        D[G.find(i)]+=1
    else:
        D[G.find(i)]=1

dp=[[0 for j in range(N)] for i in range(N+1)]
for j in range(N):
    dp[0][j]=j
for i in range(N):
    for j in range(N):
        dp[i+1][j]=P[dp[i][j]]
#1 3 4 0 2
#0->1->3->0, 2->4->2
score=[[0 for j in range(N)] for i in range(N)]
for i in range(N):
    for j in range(N):
        score[i][j]=score[i-1][j]+C[dp[i+1][j]]
def maxpoint(j,L):
    if L<=N:
        return max([score[i][j] for i in range(L)])
    else:
        roop=D[G.find(j)]
        cyclepoint=score[roop-1][j]
        if cyclepoint<0:
            return maxpoint(j,roop)
        else:
            if L%roop!=0:
                return maxpoint(j,L%roop)+(L//roop)*cyclepoint
            else:
                return maxpoint(j,roop)+(-1+L//roop)*cyclepoint
ans=[maxpoint(j,K) for j in range(N)]
print(max(ans))
