n,k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

def sim(start,mx): # simulate mx step from start, returns max_score
    if mx==0:
        return 0
    now_p=0
    nxt=p[start]-1
    cnt=0
    p_lis=[]

    while True:
        nxt=p[nxt]-1
        now_p+=c[nxt]
        p_lis.append(now_p)
        cnt+=1
        if cnt==mx:
            break

    return max(p_lis)

visited_p=set() # already checked flag
cycle_score=[] # cycle loop score
cycle_points=[]
now_cycle=0

for i in range(n):
    if i in visited_p:
        continue
    visited_p.add(i)
    now_p=0
    nxt=p[i]-1
    visited=set()
    while True:
        nxt=p[nxt]-1
        visited_p.add(nxt)
        if nxt in visited:
            break
        visited.add(nxt)
        now_p+=c[nxt]
    cycle_score.append(now_p)
    cycle_points.append(list(visited))

ans=-float('inf')

for c_num in range(len(cycle_score)):
#    print(c_num,cycle_score[c_num],cycle_points[c_num])
    cycle_len=len(cycle_points[c_num])
    for start in cycle_points[c_num]:

        if cycle_score[c_num]<0: # no loop, just check min(loop_len,k)
            score = sim(start,min(cycle_len,k))
            ans=max(ans,score)

        else: # loop as many as possible
            loop_num = max(0,(k // cycle_len) - 1)
            nokori=k-loop_num*cycle_len
            score = sim(start,nokori) + loop_num*cycle_score[c_num]
            ans=max(ans,score)

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 def sim(start,mx): # simulate mx step from start, returns max_score
    if mx==0:
        return 0
    now_p=0
    nxt=p[start]-1
    cnt=0
    p_lis=[]

    while True:
        nxt=p[nxt]-1
        now_p+=c[nxt]
        p_lis.append(now_p)
        cnt+=1
        if cnt==mx:
            break

    return max(p_lis) sim if mx==0:
        return 0
     mx==0 mx 0 return 0 0 now_p=0 now_p 0 nxt=p[start]-1 nxt p[start]-1 p[start] p start 1 cnt=0 cnt 0 p_lis=[] p_lis [] while True:
        nxt=p[nxt]-1
        now_p+=c[nxt]
        p_lis.append(now_p)
        cnt+=1
        if cnt==mx:
            break

     True nxt=p[nxt]-1 nxt p[nxt]-1 p[nxt] p nxt 1 now_p+=c[nxt] now_p c[nxt] c nxt p_lis.append(now_p) p_lis.append p_lis append now_p cnt+=1 cnt 1 if cnt==mx:
            break

     cnt==mx cnt mx break return max(p_lis) max(p_lis) max p_lis start start mx mx visited_p=set() visited_p set() set cycle_score=[] cycle_score [] cycle_points=[] cycle_points [] now_cycle=0 now_cycle 0 for i in range(n):
    if i in visited_p:
        continue
    visited_p.add(i)
    now_p=0
    nxt=p[i]-1
    visited=set()
    while True:
        nxt=p[nxt]-1
        visited_p.add(nxt)
        if nxt in visited:
            break
        visited.add(nxt)
        now_p+=c[nxt]
    cycle_score.append(now_p)
    cycle_points.append(list(visited)) i range(n) range n if i in visited_p:
        continue
     i in visited_p i visited_p continue visited_p.add(i) visited_p.add visited_p add i now_p=0 now_p 0 nxt=p[i]-1 nxt p[i]-1 p[i] p i 1 visited=set() visited set() set while True:
        nxt=p[nxt]-1
        visited_p.add(nxt)
        if nxt in visited:
            break
        visited.add(nxt)
        now_p+=c[nxt]
     True nxt=p[nxt]-1 nxt p[nxt]-1 p[nxt] p nxt 1 visited_p.add(nxt) visited_p.add visited_p add nxt if nxt in visited:
            break
         nxt in visited nxt visited break visited.add(nxt) visited.add visited add nxt now_p+=c[nxt] now_p c[nxt] c nxt cycle_score.append(now_p) cycle_score.append cycle_score append now_p cycle_points.append(list(visited)) cycle_points.append cycle_points append list(visited) list visited ans=-float('inf') ans -float('inf') float('inf') float 'inf' for c_num in range(len(cycle_score)):
#    print(c_num,cycle_score[c_num],cycle_points[c_num])
    cycle_len=len(cycle_points[c_num])
    for start in cycle_points[c_num]:

        if cycle_score[c_num]<0: # no loop, just check min(loop_len,k)
            score = sim(start,min(cycle_len,k))
            ans=max(ans,score)

        else: # loop as many as possible
            loop_num = max(0,(k // cycle_len) - 1)
            nokori=k-loop_num*cycle_len
            score = sim(start,nokori) + loop_num*cycle_score[c_num]
            ans=max(ans,score) c_num range(len(cycle_score)) range len(cycle_score) len cycle_score cycle_len=len(cycle_points[c_num]) cycle_len len(cycle_points[c_num]) len cycle_points[c_num] cycle_points c_num for start in cycle_points[c_num]:

        if cycle_score[c_num]<0: # no loop, just check min(loop_len,k)
            score = sim(start,min(cycle_len,k))
            ans=max(ans,score)

        else: # loop as many as possible
            loop_num = max(0,(k // cycle_len) - 1)
            nokori=k-loop_num*cycle_len
            score = sim(start,nokori) + loop_num*cycle_score[c_num]
            ans=max(ans,score) start cycle_points[c_num] cycle_points c_num if cycle_score[c_num]<0: # no loop, just check min(loop_len,k)
            score = sim(start,min(cycle_len,k))
            ans=max(ans,score)

        else: # loop as many as possible
            loop_num = max(0,(k // cycle_len) - 1)
            nokori=k-loop_num*cycle_len
            score = sim(start,nokori) + loop_num*cycle_score[c_num]
            ans=max(ans,score) cycle_score[c_num]<0 cycle_score[c_num] cycle_score c_num 0 score = sim(start,min(cycle_len,k)) score sim(start,min(cycle_len,k)) sim start min(cycle_len,k) min cycle_len k ans=max(ans,score) ans max(ans,score) max ans score loop_num = max(0,(k // cycle_len) - 1) loop_num max(0,(k // cycle_len) - 1) max 0 (k // cycle_len) - 1 k // cycle_len k cycle_len 1 nokori=k-loop_num*cycle_len nokori k-loop_num*cycle_len k loop_num*cycle_len loop_num cycle_len score = sim(start,nokori) + loop_num*cycle_score[c_num] score sim(start,nokori) + loop_num*cycle_score[c_num] sim(start,nokori) sim start nokori loop_num*cycle_score[c_num] loop_num cycle_score[c_num] cycle_score c_num ans=max(ans,score) ans max(ans,score) max ans score print(ans) print ans nokori=k-loop_num*cycle_len k-loop_num*cycle_len nokori now_p=0 0 now_p ans=max(ans,score) max(ans,score) ans def sim(start,mx): # simulate mx step from start, returns max_score
    if mx==0:
        return 0
    now_p=0
    nxt=p[start]-1
    cnt=0
    p_lis=[]

    while True:
        nxt=p[nxt]-1
        now_p+=c[nxt]
        p_lis.append(now_p)
        cnt+=1
        if cnt==mx:
            break

    return max(p_lis) def sim(start,mx): # simulate mx step from start, returns max_score
    if mx==0:
        return 0
    now_p=0
    nxt=p[start]-1
    cnt=0
    p_lis=[]

    while True:
        nxt=p[nxt]-1
        now_p+=c[nxt]
        p_lis.append(now_p)
        cnt+=1
        if cnt==mx:
            break

    return max(p_lis) sim nxt=p[nxt]-1 p[nxt]-1 nxt score = sim(start,nokori) + loop_num*cycle_score[c_num] sim(start,nokori) + loop_num*cycle_score[c_num] score ans=max(ans,score) max(ans,score) ans cycle_points=[] [] cycle_points cycle_len=len(cycle_points[c_num]) len(cycle_points[c_num]) cycle_len nxt=p[i]-1 p[i]-1 nxt visited_p=set() set() visited_p now_cycle=0 0 now_cycle loop_num = max(0,(k // cycle_len) - 1) max(0,(k // cycle_len) - 1) loop_num c = list(map(int, input().split())) list(map(int, input().split())) c visited=set() set() visited k = map(int, input().split()) map(int, input().split()) k p = list(map(int, input().split())) list(map(int, input().split())) p ans=-float('inf') -float('inf') ans now_p+=c[nxt] c[nxt] now_p n,k = map(int, input().split()) map(int, input().split()) n cycle_score=[] [] cycle_score score = sim(start,min(cycle_len,k)) sim(start,min(cycle_len,k)) score
n,k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

def sim(start,mx): # simulate mx step from start, returns max_score
    if mx==0:
        return 0
    now_p=0
    nxt=p[start]-1
    cnt=0
    p_lis=[]

    while True:
        nxt=p[nxt]-1
        now_p+=c[nxt]
        p_lis.append(now_p)
        cnt+=1
        if cnt==mx:
            break

    return max(p_lis)

visited_p=set() # already checked flag
cycle_score=[] # cycle loop score
cycle_points=[]
now_cycle=0

for i in range(n):
    if i in visited_p:
        continue
    visited_p.add(i)
    now_p=0
    nxt=p[i]-1
    visited=set()
    while True:
        nxt=p[nxt]-1
        visited_p.add(nxt)
        if nxt in visited:
            break
        visited.add(nxt)
        now_p+=c[nxt]
    cycle_score.append(now_p)
    cycle_points.append(list(visited))

ans=-float('inf')

for c_num in range(len(cycle_score)):
#    print(c_num,cycle_score[c_num],cycle_points[c_num])
    cycle_len=len(cycle_points[c_num])
    for start in cycle_points[c_num]:

        if cycle_score[c_num]<0: # no loop, just check min(loop_len,k)
            score = sim(start,min(cycle_len,k))
            ans=max(ans,score)

        else: # loop as many as possible
            loop_num = max(0,(k // cycle_len) - 1)
            nokori=k-loop_num*cycle_len
            score = sim(start,nokori) + loop_num*cycle_score[c_num]
            ans=max(ans,score)

print(ans)

