N, K = map(int, input().split())
moves = [0] + list(map(int, input().split()))
points = [0] + list(map(int, input().split()))
INF = 10**10

arrived = [0 for _ in range(N+1)]
answer = -INF
for move in range(1, N+1):
  if arrived[move] == 1:
    continue
  else:
    arrived[move] = 1
    komas = [move]
    next = moves[move]
    while(arrived[next] == 0):
      arrived[next] = 1
      komas.append(next)
      next = moves[next]
    len_k = len(komas)
    sum_k = [0 for _ in range(len_k*2)]
    sum_k[0] = points[komas[0]]
    for i in range(1, len_k*2):
      sum_k[i] = sum_k[i-1] + points[komas[i%len_k]]
    scores = [-INF for _ in range(len_k+1)]
    for j in range(len_k):
      for k in range(1, len_k+1):
        scores[k] = max(scores[k], sum_k[j+k] - sum_k[j])
    if K > len_k and scores[len_k] > 0:
      max_s = -INF
      max_k = -INF
      for m in range(1, len_k+1):
        if m <= K%len_k:
          max_k = max(max_k, scores[m])
        max_s = max(max_s, scores[m])
      answer = max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1))
    else:
      max_s = -INF
      for m in range(1, min(K, len_k)+1):
        max_s = max(max_s, scores[m])
      answer = max(answer, max_s)
print(answer) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K moves = [0] + list(map(int, input().split())) moves [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 points = [0] + list(map(int, input().split())) points [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 INF = 10**10 INF 10**10 10 10 0 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) 0 arrived = [0 for _ in range(N+1)] arrived [0 for _ in range(N+1)] answer = -INF answer -INF INF for move in range(1, N+1):
  if arrived[move] == 1:
    continue
  else:
    arrived[move] = 1
    komas = [move]
    next = moves[move]
    while(arrived[next] == 0):
      arrived[next] = 1
      komas.append(next)
      next = moves[next]
    len_k = len(komas)
    sum_k = [0 for _ in range(len_k*2)]
    sum_k[0] = points[komas[0]]
    for i in range(1, len_k*2):
      sum_k[i] = sum_k[i-1] + points[komas[i%len_k]]
    scores = [-INF for _ in range(len_k+1)]
    for j in range(len_k):
      for k in range(1, len_k+1):
        scores[k] = max(scores[k], sum_k[j+k] - sum_k[j])
    if K > len_k and scores[len_k] > 0:
      max_s = -INF
      max_k = -INF
      for m in range(1, len_k+1):
        if m <= K%len_k:
          max_k = max(max_k, scores[m])
        max_s = max(max_s, scores[m])
      answer = max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1))
    else:
      max_s = -INF
      for m in range(1, min(K, len_k)+1):
        max_s = max(max_s, scores[m])
      answer = max(answer, max_s) move range(1, N+1) range 1 N+1 N 1 if arrived[move] == 1:
    continue
  else:
    arrived[move] = 1
    komas = [move]
    next = moves[move]
    while(arrived[next] == 0):
      arrived[next] = 1
      komas.append(next)
      next = moves[next]
    len_k = len(komas)
    sum_k = [0 for _ in range(len_k*2)]
    sum_k[0] = points[komas[0]]
    for i in range(1, len_k*2):
      sum_k[i] = sum_k[i-1] + points[komas[i%len_k]]
    scores = [-INF for _ in range(len_k+1)]
    for j in range(len_k):
      for k in range(1, len_k+1):
        scores[k] = max(scores[k], sum_k[j+k] - sum_k[j])
    if K > len_k and scores[len_k] > 0:
      max_s = -INF
      max_k = -INF
      for m in range(1, len_k+1):
        if m <= K%len_k:
          max_k = max(max_k, scores[m])
        max_s = max(max_s, scores[m])
      answer = max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1))
    else:
      max_s = -INF
      for m in range(1, min(K, len_k)+1):
        max_s = max(max_s, scores[m])
      answer = max(answer, max_s) arrived[move] == 1 arrived[move] arrived move 1 continue arrived[move] = 1 arrived[move] arrived move 1 komas = [move] komas [move] move next = moves[move] next moves[move] moves move while(arrived[next] == 0):
      arrived[next] = 1
      komas.append(next)
      next = moves[next]
     arrived[next] == 0 arrived[next] arrived next 0 arrived[next] = 1 arrived[next] arrived next 1 komas.append(next) komas.append komas append next next = moves[next] next moves[next] moves next len_k = len(komas) len_k len(komas) len komas 0 for _ in range(len_k*2) for _ in range(len_k*2) _ range(len_k*2) range len_k*2 len_k 2 for _ in range(len_k*2) 0 sum_k = [0 for _ in range(len_k*2)] sum_k [0 for _ in range(len_k*2)] sum_k[0] = points[komas[0]] sum_k[0] sum_k 0 points[komas[0]] points komas[0] komas 0 for i in range(1, len_k*2):
      sum_k[i] = sum_k[i-1] + points[komas[i%len_k]]
     i range(1, len_k*2) range 1 len_k*2 len_k 2 sum_k[i] = sum_k[i-1] + points[komas[i%len_k]] sum_k[i] sum_k i sum_k[i-1] + points[komas[i%len_k]] sum_k[i-1] sum_k i-1 i 1 points[komas[i%len_k]] points komas[i%len_k] komas i%len_k i len_k -INF for _ in range(len_k+1) for _ in range(len_k+1) _ range(len_k+1) range len_k+1 len_k 1 for _ in range(len_k+1) -INF INF scores = [-INF for _ in range(len_k+1)] scores [-INF for _ in range(len_k+1)] for j in range(len_k):
      for k in range(1, len_k+1):
        scores[k] = max(scores[k], sum_k[j+k] - sum_k[j])
     j range(len_k) range len_k for k in range(1, len_k+1):
        scores[k] = max(scores[k], sum_k[j+k] - sum_k[j])
     k range(1, len_k+1) range 1 len_k+1 len_k 1 scores[k] = max(scores[k], sum_k[j+k] - sum_k[j]) scores[k] scores k max(scores[k], sum_k[j+k] - sum_k[j]) max scores[k] scores k sum_k[j+k] - sum_k[j] sum_k[j+k] sum_k j+k j k sum_k[j] sum_k j if K > len_k and scores[len_k] > 0:
      max_s = -INF
      max_k = -INF
      for m in range(1, len_k+1):
        if m <= K%len_k:
          max_k = max(max_k, scores[m])
        max_s = max(max_s, scores[m])
      answer = max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1))
    else:
      max_s = -INF
      for m in range(1, min(K, len_k)+1):
        max_s = max(max_s, scores[m])
      answer = max(answer, max_s) K > len_k and scores[len_k] > 0 K > len_k K len_k scores[len_k] > 0 scores[len_k] scores len_k 0 max_s = -INF max_s -INF INF max_k = -INF max_k -INF INF for m in range(1, len_k+1):
        if m <= K%len_k:
          max_k = max(max_k, scores[m])
        max_s = max(max_s, scores[m])
       m range(1, len_k+1) range 1 len_k+1 len_k 1 if m <= K%len_k:
          max_k = max(max_k, scores[m])
         m <= K%len_k m K%len_k K len_k max_k = max(max_k, scores[m]) max_k max(max_k, scores[m]) max max_k scores[m] scores m max_s = max(max_s, scores[m]) max_s max(max_s, scores[m]) max max_s scores[m] scores m answer = max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1)) answer max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1)) max answer max_k+scores[len_k]*(K//len_k) max_k scores[len_k]*(K//len_k) scores[len_k] scores len_k K//len_k K len_k max_s+scores[len_k]*(K//len_k-1) max_s scores[len_k]*(K//len_k-1) scores[len_k] scores len_k K//len_k-1 K//len_k K len_k 1 max_s = -INF max_s -INF INF for m in range(1, min(K, len_k)+1):
        max_s = max(max_s, scores[m])
       m range(1, min(K, len_k)+1) range 1 min(K, len_k)+1 min(K, len_k) min K len_k 1 max_s = max(max_s, scores[m]) max_s max(max_s, scores[m]) max max_s scores[m] scores m answer = max(answer, max_s) answer max(answer, max_s) max answer max_s print(answer) print answer K = map(int, input().split()) map(int, input().split()) K answer = max(answer, max_s) max(answer, max_s) answer answer = max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1)) max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1)) answer next = moves[move] moves[move] next moves = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) moves INF = 10**10 10**10 INF arrived = [0 for _ in range(N+1)] [0 for _ in range(N+1)] arrived len_k = len(komas) len(komas) len_k scores = [-INF for _ in range(len_k+1)] [-INF for _ in range(len_k+1)] scores max_s = -INF -INF max_s max_s = max(max_s, scores[m]) max(max_s, scores[m]) max_s max_s = -INF -INF max_s max_k = -INF -INF max_k max_k = max(max_k, scores[m]) max(max_k, scores[m]) max_k sum_k = [0 for _ in range(len_k*2)] [0 for _ in range(len_k*2)] sum_k points = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) points next = moves[next] moves[next] next answer = -INF -INF answer komas = [move] [move] komas N, K = map(int, input().split()) map(int, input().split()) N max_s = max(max_s, scores[m]) max(max_s, scores[m]) max_s
N, K = map(int, input().split())
moves = [0] + list(map(int, input().split()))
points = [0] + list(map(int, input().split()))
INF = 10**10

arrived = [0 for _ in range(N+1)]
answer = -INF
for move in range(1, N+1):
  if arrived[move] == 1:
    continue
  else:
    arrived[move] = 1
    komas = [move]
    next = moves[move]
    while(arrived[next] == 0):
      arrived[next] = 1
      komas.append(next)
      next = moves[next]
    len_k = len(komas)
    sum_k = [0 for _ in range(len_k*2)]
    sum_k[0] = points[komas[0]]
    for i in range(1, len_k*2):
      sum_k[i] = sum_k[i-1] + points[komas[i%len_k]]
    scores = [-INF for _ in range(len_k+1)]
    for j in range(len_k):
      for k in range(1, len_k+1):
        scores[k] = max(scores[k], sum_k[j+k] - sum_k[j])
    if K > len_k and scores[len_k] > 0:
      max_s = -INF
      max_k = -INF
      for m in range(1, len_k+1):
        if m <= K%len_k:
          max_k = max(max_k, scores[m])
        max_s = max(max_s, scores[m])
      answer = max(answer, max_k+scores[len_k]*(K//len_k), max_s+scores[len_k]*(K//len_k-1))
    else:
      max_s = -INF
      for m in range(1, min(K, len_k)+1):
        max_s = max(max_s, scores[m])
      answer = max(answer, max_s)
print(answer)