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

#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define ss(a) scanf("%s", a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (int i = head[s], v = e[i].to; i; i = e[i].nxt, v = e[i].to)
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
// #define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

const int N = 5100;
typedef long long ll;
int p[N], vis[N];
ll c[N];
ll n, k;
ll res = -1e9 - 10;

ll sol(ll cir[], int size, ll k) {
  if (!k)
    return 0ll;
  ll sum = 0, res = -1e9 - 10, que1[N << 1], que2[N << 1];
  int h = 0, t = -1;
  que1[++t] = 0;
  que2[t] = 0;
  repi(i, 1, size) {
    sum += cir[i];
    while (i - que2[h] > k && h <= t)
      h++;
    res = max(res, sum - que1[h]);
    //		cout<<sum<<' '<<sum - que1[h]<<endl;
    while (t >= h && sum <= que1[t])
      t--;
    que1[++t] = sum;
    que2[t] = i;
    while (i + 1 - que2[h] > k && h <= t)
      h++;
  }
  return res;
}

int main() {
  sl(n), sl(k);
  repi(i, 1, n) { si(p[i]); }
  repi(i, 1, n) { sl(c[i]); }
  ll cir[N << 2], size;
  repi(i, 1, n) {
    if (vis[i])
      continue;
    int t = i;
    ll sum = 0;
    size = 0;
    while (1) {
      vis[t] = 1;
      cir[++size] = c[t];
      sum += c[t];
      if (vis[p[t]])
        break;
      t = p[t];
    }
    repi(i, 1, size) {
      cir[i + size] = cir[i];
      //			cout<<cir[i]<<' ';
    }
    //		cout<<endl;
    //		cout<<sum * (k / size)<<endl;
    res = max(res, sol(cir, 2 * size, k));
    if (sum > 0 && k > size) {
      res = max(res, sum * (k / size) + sol(cir, 2 * size, k % size));
      res = max(res, sum * (k / size - 1) + sol(cir, 2 * size, size));
    }
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define ss(a) scanf("%s", a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (int i = head[s], v = e[i].to; i; i = e[i].nxt, v = e[i].to)
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
// #define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

const int N = 5100;
typedef long long ll;
int p[N], vis[N];
ll c[N];
ll n, k;
ll res = -1e9 - 10;

ll sol(ll cir[], int size, ll k) {
  ll sum = 0, res = -1e9 - 10, que1[N << 1], que2[N << 1];
  int h = 0, t = -1;
  que1[++t] = 0;
  que2[t] = 0;
  repi(i, 1, size) {
    sum += cir[i];
    res = max(res, sum - que1[h]);
    //		cout<<sum<<' '<<sum - que1[h]<<endl;
    while (t >= h && sum <= que1[t])
      t--;
    que1[++t] = sum;
    que2[t] = i;
    if (i + 1 - que2[h] > k && h <= t)
      h++;
  }
  return res;
}

int main() {
  sl(n), sl(k);
  repi(i, 1, n) { si(p[i]); }
  repi(i, 1, n) { sl(c[i]); }
  ll cir[N << 2], size;
  repi(i, 1, n) {
    if (vis[i])
      continue;
    int t = i;
    ll sum = 0;
    size = 0;
    while (1) {
      vis[t] = 1;
      cir[++size] = c[t];
      sum += c[t];
      if (vis[p[t]])
        break;
      t = p[t];
    }
    repi(i, 1, size) {
      cir[i + size] = cir[i];
      cir[i + 2 * size] = cir[i];
      //			cout<<cir[i]<<' ';
    }
    //		cout<<endl;
    //		cout<<sum * (k / size)<<endl;
    res = max(res, sol(cir, 2 * size, k));
    if (sum > 0 && k > size) {
      res = max(res, sum * (k / size) + sol(cir, 2 * size, k % size));
      res =
          max(res, sum * (k / size - 1) + sol(cir, 3 * size, k % size + size));
    }
  }
  cout << res << endl;
  return 0;
}