
IN = list(map(int, input().split()))
N, K = IN[0], IN[1]
P = [None] + list(map(int, input().split()))
C = [None] + list(map(int, input().split()))
MAX_N = 45000
#N = 1000
#K = 10**7
#P = [None] + [random.choice(list(itertools.chain(range(1,i), range(i+1,N)))) for i in range(1, N+1)]
#C = [None] + random.choices(range(-10**9, 10**9), k=N)

verbose = False

def cumsum(a):
    tmp = [0]*(len(a)+1)
    for i in range(1, len(a)+1): tmp[i] = a[i-1] + tmp[i-1]
    return tmp[1:]

def find_cycle(p, pos):
    cnt = 0
    steps = [0] * (MAX_N+1)
    path = [False] * (MAX_N+1)
    while True:
        steps[cnt] = pos
        path[pos] = True
        cnt += 1
        pos = p[pos]
        if path[pos]: break # found closure
    st = steps.index(pos) # start of closure
    return [steps[:st], steps[st:cnt]]


def score(steps, c):
    ret = [0]*len(steps)
    for i in range(len(steps)): ret[i] = c[steps[i]]
    return ret

maxsc = -10**9-1
done = [False] * (N+1)
for i in range(1, N+1):
    if verbose: print('entering', i)
    if done[i]: continue
    [preclosure_path, closure_path] = find_cycle(P, i)
    if verbose: print('closure found:', closure_path)
    closure_scores = score(closure_path, C)
    preclosure_scores = score(preclosure_path, C)
    closure_score = sum(closure_scores)
    preclosure_len, closure_len = len(preclosure_path), len(closure_path)

    cycles = (K-preclosure_len)//closure_len
    resid = (K-preclosure_len)%closure_len
    if cycles>=1: cycles -= 1
    abbrev = closure_score * cycles
   
    trail = preclosure_scores + closure_scores * 3
    path = preclosure_path + closure_path*3
    if verbose: print('score trail:', trail)
    if verbose: print('path:', preclosure_path + closure_path*3)
    if verbose: print('mark:', mark)
    mark = min(K, len(preclosure_path+closure_path)+resid)
    localmax = max(cumsum(trail[0:mark])) + max(0, closure_score*cycles)
    if verbose: print('localmax:', localmax, 'cycles:', cycles)
    maxsc = max(localmax, maxsc)

    if verbose: print('going greedy')
    for j in range(1, closure_len):
        if K<=mark+j: break
        target = path[j]
        if done[target]: continue
        localmax = max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles)
        maxsc = max(localmax, maxsc)
        done[target] = True
        if verbose: print('done:', target, 'with', localmax)

print(maxsc) IN = list(map(int, input().split())) IN list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split N, K = IN[0], IN[1] N IN[0] IN 0 K IN[1] IN 1 P = [None] + list(map(int, input().split())) P [None] + list(map(int, input().split())) [None] None list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = [None] + list(map(int, input().split())) C [None] + list(map(int, input().split())) [None] None list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split MAX_N = 45000 MAX_N 45000 verbose = False verbose False def cumsum(a):
    tmp = [0]*(len(a)+1)
    for i in range(1, len(a)+1): tmp[i] = a[i-1] + tmp[i-1]
    return tmp[1:] cumsum tmp = [0]*(len(a)+1) tmp [0]*(len(a)+1) [0] 0 len(a)+1 len(a) len a 1 for i in range(1, len(a)+1): tmp[i] = a[i-1] + tmp[i-1]
     i range(1, len(a)+1) range 1 len(a)+1 len(a) len a 1 return tmp[1:] tmp[1:] tmp 1: 1 a a def find_cycle(p, pos):
    cnt = 0
    steps = [0] * (MAX_N+1)
    path = [False] * (MAX_N+1)
    while True:
        steps[cnt] = pos
        path[pos] = True
        cnt += 1
        pos = p[pos]
        if path[pos]: break # found closure
    st = steps.index(pos) # start of closure
    return [steps[:st], steps[st:cnt]] find_cycle cnt = 0 cnt 0 steps = [0] * (MAX_N+1) steps [0] * (MAX_N+1) [0] 0 MAX_N+1 MAX_N 1 path = [False] * (MAX_N+1) path [False] * (MAX_N+1) [False] False MAX_N+1 MAX_N 1 while True:
        steps[cnt] = pos
        path[pos] = True
        cnt += 1
        pos = p[pos]
        if path[pos]: break # found closure
     True steps[cnt] = pos steps[cnt] steps cnt pos path[pos] = True path[pos] path pos True cnt += 1 cnt 1 pos = p[pos] pos p[pos] p pos if path[pos]: break # found closure
     path[pos] path pos st = steps.index(pos) st steps.index(pos) steps.index steps index pos return [steps[:st], steps[st:cnt]] [steps[:st], steps[st:cnt]] steps[:st] steps :st st steps[st:cnt] steps st:cnt st cnt p p pos pos def score(steps, c):
    ret = [0]*len(steps)
    for i in range(len(steps)): ret[i] = c[steps[i]]
    return ret score ret = [0]*len(steps) ret [0]*len(steps) [0] 0 len(steps) len steps for i in range(len(steps)): ret[i] = c[steps[i]]
     i range(len(steps)) range len(steps) len steps return ret ret steps steps c c maxsc = -10**9-1 maxsc -10**9-1 -10**9 -10 9 1 done = [False] * (N+1) done [False] * (N+1) [False] False N+1 N 1 for i in range(1, N+1):
    if verbose: print('entering', i)
    if done[i]: continue
    [preclosure_path, closure_path] = find_cycle(P, i)
    if verbose: print('closure found:', closure_path)
    closure_scores = score(closure_path, C)
    preclosure_scores = score(preclosure_path, C)
    closure_score = sum(closure_scores)
    preclosure_len, closure_len = len(preclosure_path), len(closure_path)

    cycles = (K-preclosure_len)//closure_len
    resid = (K-preclosure_len)%closure_len
    if cycles>=1: cycles -= 1
    abbrev = closure_score * cycles
   
    trail = preclosure_scores + closure_scores * 3
    path = preclosure_path + closure_path*3
    if verbose: print('score trail:', trail)
    if verbose: print('path:', preclosure_path + closure_path*3)
    if verbose: print('mark:', mark)
    mark = min(K, len(preclosure_path+closure_path)+resid)
    localmax = max(cumsum(trail[0:mark])) + max(0, closure_score*cycles)
    if verbose: print('localmax:', localmax, 'cycles:', cycles)
    maxsc = max(localmax, maxsc)

    if verbose: print('going greedy')
    for j in range(1, closure_len):
        if K<=mark+j: break
        target = path[j]
        if done[target]: continue
        localmax = max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles)
        maxsc = max(localmax, maxsc)
        done[target] = True
        if verbose: print('done:', target, 'with', localmax) i range(1, N+1) range 1 N+1 N 1 if verbose: print('entering', i)
     verbose if done[i]: continue
     done[i] done i [preclosure_path, closure_path] = find_cycle(P, i) [preclosure_path, closure_path] preclosure_path closure_path find_cycle(P, i) find_cycle P i if verbose: print('closure found:', closure_path)
     verbose closure_scores = score(closure_path, C) closure_scores score(closure_path, C) score closure_path C preclosure_scores = score(preclosure_path, C) preclosure_scores score(preclosure_path, C) score preclosure_path C closure_score = sum(closure_scores) closure_score sum(closure_scores) sum closure_scores preclosure_len, closure_len = len(preclosure_path), len(closure_path) preclosure_len len(preclosure_path) len preclosure_path closure_len len(closure_path) len closure_path cycles = (K-preclosure_len)//closure_len cycles (K-preclosure_len)//closure_len K-preclosure_len K preclosure_len closure_len resid = (K-preclosure_len)%closure_len resid (K-preclosure_len)%closure_len K-preclosure_len K preclosure_len closure_len if cycles>=1: cycles -= 1
     cycles>=1 cycles 1 abbrev = closure_score * cycles abbrev closure_score * cycles closure_score cycles trail = preclosure_scores + closure_scores * 3 trail preclosure_scores + closure_scores * 3 preclosure_scores closure_scores * 3 closure_scores 3 path = preclosure_path + closure_path*3 path preclosure_path + closure_path*3 preclosure_path closure_path*3 closure_path 3 if verbose: print('score trail:', trail)
     verbose if verbose: print('path:', preclosure_path + closure_path*3)
     verbose if verbose: print('mark:', mark)
     verbose mark = min(K, len(preclosure_path+closure_path)+resid) mark min(K, len(preclosure_path+closure_path)+resid) min K len(preclosure_path+closure_path)+resid len(preclosure_path+closure_path) len preclosure_path+closure_path preclosure_path closure_path resid localmax = max(cumsum(trail[0:mark])) + max(0, closure_score*cycles) localmax max(cumsum(trail[0:mark])) + max(0, closure_score*cycles) max(cumsum(trail[0:mark])) max cumsum(trail[0:mark]) cumsum trail[0:mark] trail 0:mark 0 mark max(0, closure_score*cycles) max 0 closure_score*cycles closure_score cycles if verbose: print('localmax:', localmax, 'cycles:', cycles)
     verbose maxsc = max(localmax, maxsc) maxsc max(localmax, maxsc) max localmax maxsc if verbose: print('going greedy')
     verbose for j in range(1, closure_len):
        if K<=mark+j: break
        target = path[j]
        if done[target]: continue
        localmax = max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles)
        maxsc = max(localmax, maxsc)
        done[target] = True
        if verbose: print('done:', target, 'with', localmax) j range(1, closure_len) range 1 closure_len if K<=mark+j: break
         K<=mark+j K mark+j mark j target = path[j] target path[j] path j if done[target]: continue
         done[target] done target localmax = max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles) localmax max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles) max(cumsum(trail[j:mark+j])) max cumsum(trail[j:mark+j]) cumsum trail[j:mark+j] trail j:mark+j j mark+j mark j max(0, closure_score*cycles) max 0 closure_score*cycles closure_score cycles maxsc = max(localmax, maxsc) maxsc max(localmax, maxsc) max localmax maxsc done[target] = True done[target] done target True if verbose: print('done:', target, 'with', l verbose print(maxsc) print maxsc closure_score = sum(closure_scores) sum(closure_scores) closure_score path = preclosure_path + closure_path*3 preclosure_path + closure_path*3 path localmax = max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles) max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles) localmax resid = (K-preclosure_len)%closure_len (K-preclosure_len)%closure_len resid localmax = max(cumsum(trail[0:mark])) + max(0, closure_score*cycles) max(cumsum(trail[0:mark])) + max(0, closure_score*cycles) localmax done = [False] * (N+1) [False] * (N+1) done verbose = False False verbose cycles = (K-preclosure_len)//closure_len (K-preclosure_len)//closure_len cycles def cumsum(a):
    tmp = [0]*(len(a)+1)
    for i in range(1, len(a)+1): tmp[i] = a[i-1] + tmp[i-1]
    return tmp[1:] def cumsum(a):
    tmp = [0]*(len(a)+1)
    for i in range(1, len(a)+1): tmp[i] = a[i-1] + tmp[i-1]
    return tmp[1:] cumsum abbrev = closure_score * cycles closure_score * cycles abbrev MAX_N = 45000 45000 MAX_N trail = preclosure_scores + closure_scores * 3 preclosure_scores + closure_scores * 3 trail IN = list(map(int, input().split())) list(map(int, input().split())) IN def find_cycle(p, pos):
    cnt = 0
    steps = [0] * (MAX_N+1)
    path = [False] * (MAX_N+1)
    while True:
        steps[cnt] = pos
        path[pos] = True
        cnt += 1
        pos = p[pos]
        if path[pos]: break # found closure
    st = steps.index(pos) # start of closure
    return [steps[:st], steps[st:cnt]] def find_cycle(p, pos):
    cnt = 0
    steps = [0] * (MAX_N+1)
    path = [False] * (MAX_N+1)
    while True:
        steps[cnt] = pos
        path[pos] = True
        cnt += 1
        pos = p[pos]
        if path[pos]: break # found closure
    st = steps.index(pos) # start of closure
    return [steps[:st], steps[st:cnt]] find_cycle maxsc = max(localmax, maxsc) max(localmax, maxsc) maxsc C = [None] + list(map(int, input().split())) [None] + list(map(int, input().split())) C closure_len = len(preclosure_path), len(closure_path) len(closure_path) closure_len mark = min(K, len(preclosure_path+closure_path)+resid) min(K, len(preclosure_path+closure_path)+resid) mark P = [None] + list(map(int, input().split())) [None] + list(map(int, input().split())) P preclosure_len, closure_len = len(preclosure_path), len(closure_path) len(preclosure_path) preclosure_len closure_scores = score(closure_path, C) score(closure_path, C) closure_scores preclosure_scores = score(preclosure_path, C) score(preclosure_path, C) preclosure_scores N, K = IN[0], IN[1] IN[0] N maxsc = -10**9-1 -10**9-1 maxsc target = path[j] path[j] target def score(steps, c):
    ret = [0]*len(steps)
    for i in range(len(steps)): ret[i] = c[steps[i]]
    return ret def score(steps, c):
    ret = [0]*len(steps)
    for i in range(len(steps)): ret[i] = c[steps[i]]
    return ret score maxsc = max(localmax, maxsc) max(localmax, maxsc) maxsc K = IN[0], IN[1] IN[1] K

IN = list(map(int, input().split()))
N, K = IN[0], IN[1]
P = [None] + list(map(int, input().split()))
C = [None] + list(map(int, input().split()))
MAX_N = 45000
#N = 1000
#K = 10**7
#P = [None] + [random.choice(list(itertools.chain(range(1,i), range(i+1,N)))) for i in range(1, N+1)]
#C = [None] + random.choices(range(-10**9, 10**9), k=N)

verbose = False

def cumsum(a):
    tmp = [0]*(len(a)+1)
    for i in range(1, len(a)+1): tmp[i] = a[i-1] + tmp[i-1]
    return tmp[1:]

def find_cycle(p, pos):
    cnt = 0
    steps = [0] * (MAX_N+1)
    path = [False] * (MAX_N+1)
    while True:
        steps[cnt] = pos
        path[pos] = True
        cnt += 1
        pos = p[pos]
        if path[pos]: break # found closure
    st = steps.index(pos) # start of closure
    return [steps[:st], steps[st:cnt]]


def score(steps, c):
    ret = [0]*len(steps)
    for i in range(len(steps)): ret[i] = c[steps[i]]
    return ret

maxsc = -10**9-1
done = [False] * (N+1)
for i in range(1, N+1):
    if verbose: print('entering', i)
    if done[i]: continue
    [preclosure_path, closure_path] = find_cycle(P, i)
    if verbose: print('closure found:', closure_path)
    closure_scores = score(closure_path, C)
    preclosure_scores = score(preclosure_path, C)
    closure_score = sum(closure_scores)
    preclosure_len, closure_len = len(preclosure_path), len(closure_path)

    cycles = (K-preclosure_len)//closure_len
    resid = (K-preclosure_len)%closure_len
    if cycles>=1: cycles -= 1
    abbrev = closure_score * cycles
   
    trail = preclosure_scores + closure_scores * 3
    path = preclosure_path + closure_path*3
    if verbose: print('score trail:', trail)
    if verbose: print('path:', preclosure_path + closure_path*3)
    if verbose: print('mark:', mark)
    mark = min(K, len(preclosure_path+closure_path)+resid)
    localmax = max(cumsum(trail[0:mark])) + max(0, closure_score*cycles)
    if verbose: print('localmax:', localmax, 'cycles:', cycles)
    maxsc = max(localmax, maxsc)

    if verbose: print('going greedy')
    for j in range(1, closure_len):
        if K<=mark+j: break
        target = path[j]
        if done[target]: continue
        localmax = max(cumsum(trail[j:mark+j])) + max(0, closure_score*cycles)
        maxsc = max(localmax, maxsc)
        done[target] = True
        if verbose: print('done:', target, 'with', localmax)

print(maxsc)
