#include <bits/stdc++.h>

#if DEBUG
template <typename Iter>
void debug_iter(Iter begin, Iter end);
template <typename T>
void debug(const T& value) {
  std::cerr << value;
}
template <typename T>
void debug(const std::vector<T>& v) {
  debug_iter(v.begin(), v.end());
}
template <typename T>
void debug(const std::set<T>& s) {
  debug_iter(s.begin(), s.end());
}
template <typename T, typename U>
void debug(const std::map<T, U>& m) {
  debug_iter(m.begin(), m.end());
}
template <typename T, typename U>
void debug(const std::pair<T, U>& p) {
  std::cerr << "{";
  debug(p.first);
  std::cerr << ", ";
  debug(p.second);
  std::cerr << "}";
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
  debug(value);
  std::cerr << ", ";
  debug(args...);
}
template <typename Iter>
void debug_iter(Iter begin, Iter end) {
  std::cerr << "{";
  for (Iter it = begin; it != end; ++it) {
    if (it != begin) std::cerr << ", ";
    debug(*it);
  }
  std::cerr << "}";
}
#define dbg(...)                        \
  do {                                  \
    cerr << #__VA_ARGS__ << ": ";       \
    debug(__VA_ARGS__);                 \
    cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << '\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
  for (T& vi : v) is >> vi;
  return is;
}

template <typename T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#include <boost/hana/functional/fix.hpp>
auto Fix = [](auto&& f) { return boost::hana::fix(f); };

using ll = long long;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

using namespace std;

int main() {
#define int ll
  rd(int, n, k);
  V<int> p(n), c(n);
  cin >> p >> c;
  int ans = numeric_limits<int>::min();
  rep(init, n) {
    // int init = 7;
    V<int> score(n, -1);
    V<int> prev(n, -1);
    score[init] = 0;
    prev[init] = 0;
    int pos = init;
    int curs = 0;
    for (int move = 1; move <= k; ++move) {
      int npos = p[pos] - 1;
      curs += c[npos];
      chmax(ans, curs);
      dbg(init + 1, move, npos + 1, curs);
      int ps = score[npos];
      if (ps == -1) {
        score[npos] = curs;
        prev[npos] = move;
        pos = npos;
        continue;
      }
      dbg("same", curs - ps);
      dbg(prev[npos], move, npos);
      if (curs <= ps) {
        chmax(ans, curs);
        break;
      }
      int pm = prev[npos];
      int rk = k - move;
      int lm = move - pm;
      if (lm <= rk) {
        int repeat = rk / lm;
        if (repeat >= 2) {
          --repeat;
          dbg(pm, rk, lm, repeat);
          curs += repeat * (curs - ps);
          move += repeat * lm;
          dbg(curs, move);
          chmax(ans, curs);
        }
      }
      score[npos] = curs;
      prev[npos] = move;
      pos = npos;
    }
  }
  wt(ans);
#undef int
}


Measured memory_bytes: 3772