N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
Q = [int(i) for i in input().split()]
ans = - float('inf')
for i in range(N):
    now = i
    cnt = 0
    span = 0
    while True:
        span += 1
        cnt += Q[now]
        now = P[now]
        if now == i:
            break
    point = 0
    now = i
    for j in range(1, span + 1):
        point += Q[now]
        now = P[now]       
        if j > K:
            break
        value = point + (max(cnt, 0) * ((K - j) // span))
        ans = max(ans, value)
print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () 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 Q = [int(i) for i in input().split()] Q [int(i) for i in input().split()] ans = - float('inf') ans - float('inf') float('inf') float 'inf' for i in range(N):
    now = i
    cnt = 0
    span = 0
    while True:
        span += 1
        cnt += Q[now]
        now = P[now]
        if now == i:
            break
    point = 0
    now = i
    for j in range(1, span + 1):
        point += Q[now]
        now = P[now]       
        if j > K:
            break
        value = point + (max(cnt, 0) * ((K - j) // span))
        ans = max(ans, value) i range(N) range N now = i now i cnt = 0 cnt 0 span = 0 span 0 while True:
        span += 1
        cnt += Q[now]
        now = P[now]
        if now == i:
            break
     True span += 1 span 1 cnt += Q[now] cnt Q[now] Q now now = P[now] now P[now] P now if now == i:
            break
     now == i now i break point = 0 point 0 now = i now i for j in range(1, span + 1):
        point += Q[now]
        now = P[now]       
        if j > K:
            break
        value = point + (max(cnt, 0) * ((K - j) // span))
        ans = max(ans, value) j range(1, span + 1) range 1 span + 1 span 1 point += Q[now] point Q[now] Q now now = P[now] now P[now] P now if j > K:
            break
         j > K j K break value = point + (max(cnt, 0) * ((K - j) // span)) value point + (max(cnt, 0) * ((K - j) // span)) point max(cnt, 0) * ((K - j) // span) max(cnt, 0) max cnt 0 (K - j) // span K - j K j span ans = max(ans, value) ans max(ans, value) max ans value print(ans) print ans span += 1 1 span N, K = map(int, input().split()) map(int, input().split()) N cnt = 0 0 cnt ans = max(ans, value) max(ans, value) ans value = point + (max(cnt, 0) * ((K - j) // span)) point + (max(cnt, 0) * ((K - j) // span)) value Q = [int(i) for i in input().split()] [int(i) for i in input().split()] Q ans = - float('inf') - float('inf') ans now = i i now now = P[now] P[now] now now = P[now] P[now] now cnt += Q[now] Q[now] cnt point += Q[now] Q[now] point K = map(int, input().split()) map(int, input().split()) K span = 0 0 span P = [int(i) - 1 for i in input().split()] [int(i) - 1 for i in input().split()] P point = 0 0 point now = i i now
N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
Q = [int(i) for i in input().split()]
ans = - float('inf')
for i in range(N):
    now = i
    cnt = 0
    span = 0
    while True:
        span += 1
        cnt += Q[now]
        now = P[now]
        if now == i:
            break
    point = 0
    now = i
    for j in range(1, span + 1):
        point += Q[now]
        now = P[now]       
        if j > K:
            break
        value = point + (max(cnt, 0) * ((K - j) // span))
        ans = max(ans, value)
print(ans)