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]


groups = []
is_appear = [False]*n
group_size = [0]*n
for i in range(n):
    if is_appear[i]:
        continue
    group = []
    group.append(i)
    is_appear[i] = True
    j = p[i]
    while j != i:
        group.append(j)
        is_appear[j] = True
        j = p[j]
        
    groups.append(group)
    
    for i in group:
        group_size[i] = len(group)


cycle_score = [0]*n
for g in groups:
    score = 0
    for i in range(len(g)):
        score += c[g[i]]
    
    for i in g:
        cycle_score[i] = score

best = -float('inf')        
for i in range(n):
    score = 0
    best_i = -float('inf')
    cyc_num = k//group_size[i]
    if cycle_score[i]>0 and cyc_num>=1:
        remain = k-group_size[i]*(cyc_num-1)
        score += cycle_score[i]*(cyc_num-1)
        
    elif cycle_score[i]<=0:
        remain = group_size[i]
    elif cyc_num<1:
        remain = k
    
    j = i
    for _ in range(remain):
        j = p[j]
        score += c[j]
        best_i = max(best_i,score)
    best = max(best,best_i)
    
print(best) 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] groups = [] groups [] is_appear = [False]*n is_appear [False]*n [False] False n group_size = [0]*n group_size [0]*n [0] 0 n for i in range(n):
    if is_appear[i]:
        continue
    group = []
    group.append(i)
    is_appear[i] = True
    j = p[i]
    while j != i:
        group.append(j)
        is_appear[j] = True
        j = p[j]
        
    groups.append(group)
    
    for i in group:
        group_size[i] = len(group) i range(n) range n if is_appear[i]:
        continue
     is_appear[i] is_appear i continue group = [] group [] group.append(i) group.append group append i is_appear[i] = True is_appear[i] is_appear i True j = p[i] j p[i] p i while j != i:
        group.append(j)
        is_appear[j] = True
        j = p[j]
        
     j != i j i group.append(j) group.append group append j is_appear[j] = True is_appear[j] is_appear j True j = p[j] j p[j] p j groups.append(group) groups.append groups append group for i in group:
        group_size[i] = len(group) i group group_size[i] = len(group) group_size[i] group_size i len(group) len group cycle_score = [0]*n cycle_score [0]*n [0] 0 n for g in groups:
    score = 0
    for i in range(len(g)):
        score += c[g[i]]
    
    for i in g:
        cycle_score[i] = score g groups score = 0 score 0 for i in range(len(g)):
        score += c[g[i]]
    
     i range(len(g)) range len(g) len g score += c[g[i]] score c[g[i]] c g[i] g i for i in g:
        cycle_score[i] = score i g cycle_score[i] = score cycle_score[i] cycle_score i score best = -float('inf') best -float('inf') float('inf') float 'inf' for i in range(n):
    score = 0
    best_i = -float('inf')
    cyc_num = k//group_size[i]
    if cycle_score[i]>0 and cyc_num>=1:
        remain = k-group_size[i]*(cyc_num-1)
        score += cycle_score[i]*(cyc_num-1)
        
    elif cycle_score[i]<=0:
        remain = group_size[i]
    elif cyc_num<1:
        remain = k
    
    j = i
    for _ in range(remain):
        j = p[j]
        score += c[j]
        best_i = max(best_i,score)
    best = max(best,best_i)
     i range(n) range n score = 0 score 0 best_i = -float('inf') best_i -float('inf') float('inf') float 'inf' cyc_num = k//group_size[i] cyc_num k//group_size[i] k group_size[i] group_size i if cycle_score[i]>0 and cyc_num>=1:
        remain = k-group_size[i]*(cyc_num-1)
        score += cycle_score[i]*(cyc_num-1)
        
    elif cycle_score[i]<=0:
        remain = group_size[i]
    elif cyc_num<1:
        remain = k
    
     cycle_score[i]>0 and cyc_num>=1 cycle_score[i]>0 cycle_score[i] cycle_score i 0 cyc_num>=1 cyc_num 1 remain = k-group_size[i]*(cyc_num-1) remain k-group_size[i]*(cyc_num-1) k group_size[i]*(cyc_num-1) group_size[i] group_size i cyc_num-1 cyc_num 1 score += cycle_score[i]*(cyc_num-1) score cycle_score[i]*(cyc_num-1) cycle_score[i] cycle_score i cyc_num-1 cyc_num 1 elif cycle_score[i]<=0:
        remain = group_size[i]
     cycle_score[i]<=0 cycle_score[i] cycle_score i 0 remain = group_size[i] remain group_size[i] group_size i elif cyc_num<1:
        remain = k
    
     cyc_num<1 cyc_num 1 remain = k remain k j = i j i for _ in range(remain):
        j = p[j]
        score += c[j]
        best_i = max(best_i,score)
     _ range(remain) range remain j = p[j] j p[j] p j score += c[j] score c[j] c j best_i = max(best_i,score) best_i max(best_i,score) max best_i score best = max(best,best_i) best max(best,best_i) max best best_i print(best) print best remain = group_size[i] group_size[i] remain k = map(int,input().split()) map(int,input().split()) k j = i i j score += c[j] c[j] score score = 0 0 score is_appear = [False]*n [False]*n is_appear group = [] [] group remain = k-group_size[i]*(cyc_num-1) k-group_size[i]*(cyc_num-1) remain j = p[j] p[j] j score += c[g[i]] c[g[i]] score groups = [] [] groups j = p[i] p[i] j best = max(best,best_i) max(best,best_i) best n,k = map(int,input().split()) map(int,input().split()) n remain = k k remain j = p[j] p[j] j best_i = max(best_i,score) max(best_i,score) best_i best_i = -float('inf') -float('inf') best_i cyc_num = k//group_size[i] k//group_size[i] cyc_num cycle_score = [0]*n [0]*n cycle_score p = [i-1 for i in p] [i-1 for i in p] p score = 0 0 score best = -float('inf') -float('inf') best c = list(map(int,input().split())) list(map(int,input().split())) c group_size = [0]*n [0]*n group_size p = list(map(int,input().split())) list(map(int,input().split())) p score += cycle_score[i]*(cyc_num-1) cycle_score[i]*(cyc_num-1) 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]


groups = []
is_appear = [False]*n
group_size = [0]*n
for i in range(n):
    if is_appear[i]:
        continue
    group = []
    group.append(i)
    is_appear[i] = True
    j = p[i]
    while j != i:
        group.append(j)
        is_appear[j] = True
        j = p[j]
        
    groups.append(group)
    
    for i in group:
        group_size[i] = len(group)


cycle_score = [0]*n
for g in groups:
    score = 0
    for i in range(len(g)):
        score += c[g[i]]
    
    for i in g:
        cycle_score[i] = score

best = -float('inf')        
for i in range(n):
    score = 0
    best_i = -float('inf')
    cyc_num = k//group_size[i]
    if cycle_score[i]>0 and cyc_num>=1:
        remain = k-group_size[i]*(cyc_num-1)
        score += cycle_score[i]*(cyc_num-1)
        
    elif cycle_score[i]<=0:
        remain = group_size[i]
    elif cyc_num<1:
        remain = k
    
    j = i
    for _ in range(remain):
        j = p[j]
        score += c[j]
        best_i = max(best_i,score)
    best = max(best,best_i)
    
print(best)