N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
P=[i-1 for i in P]
idle_max=-10**27



for s in range(0,N):
    k=K-1
    score=0
    s=P[s]
    score+=C[s]
    count=1
    idle_max=max(idle_max,score)
    visited={s:[count,score]}
    while k and not P[s] in visited:

        k-=1
        count+=1
        s=P[s]
        score+=C[s]
        visited[s]=[count,score]

        idle_max=max(idle_max,score)
    #print(visited)
    if k:
        c1,score1=visited[s]
        c2,score2=visited[P[s]]
        c1+=1
        s=P[s]
        score1+=C[s]
        score+=C[s]
        k-=1


        n=c1-c2
        score+=(score1-score2)*(k//n)
        k%=n
        idle_max=max(idle_max,score)
    while k:
        k-=1
        s=P[s]
        score+=C[s]
        idle_max=max(idle_max,score)


print(idle_max) 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 i-1 for i in P for i in P i P for i in P i-1 i 1 P=[i-1 for i in P] P [i-1 for i in P] idle_max=-10**27 idle_max -10**27 -10 27 for s in range(0,N):
    k=K-1
    score=0
    s=P[s]
    score+=C[s]
    count=1
    idle_max=max(idle_max,score)
    visited={s:[count,score]}
    while k and not P[s] in visited:

        k-=1
        count+=1
        s=P[s]
        score+=C[s]
        visited[s]=[count,score]

        idle_max=max(idle_max,score)
    #print(visited)
    if k:
        c1,score1=visited[s]
        c2,score2=visited[P[s]]
        c1+=1
        s=P[s]
        score1+=C[s]
        score+=C[s]
        k-=1


        n=c1-c2
        score+=(score1-score2)*(k//n)
        k%=n
        idle_max=max(idle_max,score)
    while k:
        k-=1
        s=P[s]
        score+=C[s]
        idle_max=max(idle_max,score) s range(0,N) range 0 N k=K-1 k K-1 K 1 score=0 score 0 s=P[s] s P[s] P s score+=C[s] score C[s] C s count=1 count 1 idle_max=max(idle_max,score) idle_max max(idle_max,score) max idle_max score visited={s:[count,score]} visited {s:[count,score]} s s [count,score] count score while k and not P[s] in visited:

        k-=1
        count+=1
        s=P[s]
        score+=C[s]
        visited[s]=[count,score]

        idle_max=max(idle_max,score)
    #print(visited)
     k and not P[s] in visited k not P[s] in visited P[s] in visited P[s] P s visited k-=1 k 1 count+=1 count 1 s=P[s] s P[s] P s score+=C[s] score C[s] C s visited[s]=[count,score] visited[s] visited s [count,score] count score idle_max=max(idle_max,score) idle_max max(idle_max,score) max idle_max score if k:
        c1,score1=visited[s]
        c2,score2=visited[P[s]]
        c1+=1
        s=P[s]
        score1+=C[s]
        score+=C[s]
        k-=1


        n=c1-c2
        score+=(score1-score2)*(k//n)
        k%=n
        idle_max=max(idle_max,score)
     k c1,score1=visited[s] c1 visited[s] visited s score1 c2,score2=visited[P[s]] c2 visited[P[s]] visited P[s] P s score2 c1+=1 c1 1 s=P[s] s P[s] P s score1+=C[s] score1 C[s] C s score+=C[s] score C[s] C s k-=1 k 1 n=c1-c2 n c1-c2 c1 c2 score+=(score1-score2)*(k//n) score (score1-score2)*(k//n) score1-score2 score1 score2 k//n k n k%=n k n idle_max=max(idle_max,score) idle_max max(idle_max,score) max idle_max score while k:
        k-=1
        s=P[s]
        score+=C[s]
        idle_max=max(idle_max,score) k k-=1 k 1 s=P[s] s P[s] P s score+=C[s] score C[s] C s idle_max=max(idle_max,score) idle_max max(idle_max,score) max idle_max score print(idle_max) print idle_max score=0 0 score C=list(map(int,input().split())) list(map(int,input().split())) C k=K-1 K-1 k idle_max=max(idle_max,score) max(idle_max,score) idle_max N,K=map(int,input().split()) map(int,input().split()) N k-=1 1 k idle_max=max(idle_max,score) max(idle_max,score) idle_max P=[i-1 for i in P] [i-1 for i in P] P score+=(score1-score2)*(k//n) (score1-score2)*(k//n) score k%=n n k score1=visited[s] visited[s] score1 idle_max=max(idle_max,score) max(idle_max,score) idle_max score+=C[s] C[s] score idle_max=max(idle_max,score) max(idle_max,score) idle_max s=P[s] P[s] s n=c1-c2 c1-c2 n K=map(int,input().split()) map(int,input().split()) K c1,score1=visited[s] visited[s] c1 P=list(map(int,input().split())) list(map(int,input().split())) P score2=visited[P[s]] visited[P[s]] score2 k-=1 1 k score1+=C[s] C[s] score1 idle_max=-10**27 -10**27 idle_max c2,score2=visited[P[s]] visited[P[s]] c2 score+=C[s] C[s] score s=P[s] P[s] s score+=C[s] C[s] score c1+=1 1 c1 count+=1 1 count count=1 1 count k-=1 1 k s=P[s] P[s] s visited={s:[count,score]} {s:[count,score]} visited s=P[s] P[s] s score+=C[s] C[s] score
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
P=[i-1 for i in P]
idle_max=-10**27



for s in range(0,N):
    k=K-1
    score=0
    s=P[s]
    score+=C[s]
    count=1
    idle_max=max(idle_max,score)
    visited={s:[count,score]}
    while k and not P[s] in visited:

        k-=1
        count+=1
        s=P[s]
        score+=C[s]
        visited[s]=[count,score]

        idle_max=max(idle_max,score)
    #print(visited)
    if k:
        c1,score1=visited[s]
        c2,score2=visited[P[s]]
        c1+=1
        s=P[s]
        score1+=C[s]
        score+=C[s]
        k-=1


        n=c1-c2
        score+=(score1-score2)*(k//n)
        k%=n
        idle_max=max(idle_max,score)
    while k:
        k-=1
        s=P[s]
        score+=C[s]
        idle_max=max(idle_max,score)


print(idle_max)
