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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int KIND = 26;

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  srand((unsigned)time(NULL));
  int D;
  cin >> D;
  vi c(KIND);
  rep(i, KIND) cin >> c[i];
  vector<vi> s(D, vi(KIND));
  rep(i, D) rep(j, KIND) cin >> s[i][j];
  vi ans(D);
  vi cand(D);
  vi damage(KIND);
  vi weight(KIND);
  vi wsum(KIND);
  int r, idx, V;
  int Vmax = INT_MIN;
  while ((double)(clock() - ti) / CLOCKS_PER_SEC < 1.98) {
    V = 0;
    rep(i, KIND) damage[i] = 0;
    rep(i, D) {
      rep(j, KIND) {
        damage[j] += c[j];
        weight[j] = s[i][j] + damage[j];
        wsum[j] = weight[j];
        V -= damage[j];
      }
      rep(j, KIND - 1) wsum[j + 1] += wsum[j];
      r = rand() % wsum[KIND - 1];
      idx = upper_bound(all(wsum), r) - wsum.begin();
      // cout<<r<<"/"<<wsum[KIND-1]<<" "<<idx<<endl;
      cand[i] = idx;
      damage[idx] = 0;
      V += weight[idx];
    }
    if (V > Vmax) {
      ans.swap(cand);
      Vmax = V;
    }
  }
  rep(i, D) cout << ans[i] + 1 << "\\n";
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int KIND = 26;

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  srand((unsigned)time(NULL));
  int D;
  cin >> D;
  vi c(KIND);
  rep(i, KIND) cin >> c[i];
  vector<vi> s(D, vi(KIND));
  rep(i, D) rep(j, KIND) cin >> s[i][j];
  vi ans;
  int Vmax = 0;
  int Q = 3;
  while ((double)(clock() - ti) / CLOCKS_PER_SEC < 1.95) {
    vi cand(D);
    int V = 0;
    vi damage(KIND);
    rep(i, D) {
      vi weight(KIND);
      vi wsum(KIND);
      rep(j, KIND) {
        damage[j] += c[j];
        weight[j] = s[i][j] + damage[j];
        wsum[j] = weight[j];
        V -= damage[j];
      }
      rep(j, KIND - 1) wsum[j + 1] += wsum[j];
      int r = (int)rand() % wsum[KIND - 1];
      int idx = upper_bound(all(wsum), r) - wsum.begin();
      cand[i] = idx;
      damage[idx] = 0;
      V += weight[idx];
    }
    if (V > Vmax) {
      ans.swap(cand);
      Vmax = V;
    }
  }
  rep(i, D) cout << ans[i] << "\\n";
  return 0;
}