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