n, k = map(int, input().split())
p = [int(i) - 1 for i in input().split()]
c = list(map(int, input().split()))

def mymax(max_variable, candidate):
    if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate)

max_cost = None
for start in range(n):
    seen = []
    next_ = p[start]
    cost_sum = []
    cost = 0
    while next_ != start:
        seen.append(next_)
        cost += c[next_]
        cost_sum.append(cost)
        next_ = p[next_]

    seen.append(start)
    cost += c[start]
    cost_sum.append(cost)

    l = len(seen)
    s = cost_sum[-1]
    for end in range(len(seen)):
        moves = end + 1
        remain_moves = k - moves
        if remain_moves < 0:
            continue
        cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l)
        max_cost = mymax(max_cost, cand_cost)
print(max_cost)
 n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k int(i) - 1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) - 1 int(i) int i 1 p = [int(i) - 1 for i in input().split()] p [int(i) - 1 for i in 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 mymax(max_variable, candidate):
    if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate) mymax if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate) max_variable is None max_variable None return candidate candidate return max(max_variable, candidate) max(max_variable, candidate) max max_variable candidate max_variable max_variable candidate candidate max_cost = None max_cost None for start in range(n):
    seen = []
    next_ = p[start]
    cost_sum = []
    cost = 0
    while next_ != start:
        seen.append(next_)
        cost += c[next_]
        cost_sum.append(cost)
        next_ = p[next_]

    seen.append(start)
    cost += c[start]
    cost_sum.append(cost)

    l = len(seen)
    s = cost_sum[-1]
    for end in range(len(seen)):
        moves = end + 1
        remain_moves = k - moves
        if remain_moves < 0:
            continue
        cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l)
        max_cost = mymax(max_cost, cand_cost) start range(n) range n seen = [] seen [] next_ = p[start] next_ p[start] p start cost_sum = [] cost_sum [] cost = 0 cost 0 while next_ != start:
        seen.append(next_)
        cost += c[next_]
        cost_sum.append(cost)
        next_ = p[next_]

     next_ != start next_ start seen.append(next_) seen.append seen append next_ cost += c[next_] cost c[next_] c next_ cost_sum.append(cost) cost_sum.append cost_sum append cost next_ = p[next_] next_ p[next_] p next_ seen.append(start) seen.append seen append start cost += c[start] cost c[start] c start cost_sum.append(cost) cost_sum.append cost_sum append cost l = len(seen) l len(seen) len seen s = cost_sum[-1] s cost_sum[-1] cost_sum -1 for end in range(len(seen)):
        moves = end + 1
        remain_moves = k - moves
        if remain_moves < 0:
            continue
        cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l)
        max_cost = mymax(max_cost, cand_cost) end range(len(seen)) range len(seen) len seen moves = end + 1 moves end + 1 end 1 remain_moves = k - moves remain_moves k - moves k moves if remain_moves < 0:
            continue
         remain_moves < 0 remain_moves 0 continue cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l) cand_cost cost_sum[end] + max(0, s) * int(remain_moves / l) cost_sum[end] cost_sum end max(0, s) * int(remain_moves / l) max(0, s) max 0 s int(remain_moves / l) int remain_moves / l remain_moves l max_cost = mymax(max_cost, cand_cost) max_cost mymax(max_cost, cand_cost) mymax max_cost cand_cost print(max_cost) print max_cost seen = [] [] seen k = map(int, input().split()) map(int, input().split()) k def mymax(max_variable, candidate):
    if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate) def mymax(max_variable, candidate):
    if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate) mymax cost += c[start] c[start] cost next_ = p[start] p[start] next_ l = len(seen) len(seen) l cost_sum = [] [] cost_sum n, k = map(int, input().split()) map(int, input().split()) n p = [int(i) - 1 for i in input().split()] [int(i) - 1 for i in input().split()] p cost += c[next_] c[next_] cost c = list(map(int, input().split())) list(map(int, input().split())) c moves = end + 1 end + 1 moves s = cost_sum[-1] cost_sum[-1] s max_cost = None None max_cost remain_moves = k - moves k - moves remain_moves cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l) cost_sum[end] + max(0, s) * int(remain_moves / l) cand_cost cost = 0 0 cost next_ = p[next_] p[next_] next_ max_cost = mymax(max_cost, cand_cost) mymax(max_cost, cand_cost) max_cost
n, k = map(int, input().split())
p = [int(i) - 1 for i in input().split()]
c = list(map(int, input().split()))

def mymax(max_variable, candidate):
    if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate)

max_cost = None
for start in range(n):
    seen = []
    next_ = p[start]
    cost_sum = []
    cost = 0
    while next_ != start:
        seen.append(next_)
        cost += c[next_]
        cost_sum.append(cost)
        next_ = p[next_]

    seen.append(start)
    cost += c[start]
    cost_sum.append(cost)

    l = len(seen)
    s = cost_sum[-1]
    for end in range(len(seen)):
        moves = end + 1
        remain_moves = k - moves
        if remain_moves < 0:
            continue
        cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l)
        max_cost = mymax(max_cost, cand_cost)
print(max_cost)

