#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < n; i++)

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> p(n), c(n);
  for (int &i : p) {
    cin >> i;
    i--;
  }
  for (int &i : c) cin >> i;
  ll ans = -1001001001;
  for (int i = 0; i < n; i++) {
    int now = i;
    vector<ll> vs;
    ll score = 0;
    while (true) {
      now = p[now];
      score += c[now];
      vs.push_back(score);
      if (now == i) break;
    }
    int sz = vs.size();
    for (int j = 0; j < sz; j++) {
      if (j + 1 > k) break;
      ll cnt = (k - j - 1) / sz;
      if (score <= 0) ans = max(ans, vs[j]);
      else ans = max(ans, vs[j] + cnt * score);
    }
  }
  cout << ans << endl;
  return 0;
}