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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define VIEW(x)                                                                \\
  do {                                                                         \\
    cerr << #x << ": ";                                                        \\
    for (auto i : x)                                                           \\
      cerr << i << " ";                                                        \\
    cerr << endl;                                                              \\
  } while (0)

signed main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];

  vector<int> loopsize(n);
  vector<ll> loopvalue(n);
  vector<vector<ll>> a(n, vector<ll>(n, 0));

  rep(i, n) {
    ll current = i;
    ll ss = 0;
    ll val = 0;
    rep(j, n) {
      ss++;
      ll pnext = p[current];
      val += c[pnext];
      a[i][j] = c[pnext] + (j > 0 ? a[i][j - 1] : 0);
      current = pnext;
      if (current == i) {
        loopsize[i] = ss;
        loopvalue[i] = val;
        break;
      }
    }
  }

  ll ans = -1000000000;
  rep(i, n) {
    if (loopvalue[i] <= 0) {
      rep(j, loopsize[i]) ans = max(ans, a[i][j]);
    } else {
      ll loops = k / loopsize[i];

      ll base = loops * loopvalue[i];
      ans = max(ans, base);
      rep(j, k % loopsize[i]) ans = max(ans, base + a[i][j]);

      ll base2 = (loops - 1) * loopvalue[i];
      rep(j, n) ans = max(ans, base2 + a[i][j]);
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define VIEW(x)                                                                \\
  do {                                                                         \\
    cerr << #x << ": ";                                                        \\
    for (auto i : x)                                                           \\
      cerr << i << " ";                                                        \\
    cerr << endl;                                                              \\
  } while (0)

signed main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];

  vector<int> loopsize(n);
  vector<ll> loopvalue(n);

  rep(i, n) {
    ll current = i;
    ll ss = 0;
    ll val = 0;
    rep(j, n) {
      ss++;
      ll pnext = p[current];
      val += c[pnext];
      current = pnext;
      if (current == i) {
        loopsize[i] = ss;
        loopvalue[i] = val;
        break;
      }
    }
  }

  vector<vector<ll>> a(n, vector<ll>(n, 0));
  rep(i, n) {
    ll current = i;
    rep(j, n) {
      ll pnext = p[current];
      a[i][j] = c[pnext] + (j > 0 ? a[i][j - 1] : 0);
      current = pnext;
    }
  }

  ll ans = -1000000000;
  if (k <= n) {
    rep(i, n) rep(j, k) ans = max(ans, a[i][j]);
    cout << ans << endl;
  } else {
    rep(i, n) {
      if (loopvalue[i] <= 0) {
        rep(j, k) ans = max(ans, a[i][j]);
      } else {
        ll loops = k / loopsize[i];
        ll base = loops * loopvalue[i];
        ans = max(ans, base);
        rep(j, k % loopsize[i]) ans = max(ans, base + a[i][j]);
        ll base2 = (loops - 1) * loopvalue[i];
        rep(j, n) ans = max(ans, base2 + a[i][j]);
      }
    }
    cout << ans << endl;
  }
}