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

max_score = -float('inf')

for i in range(n):
    loop_score_array = []
    loop_score = 0
    loop_len = 0
    pos = i
    while True:
        pos = p[pos] - 1
        loop_score += c[pos]
        loop_score_array.append(loop_score)
        loop_len+=1
        if pos == i:
            break
    for i, score in enumerate(loop_score_array):
        if k-i-1 < 0:
            continue
        loop_num = (k-i-1)//loop_len
        score_sum = score + max(0, loop_score)*loop_num
        max_score = max(score_sum, max_score)
print(max_score) 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 max_score = -float('inf') max_score -float('inf') float('inf') float 'inf' for i in range(n):
    loop_score_array = []
    loop_score = 0
    loop_len = 0
    pos = i
    while True:
        pos = p[pos] - 1
        loop_score += c[pos]
        loop_score_array.append(loop_score)
        loop_len+=1
        if pos == i:
            break
    for i, score in enumerate(loop_score_array):
        if k-i-1 < 0:
            continue
        loop_num = (k-i-1)//loop_len
        score_sum = score + max(0, loop_score)*loop_num
        max_score = max(score_sum, max_score) i range(n) range n loop_score_array = [] loop_score_array [] loop_score = 0 loop_score 0 loop_len = 0 loop_len 0 pos = i pos i while True:
        pos = p[pos] - 1
        loop_score += c[pos]
        loop_score_array.append(loop_score)
        loop_len+=1
        if pos == i:
            break
     True pos = p[pos] - 1 pos p[pos] - 1 p[pos] p pos 1 loop_score += c[pos] loop_score c[pos] c pos loop_score_array.append(loop_score) loop_score_array.append loop_score_array append loop_score loop_len+=1 loop_len 1 if pos == i:
            break
     pos == i pos i break for i, score in enumerate(loop_score_array):
        if k-i-1 < 0:
            continue
        loop_num = (k-i-1)//loop_len
        score_sum = score + max(0, loop_score)*loop_num
        max_score = max(score_sum, max_score) i score enumerate(loop_score_array) enumerate loop_score_array if k-i-1 < 0:
            continue
         k-i-1 < 0 k-i-1 k-i k i 1 0 continue loop_num = (k-i-1)//loop_len loop_num (k-i-1)//loop_len k-i-1 k-i k i 1 loop_len score_sum = score + max(0, loop_score)*loop_num score_sum score + max(0, loop_score)*loop_num score max(0, loop_score)*loop_num max(0, loop_score) max 0 loop_score loop_num max_score = max(score_sum, max_score) max_score max(score_sum, max_score) max score_sum max_score print(max_score) print max_score loop_num = (k-i-1)//loop_len (k-i-1)//loop_len loop_num c = list(map(int, input().split())) list(map(int, input().split())) c max_score = max(score_sum, max_score) max(score_sum, max_score) max_score loop_len+=1 1 loop_len pos = p[pos] - 1 p[pos] - 1 pos score_sum = score + max(0, loop_score)*loop_num score + max(0, loop_score)*loop_num score_sum max_score = -float('inf') -float('inf') max_score k = map(int, input().split()) map(int, input().split()) k loop_score += c[pos] c[pos] loop_score loop_len = 0 0 loop_len pos = i i pos p = list(map(int, input().split())) list(map(int, input().split())) p loop_score = 0 0 loop_score n, k = map(int, input().split()) map(int, input().split()) n loop_score_array = [] [] loop_score_array
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

max_score = -float('inf')

for i in range(n):
    loop_score_array = []
    loop_score = 0
    loop_len = 0
    pos = i
    while True:
        pos = p[pos] - 1
        loop_score += c[pos]
        loop_score_array.append(loop_score)
        loop_len+=1
        if pos == i:
            break
    for i, score in enumerate(loop_score_array):
        if k-i-1 < 0:
            continue
        loop_num = (k-i-1)//loop_len
        score_sum = score + max(0, loop_score)*loop_num
        max_score = max(score_sum, max_score)
print(max_score)