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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 7;

vector<ll> used, p, c;
vector<vector<ll>> loop;
ll cnt = 0;
void dfs(ll v) {
  if (used[v]) {
    return;
  }
  used[v] = 1;
  loop[cnt].push_back(v);
  dfs(p[v]);
}

int main() {
  ios;
  ll n, k;
  cin >> n >> k;
  used.resize(n);
  p.resize(n);
  c.resize(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }

  loop.resize(n);
  rep(i, n) {
    if (used[i] == 0) {
      dfs(i);
      cnt++;
    }
  }
  ll ans = -INF;

  rep(i, cnt) {
    ll m = loop[i].size();
    ll tot = 0;
    ll r = (k - 1) / m;
    ll t = k - r * m;
    ll sum = 0;

    vector<ll> s(2 * m + 1);
    rep(j, m) {
      sum += c[loop[i][j]];
      // s[j+1]=s[j]+c[loop[i][j]];
    }
    /*rep(j,m){
        s[m+j+1]=s[m+j]+c[loop[i][j]];
    }*/
    if (sum > 0) {
      tot += r * sum;
      ans = max(ans, tot);
    }
    vector<ll> q(2 * m);
    rep(j, m) {
      tot += c[loop[i][j]];
      q[j] = tot;
    }
    if (sum <= 0 && r >= 1)
      t = m;
    rep(j, t) {
      rep(k, m) {
        ans = max(ans, q[j]);
        q[j] -= c[loop[i][(0 + k) % m]];
        q[j] += c[loop[i][(j + 1 + k) % m]];
        ans = max(ans, q[j]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 7;

vector<ll> used, p, c;
vector<vector<ll>> loop;
ll cnt = 0;
void dfs(ll v) {
  if (used[v]) {
    return;
  }
  used[v] = 1;
  loop[cnt].push_back(v);
  dfs(p[v]);
}

int main() {
  ll n, k;
  cin >> n >> k;
  used.resize(n);
  p.resize(n);
  c.resize(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) { cin >> c[i]; }

  loop.resize(n);
  rep(i, n) {
    if (used[i] == 0) {
      dfs(i);
      cnt++;
    }
  }
  ll ans = -INF;

  rep(i, cnt) {
    ll m = loop[i].size();
    ll tot = 0;
    ll r = k / m;
    ll t = k % m;
    ll sum = 0;

    vector<ll> s(2 * m + 1);
    rep(j, m) {
      sum += c[loop[i][j]];
      // s[j+1]=s[j]+c[loop[i][j]];
    }
    /*rep(j,m){
        s[m+j+1]=s[m+j]+c[loop[i][j]];
    }*/
    if (sum > 0) {
      tot += r * sum;
      ans = max(ans, tot);
    }
    vector<ll> q(m);
    rep(j, m) {
      tot += c[loop[i][j]];
      q[j] = tot;
      ans = max(ans, q[j]);
    }
    rep(j, m) {
      // cout << i << " " << q[j] << endl;
    }
    // cout << i << " " << tot << endl;
    if (sum <= 0)
      t = k;
    rep(j, t) {
      rep(k, m) {
        ans = max(ans, q[j]);
        q[j] -= c[loop[i][(0 + k) % m]];
        q[j] += c[loop[i][(j + 1 + k) % m]];
        ans = max(ans, q[j]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}