Problem p02585 (C++) — buggy submission, verdict: Runtime Error

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 08/15/20 20:19:02
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  vector<int> P(n);
  for (auto &p : P) {
    cin >> p;
    p--;
  }
  vector<int> C(n);
  for (auto &c : C)
    cin >> c;
  vector<bool> vis(n);
  auto dfs = [&](auto f, int s, ll sum, vector<int> &path) {
    if (vis[s]) {
      return sum;
    }
    vis[s] = true;
    path.push_back(C[s]);
    return f(f, P[s], sum + C[s], path);
  };
  ll ans = -2e18;
  for (int s = 0; s < n; s++) {
    if (vis[s])
      continue;
    vector<int> path;
    ll sum = dfs(dfs, s, 0, path);
    assert(!path.empty());
    ll m = path.size();
    if (m == 1) {
      ll score = sum > 0 ? k * sum : sum;
      ans = max(ans, score);
      continue;
    }
    vector<ll> cc(m * 2);
    for (int i = 0; i < m * 2; i++) {
      cc[i] = path[i % m];
      if (i > 0)
        cc[i] += cc[i - 1];
    }
    if (sum > 0 and k / m > 0)
      ans = max(ans, sum * (k / m));
    for (int len = 1; len < m; len++) {
      if (len > k)
        break;
      ll bs = -2e18;
      for (int i = 0; i < m; i++) {
        ll cs = cc[i + len - 1];
        if (i > 0)
          cs -= cc[i - 1];
        bs = max(bs, cs);
      }
      if (sum > 0) {
        if (len <= k % m) {
          bs += sum * (k / m);
        } else {
          bs += sum * (k / m - 1);
        }
      }
      ans = max(ans, bs);
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 08/15/20 20:19:02
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  vector<int> P(n);
  for (auto &p : P) {
    cin >> p;
    p--;
  }
  vector<int> C(n);
  for (auto &c : C)
    cin >> c;
  vector<bool> vis(n);
  auto dfs = [&](auto f, int s, ll sum, vector<int> &path) {
    if (vis[s]) {
      return sum;
    }
    vis[s] = true;
    path.push_back(C[s]);
    return f(f, P[s], sum + C[s], path);
  };
  ll ans = -2e18;
  for (int s = 0; s < n; s++) {
    if (vis[s])
      continue;
    vector<int> path;
    ll sum = dfs(dfs, s, 0, path);
    assert(!path.empty());
    ll m = path.size();
    if (m == 1) {
      ll score = sum > 0 ? k * sum : sum;
      ans = max(ans, score);
      continue;
    }
    ll score = 0;
    ll kk = k;
    if (k >= m * 2 and sum > 0) {
      score += sum * (k / m - 1);
      kk = k - (k / m - 1) * m;
    }
    vector<ll> cpt(m * 3);
    for (int i = 0; i < m * 3; i++) {
      cpt[i] = path[i % m];
      if (i > 0)
        cpt[i] += cpt[i - 1];
    }
    ll max_score = -2e18;
    for (int i = 0; i < m; i++) {
      for (int len = 1; len <= kk; len++) {
        ll sc = cpt[i + len - 1];
        if (i > 0)
          sc -= cpt[i - 1];
        max_score = max(max_score, sc);
      }
    }
    ans = max(ans, score + max_score);
  }
  cout << ans << endl;

  return 0;
}