N, K = [int(i) for i in input().split()]
P = [int(i)-1 for i in input().split()]
C = [int(i) for i in input().split()]
def f(s):
    tmp = s
    circle_length = 0
    circle_score = 0
    for i in range(1, N+1):
        tmp = P[tmp]
        circle_length += 1
        circle_score += C[tmp]
        if s == tmp:
            return (True, circle_length, circle_score)
        if i == K:
            return (False, K, -10**10)

def f1(s, circle_length):
    tmp = s
    max_score = -10**10
    tmp_score = 0
    for i in range(1, circle_length+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        if max_score < tmp_score:
            max_score = tmp_score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score

def f2(s, circle_length, circle_score):
    max_score = (K//circle_length) * circle_score
    tmp = s
    tmp_score = 0
    for i in range(1, (circle_length)+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        score = ((K-i)//circle_length) * circle_score + tmp_score
        if max_score < score:
            max_score = score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score

tot_max = -10**10
for i in range(N):
    is_circle, circle_length, circle_score = f(i)
    max_score = f1(i, circle_length)
    if is_circle and circle_score > 0:
        max_score2 = f2(i, circle_length, circle_score)
        if max_score2 > max_score:
            max_score = max_score2
    if max_score > tot_max:
        tot_max = max_score
print(tot_max) int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i N, K = [int(i) for i in input().split()] N [int(i) for i in input().split()] K int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 P = [int(i)-1 for i in input().split()] P [int(i)-1 for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i C = [int(i) for i in input().split()] C [int(i) for i in input().split()] def f(s):
    tmp = s
    circle_length = 0
    circle_score = 0
    for i in range(1, N+1):
        tmp = P[tmp]
        circle_length += 1
        circle_score += C[tmp]
        if s == tmp:
            return (True, circle_length, circle_score)
        if i == K:
            return (False, K, -10**10) f tmp = s tmp s circle_length = 0 circle_length 0 circle_score = 0 circle_score 0 for i in range(1, N+1):
        tmp = P[tmp]
        circle_length += 1
        circle_score += C[tmp]
        if s == tmp:
            return (True, circle_length, circle_score)
        if i == K:
            return (False, K, -10**10) i range(1, N+1) range 1 N+1 N 1 tmp = P[tmp] tmp P[tmp] P tmp circle_length += 1 circle_length 1 circle_score += C[tmp] circle_score C[tmp] C tmp if s == tmp:
            return (True, circle_length, circle_score)
         s == tmp s tmp return (True, circle_length, circle_score) (True, circle_length, circle_score) True circle_length circle_score if i == K:
            return (False, K, -10**10) i == K i K return (False, K, -10**10) (False, K, -10**10) False K -10**10 -10 10 s s def f1(s, circle_length):
    tmp = s
    max_score = -10**10
    tmp_score = 0
    for i in range(1, circle_length+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        if max_score < tmp_score:
            max_score = tmp_score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score f1 tmp = s tmp s max_score = -10**10 max_score -10**10 -10 10 tmp_score = 0 tmp_score 0 for i in range(1, circle_length+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        if max_score < tmp_score:
            max_score = tmp_score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
     i range(1, circle_length+1) range 1 circle_length+1 circle_length 1 tmp = P[tmp] tmp P[tmp] P tmp tmp_score += C[tmp] tmp_score C[tmp] C tmp if max_score < tmp_score:
            max_score = tmp_score
         max_score < tmp_score max_score tmp_score max_score = tmp_score max_score tmp_score if tmp_score < 0:
            return max_score
         tmp_score < 0 tmp_score 0 return max_score max_score if i == K:
            return max_score
     i == K i K return max_score max_score return max_score max_score s s circle_length circle_length def f2(s, circle_length, circle_score):
    max_score = (K//circle_length) * circle_score
    tmp = s
    tmp_score = 0
    for i in range(1, (circle_length)+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        score = ((K-i)//circle_length) * circle_score + tmp_score
        if max_score < score:
            max_score = score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score f2 max_score = (K//circle_length) * circle_score max_score (K//circle_length) * circle_score K//circle_length K circle_length circle_score tmp = s tmp s tmp_score = 0 tmp_score 0 for i in range(1, (circle_length)+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        score = ((K-i)//circle_length) * circle_score + tmp_score
        if max_score < score:
            max_score = score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
     i range(1, (circle_length)+1) range 1 (circle_length)+1 circle_length 1 tmp = P[tmp] tmp P[tmp] P tmp tmp_score += C[tmp] tmp_score C[tmp] C tmp score = ((K-i)//circle_length) * circle_score + tmp_score score ((K-i)//circle_length) * circle_score + tmp_score ((K-i)//circle_length) * circle_score (K-i)//circle_length K-i K i circle_length circle_score tmp_score if max_score < score:
            max_score = score
         max_score < score max_score score max_score = score max_score score if tmp_score < 0:
            return max_score
         tmp_score < 0 tmp_score 0 return max_score max_score if i == K:
            return max_score
     i == K i K return max_score max_score return max_score max_score s s circle_length circle_length circle_score circle_score tot_max = -10**10 tot_max -10**10 -10 10 for i in range(N):
    is_circle, circle_length, circle_score = f(i)
    max_score = f1(i, circle_length)
    if is_circle and circle_score > 0:
        max_score2 = f2(i, circle_length, circle_score)
        if max_score2 > max_score:
            max_score = max_score2
    if max_score > tot_max:
        tot_max = max_score i range(N) range N is_circle, circle_length, circle_score = f(i) is_circle f(i) f i circle_length circle_score max_score = f1(i, circle_length) max_score f1(i, circle_length) f1 i circle_length if is_circle and circle_score > 0:
        max_score2 = f2(i, circle_length, circle_score)
        if max_score2 > max_score:
            max_score = max_score2
     is_circle and circle_score > 0 is_circle circle_score > 0 circle_score 0 max_score2 = f2(i, circle_length, circle_score) max_score2 f2(i, circle_length, circle_score) f2 i circle_length circle_score if max_score2 > max_score:
            max_score = max_score2
     max_score2 > max_score max_score2 max_score max_score = max_score2 max_score max_score2 if max_score > tot_max:
        tot_max = max_score max_score > tot_max max_score tot_max tot_max = max_score tot_max max_score print(tot_max) print tot_max max_score = max_score2 max_score2 max_score circle_length, circle_score = f(i) f(i) circle_length C = [int(i) for i in input().split()] [int(i) for i in input().split()] C def f(s):
    tmp = s
    circle_length = 0
    circle_score = 0
    for i in range(1, N+1):
        tmp = P[tmp]
        circle_length += 1
        circle_score += C[tmp]
        if s == tmp:
            return (True, circle_length, circle_score)
        if i == K:
            return (False, K, -10**10) def f(s):
    tmp = s
    circle_length = 0
    circle_score = 0
    for i in range(1, N+1):
        tmp = P[tmp]
        circle_length += 1
        circle_score += C[tmp]
        if s == tmp:
            return (True, circle_length, circle_score)
        if i == K:
            return (False, K, -10**10) f N, K = [int(i) for i in input().split()] [int(i) for i in input().split()] N def f1(s, circle_length):
    tmp = s
    max_score = -10**10
    tmp_score = 0
    for i in range(1, circle_length+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        if max_score < tmp_score:
            max_score = tmp_score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score def f1(s, circle_length):
    tmp = s
    max_score = -10**10
    tmp_score = 0
    for i in range(1, circle_length+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        if max_score < tmp_score:
            max_score = tmp_score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score f1 circle_score = f(i) f(i) circle_score is_circle, circle_length, circle_score = f(i) f(i) is_circle tot_max = max_score max_score tot_max tot_max = -10**10 -10**10 tot_max def f2(s, circle_length, circle_score):
    max_score = (K//circle_length) * circle_score
    tmp = s
    tmp_score = 0
    for i in range(1, (circle_length)+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        score = ((K-i)//circle_length) * circle_score + tmp_score
        if max_score < score:
            max_score = score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score def f2(s, circle_length, circle_score):
    max_score = (K//circle_length) * circle_score
    tmp = s
    tmp_score = 0
    for i in range(1, (circle_length)+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        score = ((K-i)//circle_length) * circle_score + tmp_score
        if max_score < score:
            max_score = score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score f2 max_score2 = f2(i, circle_length, circle_score) f2(i, circle_length, circle_score) max_score2 max_score = f1(i, circle_length) f1(i, circle_length) max_score K = [int(i) for i in input().split()] [int(i) for i in input().split()] K P = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] P
N, K = [int(i) for i in input().split()]
P = [int(i)-1 for i in input().split()]
C = [int(i) for i in input().split()]
def f(s):
    tmp = s
    circle_length = 0
    circle_score = 0
    for i in range(1, N+1):
        tmp = P[tmp]
        circle_length += 1
        circle_score += C[tmp]
        if s == tmp:
            return (True, circle_length, circle_score)
        if i == K:
            return (False, K, -10**10)

def f1(s, circle_length):
    tmp = s
    max_score = -10**10
    tmp_score = 0
    for i in range(1, circle_length+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        if max_score < tmp_score:
            max_score = tmp_score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score

def f2(s, circle_length, circle_score):
    max_score = (K//circle_length) * circle_score
    tmp = s
    tmp_score = 0
    for i in range(1, (circle_length)+1):
        tmp = P[tmp]
        tmp_score += C[tmp]
        score = ((K-i)//circle_length) * circle_score + tmp_score
        if max_score < score:
            max_score = score
        if tmp_score < 0:
            return max_score
        if i == K:
            return max_score
    return max_score

tot_max = -10**10
for i in range(N):
    is_circle, circle_length, circle_score = f(i)
    max_score = f1(i, circle_length)
    if is_circle and circle_score > 0:
        max_score2 = f2(i, circle_length, circle_score)
        if max_score2 > max_score:
            max_score = max_score2
    if max_score > tot_max:
        tot_max = max_score
print(tot_max)
