n, k = [int(z) for z in input().strip().split()]
p = [int(z) - 1 for z in input().strip().split()]
c = [int(z) for z in input().strip().split()]
 
visited = [False for _ in range(n)]
cycle = []
for start in range(n):
    if visited[start]:
        continue
    now = start
    route = []
    while not visited[now]:
        visited[now] = True
        route.append(now)
        now = p[now]
    cycle.append(route)
 
min_c = min(c)
max_value = min_c
for route in cycle:
    full_value = sum([c[i] for i in route])
    double_route = route + route
    v = [c[i] for i in double_route]
    max_sum_step = []
    for step in range(1, len(route)):
        s = sum(v[:step])
        max_sum = max(s, min_c)
        for idx in range(step, step + len(route) - 1):
            s += v[idx]
            if idx >= step:
                s -= v[idx - step]
            max_sum = max(s, max_sum)
        max_sum_step.append(max_sum)
    addition = k % len(route)
    if full_value > 0:
        addition_value = 0 if addition == 0 else max(max_sum_step[:addition])
        value1 = int(k / len(route)) * full_value + addition_value
        value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
        value = max(value1, value2)
    else:
        value = max(max_sum_step[:min(k, len(max_sum_step))])
    max_value = max(max_value, value)
print(max_value) int(z) for z in input().strip().split() for z in input().strip().split() z input().strip().split() ().strip().split ().strip() ().strip () input strip split for z in input().strip().split() int(z) int z n, k = [int(z) for z in input().strip().split()] n [int(z) for z in input().strip().split()] k int(z) - 1 for z in input().strip().split() for z in input().strip().split() z input().strip().split() ().strip().split ().strip() ().strip () input strip split for z in input().strip().split() int(z) - 1 int(z) int z 1 p = [int(z) - 1 for z in input().strip().split()] p [int(z) - 1 for z in input().strip().split()] int(z) for z in input().strip().split() for z in input().strip().split() z input().strip().split() ().strip().split ().strip() ().strip () input strip split for z in input().strip().split() int(z) int z c = [int(z) for z in input().strip().split()] c [int(z) for z in input().strip().split()] False for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) False visited = [False for _ in range(n)] visited [False for _ in range(n)] cycle = [] cycle [] for start in range(n):
    if visited[start]:
        continue
    now = start
    route = []
    while not visited[now]:
        visited[now] = True
        route.append(now)
        now = p[now]
    cycle.append(route)
  start range(n) range n if visited[start]:
        continue
     visited[start] visited start continue now = start now start route = [] route [] while not visited[now]:
        visited[now] = True
        route.append(now)
        now = p[now]
     not visited[now] visited[now] visited now visited[now] = True visited[now] visited now True route.append(now) route.append route append now now = p[now] now p[now] p now cycle.append(route) cycle.append cycle append route min_c = min(c) min_c min(c) min c max_value = min_c max_value min_c for route in cycle:
    full_value = sum([c[i] for i in route])
    double_route = route + route
    v = [c[i] for i in double_route]
    max_sum_step = []
    for step in range(1, len(route)):
        s = sum(v[:step])
        max_sum = max(s, min_c)
        for idx in range(step, step + len(route) - 1):
            s += v[idx]
            if idx >= step:
                s -= v[idx - step]
            max_sum = max(s, max_sum)
        max_sum_step.append(max_sum)
    addition = k % len(route)
    if full_value > 0:
        addition_value = 0 if addition == 0 else max(max_sum_step[:addition])
        value1 = int(k / len(route)) * full_value + addition_value
        value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
        value = max(value1, value2)
    else:
        value = max(max_sum_step[:min(k, len(max_sum_step))])
    max_value = max(max_value, value) route cycle c[i] for i in route for i in route i route for i in route c[i] c i full_value = sum([c[i] for i in route]) full_value sum([c[i] for i in route]) sum [c[i] for i in route] double_route = route + route double_route route + route route route c[i] for i in double_route for i in double_route i double_route for i in double_route c[i] c i v = [c[i] for i in double_route] v [c[i] for i in double_route] max_sum_step = [] max_sum_step [] for step in range(1, len(route)):
        s = sum(v[:step])
        max_sum = max(s, min_c)
        for idx in range(step, step + len(route) - 1):
            s += v[idx]
            if idx >= step:
                s -= v[idx - step]
            max_sum = max(s, max_sum)
        max_sum_step.append(max_sum)
     step range(1, len(route)) range 1 len(route) len route s = sum(v[:step]) s sum(v[:step]) sum v[:step] v :step step max_sum = max(s, min_c) max_sum max(s, min_c) max s min_c for idx in range(step, step + len(route) - 1):
            s += v[idx]
            if idx >= step:
                s -= v[idx - step]
            max_sum = max(s, max_sum)
         idx range(step, step + len(route) - 1) range step step + len(route) - 1 step + len(route) step len(route) len route 1 s += v[idx] s v[idx] v idx if idx >= step:
                s -= v[idx - step]
             idx >= step idx step s -= v[idx - step] s v[idx - step] v idx - step idx step max_sum = max(s, max_sum) max_sum max(s, max_sum) max s max_sum max_sum_step.append(max_sum) max_sum_step.append max_sum_step append max_sum addition = k % len(route) addition k % len(route) k len(route) len route if full_value > 0:
        addition_value = 0 if addition == 0 else max(max_sum_step[:addition])
        value1 = int(k / len(route)) * full_value + addition_value
        value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
        value = max(value1, value2)
    else:
        value = max(max_sum_step[:min(k, len(max_sum_step))])
     full_value > 0 full_value 0 addition_value = 0 if addition == 0 else max(max_sum_step[:addition]) addition_value 0 if addition == 0 else max(max_sum_step[:addition]) addition == 0 addition 0 0 max(max_sum_step[:addition]) max max_sum_step[:addition] max_sum_step :addition addition value1 = int(k / len(route)) * full_value + addition_value value1 int(k / len(route)) * full_value + addition_value int(k / len(route)) * full_value int(k / len(route)) int k / len(route) k len(route) len route full_value addition_value value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step) value2 (int(k / len(route)) - 1) * full_value + max(max_sum_step) (int(k / len(route)) - 1) * full_value int(k / len(route)) - 1 int(k / len(route)) int k / len(route) k len(route) len route 1 full_value max(max_sum_step) max max_sum_step value = max(value1, value2) value max(value1, value2) max value1 value2 value = max(max_sum_step[:min(k, len(max_sum_step))]) value max(max_sum_step[:min(k, len(max_sum_step))]) max max_sum_step[:min(k, len(max_sum_step))] max_sum_step :min(k, len(max_sum_step)) min(k, len(max_sum_step)) min k len(max_sum_step) len max_sum_step max_value = max(max_value, value) max_value max(max_value, value) max max_value value print(max_value) print max_value cycle = [] [] cycle s -= v[idx - step] v[idx - step] s addition_value = 0 if addition == 0 else max(max_sum_step[:addition]) 0 if addition == 0 else max(max_sum_step[:addition]) addition_value now = start start now value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step) (int(k / len(route)) - 1) * full_value + max(max_sum_step) value2 max_sum = max(s, max_sum) max(s, max_sum) max_sum addition = k % len(route) k % len(route) addition n, k = [int(z) for z in input().strip().split()] [int(z) for z in input().strip().split()] n p = [int(z) - 1 for z in input().strip().split()] [int(z) - 1 for z in input().strip().split()] p min_c = min(c) min(c) min_c value1 = int(k / len(route)) * full_value + addition_value int(k / len(route)) * full_value + addition_value value1 full_value = sum([c[i] for i in route]) sum([c[i] for i in route]) full_value route = [] [] route visited = [False for _ in range(n)] [False for _ in range(n)] visited max_sum = max(s, min_c) max(s, min_c) max_sum max_value = max(max_value, value) max(max_value, value) max_value max_sum_step = [] [] max_sum_step k = [int(z) for z in input().strip().split()] [int(z) for z in input().strip().split()] k now = p[now] p[now] now s = sum(v[:step]) sum(v[:step]) s double_route = route + route route + route double_route value = max(value1, value2) max(value1, value2) value max_value = min_c min_c max_value s += v[idx] v[idx] s v = [c[i] for i in double_route] [c[i] for i in double_route] v c = [int(z) for z in input().strip().split()] [int(z) for z in input().strip().split()] c value = max(max_sum_step[:min(k, len(max_sum_step))]) max(max_sum_step[:min(k, len(max_sum_step))]) value
n, k = [int(z) for z in input().strip().split()]
p = [int(z) - 1 for z in input().strip().split()]
c = [int(z) for z in input().strip().split()]
 
visited = [False for _ in range(n)]
cycle = []
for start in range(n):
    if visited[start]:
        continue
    now = start
    route = []
    while not visited[now]:
        visited[now] = True
        route.append(now)
        now = p[now]
    cycle.append(route)
 
min_c = min(c)
max_value = min_c
for route in cycle:
    full_value = sum([c[i] for i in route])
    double_route = route + route
    v = [c[i] for i in double_route]
    max_sum_step = []
    for step in range(1, len(route)):
        s = sum(v[:step])
        max_sum = max(s, min_c)
        for idx in range(step, step + len(route) - 1):
            s += v[idx]
            if idx >= step:
                s -= v[idx - step]
            max_sum = max(s, max_sum)
        max_sum_step.append(max_sum)
    addition = k % len(route)
    if full_value > 0:
        addition_value = 0 if addition == 0 else max(max_sum_step[:addition])
        value1 = int(k / len(route)) * full_value + addition_value
        value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
        value = max(value1, value2)
    else:
        value = max(max_sum_step[:min(k, len(max_sum_step))])
    max_value = max(max_value, value)
print(max_value)