n,k=map(int,input().split())

p=list(map(int,input().split()))
score=list(map(int,input().split()))

group_score=[]
checked=[0]*n

for i in range(n):
    if checked[i]==0:
        tmp_group=[]
        tmp=i
        while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
        group_score.append(tmp_group)



#aryの中から連続するk個選択するときの最大
def calc(k):
    if k==0:
        return 0
    global group
    ans="INF"
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
    return ans

ans=score[0]
for group in group_score:
    #一周以上する場合
    if len(group)<k:
        cycle=sum(group)
        if cycle>0:
            cycle_sum=cycle*(k//len(group))
            tmp=calc(k%len(group))
            ans=max(ans,cycle_sum+tmp)

            if k//len(group)>=2:
                cycle_sum=cycle*(k//len(group)-1)
                tmp=calc(len(group))
                ans=max(ans,cycle_sum+tmp)
          

        tmp=calc(len(group))
        ans=max(ans,tmp)



    #一周しない場合k)
    else:
        tmp=calc(k)
        ans=max(ans,tmp)

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 score=list(map(int,input().split())) score list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split group_score=[] group_score [] checked=[0]*n checked [0]*n [0] 0 n for i in range(n):
    if checked[i]==0:
        tmp_group=[]
        tmp=i
        while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
        group_score.append(tmp_group)



#aryの中から連続するk個選択するときの最大 i range(n) range n if checked[i]==0:
        tmp_group=[]
        tmp=i
        while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
        group_score.append(tmp_group)



#aryの中から連続するk個選択するときの最大 checked[i]==0 checked[i] checked i 0 tmp_group=[] tmp_group [] tmp=i tmp i while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
         checked[tmp]==0 checked[tmp] checked tmp 0 checked[tmp]=1 checked[tmp] checked tmp 1 tmp_group.append(score[tmp]) tmp_group.append tmp_group append score[tmp] score tmp tmp=p[tmp]-1 tmp p[tmp]-1 p[tmp] p tmp 1 group_score.append(tmp_group) group_score.append group_score append tmp_group def calc(k):
    if k==0:
        return 0
    global group
    ans="INF"
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
    return ans calc if k==0:
        return 0
     k==0 k 0 return 0 0 global group ans="INF" ans "INF" for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
     i range(len(group)) range len(group) len group tmp=0 tmp 0 for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
     j range(k) range k tmp+=group[(i+j)%len(group)] tmp group[(i+j)%len(group)] group (i+j)%len(group) i+j i j len(group) len group if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
     ans=="INF" ans "INF" ans=tmp ans tmp if ans<tmp:
                    ans=tmp
     ans<tmp ans tmp ans=tmp ans tmp return ans ans k k ans=score[0] ans score[0] score 0 for group in group_score:
    #一周以上する場合
    if len(group)<k:
        cycle=sum(group)
        if cycle>0:
            cycle_sum=cycle*(k//len(group))
            tmp=calc(k%len(group))
            ans=max(ans,cycle_sum+tmp)

            if k//len(group)>=2:
                cycle_sum=cycle*(k//len(group)-1)
                tmp=calc(len(group))
                ans=max(ans,cycle_sum+tmp)
          

        tmp=calc(len(group))
        ans=max(ans,tmp)



    #一周しない場合k)
    else:
        tmp=calc(k)
        ans=max(ans,tmp) group group_score if len(group)<k:
        cycle=sum(group)
        if cycle>0:
            cycle_sum=cycle*(k//len(group))
            tmp=calc(k%len(group))
            ans=max(ans,cycle_sum+tmp)

            if k//len(group)>=2:
                cycle_sum=cycle*(k//len(group)-1)
                tmp=calc(len(group))
                ans=max(ans,cycle_sum+tmp)
          

        tmp=calc(len(group))
        ans=max(ans,tmp)



    #一周しない場合k)
    else:
        tmp=calc(k)
        ans=max(ans,tmp) len(group)<k len(group) len group k cycle=sum(group) cycle sum(group) sum group if cycle>0:
            cycle_sum=cycle*(k//len(group))
            tmp=calc(k%len(group))
            ans=max(ans,cycle_sum+tmp)

            if k//len(group)>=2:
                cycle_sum=cycle*(k//len(group)-1)
                tmp=calc(len(group))
                ans=max(ans,cycle_sum+tmp)
          

         cycle>0 cycle 0 cycle_sum=cycle*(k//len(group)) cycle_sum cycle*(k//len(group)) cycle k//len(group) k len(group) len group tmp=calc(k%len(group)) tmp calc(k%len(group)) calc k%len(group) k len(group) len group ans=max(ans,cycle_sum+tmp) ans max(ans,cycle_sum+tmp) max ans cycle_sum+tmp cycle_sum tmp if k//len(group)>=2:
                cycle_sum=cycle*(k//len(group)-1)
                tmp=calc(len(group))
                ans=max(ans,cycle_sum+tmp)
          

         k//len(group)>=2 k//len(group) k len(group) len group 2 cycle_sum=cycle*(k//len(group)-1) cycle_sum cycle*(k//len(group)-1) cycle k//len(group)-1 k//len(group) k len(group) len group 1 tmp=calc(len(group)) tmp calc(len(group)) calc len(group) len group ans=max(ans,cycle_sum+tmp) ans max(ans,cycle_sum+tmp) max ans cycle_sum+tmp cycle_sum tmp tmp=calc(len(group)) tmp calc(len(group)) calc len(group) len group ans=max(ans,tmp) ans max(ans,tmp) max ans tmp tmp=calc(k) tmp calc(k) calc k ans=max(ans,tmp) ans max(ans,tmp) max ans tmp print(ans) print ans ans=max(ans,tmp) max(ans,tmp) ans tmp=p[tmp]-1 p[tmp]-1 tmp ans=max(ans,cycle_sum+tmp) max(ans,cycle_sum+tmp) ans ans=max(ans,tmp) max(ans,tmp) ans tmp=calc(k%len(group)) calc(k%len(group)) tmp ans=max(ans,cycle_sum+tmp) max(ans,cycle_sum+tmp) ans k=map(int,input().split()) map(int,input().split()) k cycle_sum=cycle*(k//len(group)) cycle*(k//len(group)) cycle_sum checked=[0]*n [0]*n checked ans=score[0] score[0] ans cycle_sum=cycle*(k//len(group)-1) cycle*(k//len(group)-1) cycle_sum p=list(map(int,input().split())) list(map(int,input().split())) p n,k=map(int,input().split()) map(int,input().split()) n tmp=calc(len(group)) calc(len(group)) tmp def calc(k):
    if k==0:
        return 0
    global group
    ans="INF"
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
    return ans def calc(k):
    if k==0:
        return 0
    global group
    ans="INF"
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
    return ans calc tmp=i i tmp score=list(map(int,input().split())) list(map(int,input().split())) score tmp=calc(k) calc(k) tmp group_score=[] [] group_score tmp_group=[] [] tmp_group tmp=calc(len(group)) calc(len(group)) tmp cycle=sum(group) sum(group) cycle
n,k=map(int,input().split())

p=list(map(int,input().split()))
score=list(map(int,input().split()))

group_score=[]
checked=[0]*n

for i in range(n):
    if checked[i]==0:
        tmp_group=[]
        tmp=i
        while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
        group_score.append(tmp_group)



#aryの中から連続するk個選択するときの最大
def calc(k):
    if k==0:
        return 0
    global group
    ans="INF"
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans=="INF":
                ans=tmp
            else:
                if ans<tmp:
                    ans=tmp
    return ans

ans=score[0]
for group in group_score:
    #一周以上する場合
    if len(group)<k:
        cycle=sum(group)
        if cycle>0:
            cycle_sum=cycle*(k//len(group))
            tmp=calc(k%len(group))
            ans=max(ans,cycle_sum+tmp)

            if k//len(group)>=2:
                cycle_sum=cycle*(k//len(group)-1)
                tmp=calc(len(group))
                ans=max(ans,cycle_sum+tmp)
          

        tmp=calc(len(group))
        ans=max(ans,tmp)



    #一周しない場合k)
    else:
        tmp=calc(k)
        ans=max(ans,tmp)

print(ans)