N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

route = {}
point = {}

for idx, p in enumerate(P):
    route[idx+1] = p
    point[p] = C[p-1]

all_max = -float('inf')
for origin_start in range(1, N+1):
    visit = set()
    visit.add(origin_start)
    now_point = 0
    max_point = -float('inf')
    start = origin_start
    for k in range(K):
        now_point += point[start]
        max_point = max(max_point, now_point)
        start = route[start]
        if start in visit:
            roup_count = k + 1
            roup_point = now_point
            if now_point > 0:
              if K // roup_count >= 0:
                now_point = (K // roup_count) * roup_point
                start = origin_start
                for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

              if K // roup_count - 1 >= 0:
                now_point = (K // roup_count - 1) * roup_point
                start = origin_start
                for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
            break
        visit.add(start)

    all_max = max(max_point, all_max)
print(all_max) 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 route = {} route {} point = {} point {} for idx, p in enumerate(P):
    route[idx+1] = p
    point[p] = C[p-1] idx p enumerate(P) enumerate P route[idx+1] = p route[idx+1] route idx+1 idx 1 p point[p] = C[p-1] point[p] point p C[p-1] C p-1 p 1 all_max = -float('inf') all_max -float('inf') float('inf') float 'inf' for origin_start in range(1, N+1):
    visit = set()
    visit.add(origin_start)
    now_point = 0
    max_point = -float('inf')
    start = origin_start
    for k in range(K):
        now_point += point[start]
        max_point = max(max_point, now_point)
        start = route[start]
        if start in visit:
            roup_count = k + 1
            roup_point = now_point
            if now_point > 0:
              if K // roup_count >= 0:
                now_point = (K // roup_count) * roup_point
                start = origin_start
                for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

              if K // roup_count - 1 >= 0:
                now_point = (K // roup_count - 1) * roup_point
                start = origin_start
                for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
            break
        visit.add(start)

    all_max = max(max_point, all_max) origin_start range(1, N+1) range 1 N+1 N 1 visit = set() visit set() set visit.add(origin_start) visit.add visit add origin_start now_point = 0 now_point 0 max_point = -float('inf') max_point -float('inf') float('inf') float 'inf' start = origin_start start origin_start for k in range(K):
        now_point += point[start]
        max_point = max(max_point, now_point)
        start = route[start]
        if start in visit:
            roup_count = k + 1
            roup_point = now_point
            if now_point > 0:
              if K // roup_count >= 0:
                now_point = (K // roup_count) * roup_point
                start = origin_start
                for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

              if K // roup_count - 1 >= 0:
                now_point = (K // roup_count - 1) * roup_point
                start = origin_start
                for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
            break
        visit.add(start)

     k range(K) range K now_point += point[start] now_point point[start] point start max_point = max(max_point, now_point) max_point max(max_point, now_point) max max_point now_point start = route[start] start route[start] route start if start in visit:
            roup_count = k + 1
            roup_point = now_point
            if now_point > 0:
              if K // roup_count >= 0:
                now_point = (K // roup_count) * roup_point
                start = origin_start
                for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

              if K // roup_count - 1 >= 0:
                now_point = (K // roup_count - 1) * roup_point
                start = origin_start
                for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
            break
         start in visit start visit roup_count = k + 1 roup_count k + 1 k 1 roup_point = now_point roup_point now_point if now_point > 0:
              if K // roup_count >= 0:
                now_point = (K // roup_count) * roup_point
                start = origin_start
                for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

              if K // roup_count - 1 >= 0:
                now_point = (K // roup_count - 1) * roup_point
                start = origin_start
                for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
             now_point > 0 now_point 0 if K // roup_count >= 0:
                now_point = (K // roup_count) * roup_point
                start = origin_start
                for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

               K // roup_count >= 0 K // roup_count K roup_count 0 now_point = (K // roup_count) * roup_point now_point (K // roup_count) * roup_point K // roup_count K roup_count roup_point start = origin_start start origin_start for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

               _ range(K % roup_count) range K % roup_count K roup_count now_point += point[start] now_point point[start] point start max_point = max(max_point, now_point) max_point max(max_point, now_point) max max_point now_point start = route[start] start route[start] route start if K // roup_count - 1 >= 0:
                now_point = (K // roup_count - 1) * roup_point
                start = origin_start
                for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
             K // roup_count - 1 >= 0 K // roup_count - 1 K // roup_count K roup_count 1 0 now_point = (K // roup_count - 1) * roup_point now_point (K // roup_count - 1) * roup_point K // roup_count - 1 K // roup_count K roup_count 1 roup_point start = origin_start start origin_start for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
             _ range(roup_count) range roup_count now_point += point[start] now_point point[start] point start max_point = max(max_point, now_point) max_point max(max_point, now_point) max max_point now_point start = route[start] start route[start] route start break visit.add(start) visit.add visit add start all_max = max(max_point, all_max) all_max max(max_point, all_max) max max_point all_max print(all_max) print all_max start = origin_start origin_start start N, K = map(int, input().split()) map(int, input().split()) N max_point = max(max_point, now_point) max(max_point, now_point) max_point point = {} {} point K = map(int, input().split()) map(int, input().split()) K start = route[start] route[start] start roup_count = k + 1 k + 1 roup_count start = route[start] route[start] start all_max = max(max_point, all_max) max(max_point, all_max) all_max P = list(map(int, input().split())) list(map(int, input().split())) P start = origin_start origin_start start roup_point = now_point now_point roup_point max_point = max(max_point, now_point) max(max_point, now_point) max_point C = list(map(int, input().split())) list(map(int, input().split())) C start = route[start] route[start] start now_point += point[start] point[start] now_point now_point += point[start] point[start] now_point now_point = (K // roup_count) * roup_point (K // roup_count) * roup_point now_point route = {} {} route max_point = -float('inf') -float('inf') max_point visit = set() set() visit all_max = -float('inf') -float('inf') all_max now_point += point[start] point[start] now_point now_point = 0 0 now_point max_point = max(max_point, now_point) max(max_point, now_point) max_point now_point = (K // roup_count - 1) * roup_point (K // roup_count - 1) * roup_point now_point start = origin_start origin_start start
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

route = {}
point = {}

for idx, p in enumerate(P):
    route[idx+1] = p
    point[p] = C[p-1]

all_max = -float('inf')
for origin_start in range(1, N+1):
    visit = set()
    visit.add(origin_start)
    now_point = 0
    max_point = -float('inf')
    start = origin_start
    for k in range(K):
        now_point += point[start]
        max_point = max(max_point, now_point)
        start = route[start]
        if start in visit:
            roup_count = k + 1
            roup_point = now_point
            if now_point > 0:
              if K // roup_count >= 0:
                now_point = (K // roup_count) * roup_point
                start = origin_start
                for _ in range(K % roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]

              if K // roup_count - 1 >= 0:
                now_point = (K // roup_count - 1) * roup_point
                start = origin_start
                for _ in range(roup_count):
                    now_point += point[start]
                    max_point = max(max_point, now_point)
                    start = route[start]
            break
        visit.add(start)

    all_max = max(max_point, all_max)
print(all_max)