#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
const int N = 5050;
long long sum[N][N];
long long maxd[N][N];
long long k;
int n;
int p[N];
int C[N];
bool st[N];
int root;
map<int, int> mp;
bool flag;
void dfs(int u, int stp) {
  if (u == root && flag) {
    mp[root] = stp - 1;
    sum[root][stp] = sum[root][stp - 1] + C[u];
    maxd[root][stp] = max(maxd[root][stp - 1], sum[root][stp]);
    return;
  }
  if (u == root) flag = true;
  sum[root][stp] = sum[root][stp - 1] + C[p[u]];
  maxd[root][stp] = max(maxd[root][stp - 1], sum[root][stp]);
  dfs(p[u], stp + 1);
}
int main() {
  cin >> n;
  cin >> k;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) maxd[i][j] = -2e18;
  for (int i = 1; i <= n; i++) cin >> C[i];
  for (int i = 1; i <= n; i++) {
    flag = false;
    root = i;
    dfs(i, 1);
  }
  long long res = -1e18;
  for (int i = 1; i <= n; i++) {
    int len = mp[i];
    long long maxv = -2e18;
    if (k <= len) {
      maxv = maxd[i][k];
    } else if (k > len) {
      long long maxv2 = -1e18;
      maxv2 = maxv = maxd[i][len];
      long long ct = k / len;
      long long ct2 = k % len;
      long long cc2 = 0;
      cc2 = max(0ll, maxd[i][ct2]);
      if (sum[i][len] > 0) {
        maxv = max(maxv, sum[i][len] * (ct - 1) + maxv2);
        maxv = max(maxv, ct * sum[i][len] + cc2);
      }
    }
    res = max(res, maxv);
  }
  cout << res << endl;
}
