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

/* Author: $%U%$
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/
#include <bits/stdc++.h>
using namespace std;
#define fore(i, a, b) for (int i = a, to = b; i < to; i++)
#define foref(i, a, b) for (int i = b - 1, to = a; i >= to; i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(v) (int)v.size()
#define pb push_back
typedef long long Long;
typedef pair<int, int> Pair;
const int MX = 5e3;
const Long INF = 1e15;
Long A[MX + 2];
Long C[MX + 2];
Long n, k;
Long maxi(vector<Long> &v, Long val) {
  if (val == 0)
    return 0LL;
  Long ans = -INF;
  Long cur = 0;
  fore(i, 0, val) {
    cur += v[i];
    ans = max(ans, cur);
  }
  return ans;
}
Long fun(vector<Long> &l, vector<Long> &r) {
  if (k <= SZ(l)) {
    return maxi(l, k);
  }
  Long auxk = k;
  auxk -= SZ(l);

  Long ans = -INF;
  Long tot = accumulate(all(l), 0LL);
  if (SZ(l) > 0) {
    ans = max(ans, maxi(l, SZ(l)));
  }
  Long ciclo = SZ(r);

  Long cntCiclo = auxk / ciclo;
  Long extra = auxk % ciclo;
  Long valorCiclo = accumulate(all(r), 0LL);
  // cout << cntCiclo << " " << extra << endl;
  if (cntCiclo > 0) {
    // cout << tot + maxi(r, SZ(r)) << " " << tot + cntCiclo * valorCiclo +
    // maxi(r, extra) << endl;

    ans = max(ans, tot + maxi(r, SZ(r)));
    ans = max(ans, tot + (cntCiclo - 1) * valorCiclo + maxi(r, SZ(r)));
    ans = max(ans, tot + cntCiclo * valorCiclo + maxi(r, extra));
  } else {
    assert(extra != 0);
    ans = max(ans, tot + maxi(r, extra));
  }
  return ans;
}
Long vis[MX + 2];
Long f(Long x) {
  memset(vis, 0, sizeof vis);
  vector<Long> values;
  vector<int> index;
  x = A[x];
  while (vis[x] == 0) {
    vis[x] = 1;
    index.pb(x);
    values.pb(C[x]);
    x = A[x];
  }
  vector<Long> l, r;
  bool ok = true;
  fore(i, 0, SZ(values)) {
    if (index[i] == x)
      ok = false;
    if (ok)
      l.pb(values[i]);
    else
      r.pb(values[i]);
  }

  return fun(l, r);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> k;
  Long ans = -INF;
  fore(i, 0, n) cin >> A[i];
  fore(i, 0, n) A[i]--;
  fore(i, 0, n) cin >> C[i];
  fore(i, 0, n) { ans = max(ans, f(i)); }
  cout << ans << '\\n';

  return 0;
}

Error: -6
f3f47eb9-7bae-452b-b358-ac6dacacf8d1.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02585/C++/s671315774.cpp:52: Long fun(std::vector<long long int>&, std::vector<long long int>&): Assertion `extra == 0' failed.


Fixed submission:

/* Author: $%U%$
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/
#include <bits/stdc++.h>
using namespace std;
#define fore(i, a, b) for (int i = a, to = b; i < to; i++)
#define foref(i, a, b) for (int i = b - 1, to = a; i >= to; i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(v) (int)v.size()
#define pb push_back
typedef long long Long;
typedef pair<int, int> Pair;
const int MX = 5e3;
const Long INF = 1e15;
Long A[MX + 2];
Long C[MX + 2];
Long n, k;
Long maxi(vector<Long> &v, Long val) {
  if (val == 0)
    return 0LL;
  Long ans = -INF;
  Long cur = 0;
  fore(i, 0, val) {
    cur += v[i];
    ans = max(ans, cur);
  }
  return ans;
}
Long fun(vector<Long> &l, vector<Long> &r) {
  if (k <= SZ(l)) {
    return maxi(l, k);
  }
  Long auxk = k;
  auxk -= SZ(l);

  Long ans = -INF;
  Long tot = accumulate(all(l), 0LL);
  if (SZ(l) > 0) {
    ans = max(ans, maxi(l, SZ(l)));
  }
  Long ciclo = SZ(r);

  Long cntCiclo = auxk / ciclo;
  Long extra = auxk % ciclo;
  Long valorCiclo = accumulate(all(r), 0LL);
  if (cntCiclo > 0) {
    // cout << tot + maxi(r, SZ(r)) << " " << tot + cntCiclo * valorCiclo +
    // maxi(r, extra) << endl;

    ans = max(ans, tot + maxi(r, SZ(r)));
    ans = max(ans, tot + cntCiclo * valorCiclo + maxi(r, extra));
  } else {
    assert(extra == 0);
    ans = max(ans, tot + maxi(r, extra));
  }
  return ans;
}
Long vis[MX + 2];
Long f(Long x) {
  memset(vis, 0, sizeof vis);
  vector<Long> values;
  vector<int> index;
  x = A[x];
  while (vis[x] == 0) {
    vis[x] = 1;
    index.pb(x);
    values.pb(C[x]);
    x = A[x];
  }
  vector<Long> l, r;
  bool ok = true;
  fore(i, 0, SZ(values)) {
    if (index[i] == x)
      ok = false;
    if (ok)
      l.pb(values[i]);
    else
      r.pb(values[i]);
  }

  return fun(l, r);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> k;
  Long ans = -INF;
  fore(i, 0, n) cin >> A[i];
  fore(i, 0, n) A[i]--;
  fore(i, 0, n) cin >> C[i];
  fore(i, 0, n) { ans = max(ans, f(i)); }
  cout << ans << '\\n';

  return 0;
}