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

pl = [0] + list(map(int, input().split()))

cl = [0] + list(map(int, input().split()))



ans = (10**18)*(-1)

for start in range(1,n+1):

    val = 0

    vals = []

    already_set = set()

    curr_mass = start

    while True:

        next_mass = pl[curr_mass]

        point = cl[next_mass]

        if next_mass in already_set:

            break

        else:

            vals.append(point)

            already_set.add(next_mass)

            curr_mass = next_mass

    # print(vals)

    if k <= len(vals):

        c_max = vals[0]

        c_val = vals[0]

        # if len(vals) > 1:

        for v in vals[1:k]:

            c_val += v

            c_max = max(c_max, c_val)

        # print(c_max)

        ans = max(c_max,ans)

    else:

        v_sum = sum(vals)

        if v_sum <= 0:

            c_max = vals[0]

            c_val = vals[0]

            # if len(vals) > 1:

            for v in vals[1:]:

                c_val += v

                c_max = max(c_max, c_val)

            # print(c_max)

            ans = max(c_max,ans)

        else:

            if k%len(vals) > 0:

                loop_cnt = k//len(vals)

                rem = k - loop_cnt*len(vals)

                c_max = v_sum*loop_cnt

                c_val = v_sum*loop_cnt

                if rem > 0:

                    for v in vals[:rem]:

                        c_val += v

                        c_max = max(c_max, c_val)

                # print(c_max)

                ans = max(c_max,ans)

            else:

                loop_cnt = k//len(vals) - 1

                rem = len(vals)

                c_max = v_sum*loop_cnt

                c_val = v_sum*loop_cnt

                if rem > 0:

                    for v in vals[:rem]:

                        c_val += v

                        c_max = max(c_max, c_val)

                # print(c_max)

                ans = max(c_max,ans)

    # print(c_max)

print(ans)

# 2939275739

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

pl = [0] + list(map(int, input().split()))

cl = [0] + list(map(int, input().split()))



valsl = []

already = [False]*(n+1)

for i in range(1,n+1):

    if already[i]: continue

    vals = [cl[i]]

    curr_i = i

    already[i] = True

    while True:

        next_i = pl[curr_i]

        if already[next_i]: break

        v = cl[next_i]

        vals.append(v)

        curr_i = next_i

        already[next_i] = True

    valsl.append(vals)





# print(valsl)

ans = -10**18

for vals in valsl:

    curr_sum = sum(vals)

    vlen = len(vals)

    for i in range(len(vals)):

        new_vals = vals[i:] + vals[0:i]

        if curr_sum <= 0:

            vmax = -10**18

            v = 0

            for j in range(min(k,vlen)):

                v += new_vals[j]

                vmax = max(v,vmax)

            ans = max(vmax, ans)

        else:

            loop = k//vlen-1

            rem = k-loop*vlen

            v = loop*curr_sum

            vmax = v

            for j in range(rem):

                v += new_vals[j%vlen]

                vmax = max(v,vmax)

            ans = max(vmax, ans)



print(ans)
n, k = list(map(int, input().split()))
pl = [0] + list(map(int, input().split()))
cl = [0] + list(map(int, input().split()))
ans = (10**18) * (-1)
for start in range(1, n + 1):
    val = 0
    vals = []
    already_set = set()
    curr_mass = start
    while True:
        next_mass = pl[curr_mass]
        point = cl[next_mass]
        if next_mass in already_set:
            break
        else:
            vals.append(point)
            already_set.add(next_mass)
            curr_mass = next_mass
    # print(vals)
    if k <= len(vals):
        c_max = vals[0]
        c_val = vals[0]
        # if len(vals) > 1:
        for v in vals[1:k]:
            c_val += v
            c_max = max(c_max, c_val)
        # print(c_max)
        ans = max(c_max, ans)
    else:
        v_sum = sum(vals)
        if v_sum <= 0:
            c_max = vals[0]
            c_val = vals[0]
            # if len(vals) > 1:
            for v in vals[1:]:
                c_val += v
                c_max = max(c_max, c_val)
            # print(c_max)
            ans = max(c_max, ans)
        else:
            if k % len(vals) > 0:
                loop_cnt = k // len(vals)
                rem = k - loop_cnt * len(vals)
                c_max = v_sum * loop_cnt
                c_val = v_sum * loop_cnt
                if rem > 0:
                    for v in vals[:rem]:
                        c_val += v
                        c_max = max(c_max, c_val)
                # print(c_max)
                ans = max(c_max, ans)
            else:
                loop_cnt = k // len(vals) - 1
                rem = len(vals)
                c_max = v_sum * loop_cnt
                c_val = v_sum * loop_cnt
                if rem > 0:
                    for v in vals[:rem]:
                        c_val += v
                        c_max = max(c_max, c_val)
                # print(c_max)
                ans = max(c_max, ans)
    # print(c_max)
print(ans)
# 2939275739

n, k = list(map(int, input().split()))
pl = [0] + list(map(int, input().split()))
cl = [0] + list(map(int, input().split()))
valsl = []
already = [False] * (n + 1)
for i in range(1, n + 1):
    if already[i]:
        continue
    vals = [cl[i]]
    curr_i = i
    already[i] = True
    while True:
        next_i = pl[curr_i]
        if already[next_i]:
            break
        v = cl[next_i]
        vals.append(v)
        curr_i = next_i
        already[next_i] = True
    valsl.append(vals)
# print(valsl)
ans = -(10**18)
for vals in valsl:
    curr_sum = sum(vals)
    vlen = len(vals)
    for i in range(len(vals)):
        new_vals = vals[i:] + vals[0:i]
        if curr_sum <= 0:
            vmax = -(10**18)
            v = 0
            for j in range(min(k, vlen)):
                v += new_vals[j]
                vmax = max(v, vmax)
            ans = max(vmax, ans)
        else:
            loop = k // vlen - 1
            rem = k - loop * vlen
            v = loop * curr_sum
            vmax = v
            for j in range(rem):
                v += new_vals[j % vlen]
                vmax = max(v, vmax)
            ans = max(vmax, ans)
print(ans)

-ans = (10**18) * (-1) -for start in range(1, n + 1): -    val = 0 -    vals = [] -    already_set = set() -    curr_mass = start +valsl = [] +already = [False] * (n + 1) +for i in range(1, n + 1): +    if already[i]: +        continue +    vals = [cl[i]] +    curr_i = i +    already[i] = True -        next_mass = pl[curr_mass] -        point = cl[next_mass] -        if next_mass in already_set: +        next_i = pl[curr_i] +        if already[next_i]: +        v = cl[next_i] +        vals.append(v) +        curr_i = next_i +        already[next_i] = True +    valsl.append(vals) +# print(valsl) +ans = -(10**18) +for vals in valsl: +    curr_sum = sum(vals) +    vlen = len(vals) +    for i in range(len(vals)): +        new_vals = vals[i:] + vals[0:i] +        if curr_sum <= 0: +            vmax = -(10**18) +            v = 0 +            for j in range(min(k, vlen)): +                v += new_vals[j] +                vmax = max(v, vmax) +            ans = max(vmax, ans) -            vals.append(point) -            already_set.add(next_mass) -            curr_mass = next_mass -    # print(vals) -    if k <= len(vals): -        c_max = vals[0] -        c_val = vals[0] -        # if len(vals) > 1: -        for v in vals[1:k]: -            c_val += v -            c_max = max(c_max, c_val) -        # print(c_max) -        ans = max(c_max, ans) -    else: -        v_sum = sum(vals) -        if v_sum <= 0: -            c_max = vals[0] -            c_val = vals[0] -            # if len(vals) > 1: -            for v in vals[1:]: -                c_val += v -                c_max = max(c_max, c_val) -            # print(c_max) -            ans = max(c_max, ans) -        else: -            if k % len(vals) > 0: -                loop_cnt = k // len(vals) -                rem = k - loop_cnt * len(vals) -                c_max = v_sum * loop_cnt -                c_val = v_sum * loop_cnt -                if rem > 0: -                    for v in vals[:rem]: -                        c_val += v -                        c_max = max(c_max, c_val) -                # print(c_max) -                ans = max(c_max, ans) -            else: -                loop_cnt = k // len(vals) - 1 -                rem = len(vals) -                c_max = v_sum * loop_cnt -                c_val = v_sum * loop_cnt -                if rem > 0: -                    for v in vals[:rem]: -                        c_val += v -                        c_max = max(c_max, c_val) -                # print(c_max) -                ans = max(c_max, ans) -    # print(c_max) +            loop = k // vlen - 1 +            rem = k - loop * vlen +            v = loop * curr_sum +            vmax = v +            for j in range(rem): +                v += new_vals[j % vlen] +                vmax = max(v, vmax) +            ans = max(vmax, ans) -# 2939275739