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

#include "bits/stdc++.h"

#define REP(i, num) for (ll i = 0; i < (num); ++i)
#define FOR(i, c, num) for (ll(i) = (c); (i) < (num); ++(i))
#define LOOP(i) while (i--)
#define ALL(c) c.begin(), c.end()
#define PRINTALL(c)                                                            \\
  for (auto pitr = c.begin(); pitr != c.end(); ++pitr) {                       \\
    cout << *pitr;                                                             \\
    if (next(pitr, 1) != c.end())                                              \\
      cout << ' ';                                                             \\
  }                                                                            \\
  cout << endl;
#define PAIRCOMP(c, comp)                                                      \\
  [](const pair<ll, ll> &lhs, const pair<ll, ll> &rhs) {                       \\
    return lhs.c comp rhs.c;                                                   \\
  }

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9 + 7;

template <typename T = ll> T in() {
  T x;
  cin >> x;
  return (x);
}
template <typename T = ll, typename C = vector<T>> C vecin(int N) {
  C x(N);
  REP(i, N) { x[i] = in<T>(); }
  return x;
}

void vout() { cout << endl; }
template <typename Head, typename... Tail> void vout(Head &&h, Tail &&...t) {
  cout << ' ' << h;
  vout(forward<Tail>(t)...);
}
void out() { cout << endl; }
template <typename Head, typename... Tail> void out(Head &&h, Tail &&...t) {
  cout << h;
  vout(forward<Tail>(t)...);
}

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

class Data {
public:
  vll H;
  ll S{-(1ll << 60)};

  void CalculateScore(vll &C, vvll &M) {
    if (S > -(1ll << 60))
      return;
    vll L(26, 0);
    auto D = H.size();
    ll A = 0;
    REP(i, D) {
      ll d = i + 1;
      A += M[i][H[i]];
      REP(j, 26) {
        if (H[i] != j) {
          A -= C[j] * (d - L[j]);
        }
      }
    }
    S = A;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  auto D = in();
  auto C = vecin(26);
  vvll S(D, vll(26));
  REP(i, D) { REP(j, 26) S[i][j] = in(); }

  ll sample_size = 500;
  mt19937 rand_type_src(12345);
  uniform_int_distribution<int> rand_type_dist(0, 25);
  uniform_int_distribution<int> rand_parent_dist(0, sample_size - 1);
  uniform_int_distribution<int> rand_day_dist(0, D - 1);
  uniform_real_distribution<double> rand_prob_dist(0, 1.0);

  vector<Data> Sample(sample_size), Sample_buff(950);
  for (auto &x : Sample) {
    REP(i, D) x.H.push_back(rand_type_dist(rand_type_src));
    x.CalculateScore(C, S);
  }
  sort(ALL(Sample), [](Data &l, Data &r) { return l.S > r.S; });

  ll Q = 10;
  LOOP(Q) {
    REP(i, sample_size) {
      Sample_buff[i].S = Sample[i].S;
      Sample_buff[i].H = Sample[i].H;
    }
    REP(i, 200) {
      ll X = rand_parent_dist(rand_type_src);
      ll Y;
      do {
        Y = rand_parent_dist(rand_type_src);
      } while (X == Y);
      Data SampleA, SampleB;
      SampleA.H.resize(D);
      SampleB.H.resize(D);
      REP(j, D) {
        if (rand_prob_dist(rand_type_src) < 0.5) {
          SampleA.H[j] = Sample[X].H[j];
          SampleB.H[j] = Sample[Y].H[j];
        } else {
          SampleB.H[j] = Sample[X].H[j];
          SampleA.H[j] = Sample[Y].H[j];
        }
      }
      Sample_buff[sample_size + 2 * i].H = SampleA.H;
      Sample_buff[sample_size + 2 * i + 1].H = SampleB.H;
      Sample_buff[sample_size + 2 * i].S = SampleA.S;
      Sample_buff[sample_size + 2 * i + 1].S = SampleB.S;
    }
    REP(i, 50) {
      ll X = rand_parent_dist(rand_type_src);
      ll N = max(1ll, D / 70ll);
      Data SampleA;
      SampleA.H = Sample[X].H;
      REP(j, N) {
        SampleA.H[rand_day_dist(rand_type_src)] = rand_type_dist(rand_type_src);
      }
      Sample_buff[900 + i].S = SampleA.S;
      Sample_buff[900 + i].H = SampleA.H;
    }
    for (auto &x : Sample_buff)
      x.CalculateScore(C, S);
    sort(ALL(Sample_buff), [](Data &l, Data &r) { return l.S > r.S; });
    REP(i, sample_size) {
      Sample[i].S = Sample_buff[i].S;
      Sample[i].H = Sample_buff[i].H;
    }
  }

  for (auto &x : Sample[0].H)
    out(x + 1);
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_array_new_length'
  what():  std::bad_array_new_length


Fixed submission:

#include "bits/stdc++.h"

#define REP(i, num) for (ll i = 0; i < (num); ++i)
#define FOR(i, c, num) for (ll(i) = (c); (i) < (num); ++(i))
#define LOOP(i) while (i--)
#define ALL(c) c.begin(), c.end()
#define PRINTALL(c)                                                            \\
  for (auto pitr = c.begin(); pitr != c.end(); ++pitr) {                       \\
    cout << *pitr;                                                             \\
    if (next(pitr, 1) != c.end())                                              \\
      cout << ' ';                                                             \\
  }                                                                            \\
  cout << endl;
#define PAIRCOMP(c, comp)                                                      \\
  [](const pair<ll, ll> &lhs, const pair<ll, ll> &rhs) {                       \\
    return lhs.c comp rhs.c;                                                   \\
  }

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9 + 7;

template <typename T = ll> T in() {
  T x;
  cin >> x;
  return (x);
}
template <typename T = ll, typename C = vector<T>> C vecin(int N) {
  C x(N);
  REP(i, N) { x[i] = in<T>(); }
  return x;
}

void vout() { cout << endl; }
template <typename Head, typename... Tail> void vout(Head &&h, Tail &&...t) {
  cout << ' ' << h;
  vout(forward<Tail>(t)...);
}
void out() { cout << endl; }
template <typename Head, typename... Tail> void out(Head &&h, Tail &&...t) {
  cout << h;
  vout(forward<Tail>(t)...);
}

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

class Data {
public:
  vll H;
  ll S{-(1ll << 60)};

  void CalculateScore(vll &C, vvll &M) {
    if (S > -(1ll << 60))
      return;
    vll L(26, 0);
    auto D = H.size();
    ll A = 0;
    REP(i, D) {
      ll d = i + 1;
      A += M[i][H[i]];
      REP(j, 26) {
        if (H[i] != j) {
          A -= C[j] * (d - L[j]);
        }
      }
    }
    S = A;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  auto D = in();
  auto C = vecin(26);
  vvll S(D, vll(26));
  REP(i, D) { REP(j, 26) S[i][j] = in(); }

  mt19937 rand_type_src(12345);
  uniform_int_distribution<int> rand_type_dist(0, 25);
  uniform_int_distribution<int> rand_parent_dist(0, 999);
  uniform_int_distribution<int> rand_day_dist(0, D - 1);
  uniform_real_distribution<double> rand_prob_dist(0, 1.0);

  ll sample_size = 500;
  vector<Data> Sample(sample_size), Sample_buff(950);
  for (auto &x : Sample) {
    REP(i, D) x.H.push_back(rand_type_dist(rand_type_src));
    x.CalculateScore(C, S);
  }
  sort(ALL(Sample), [](Data &l, Data &r) { return l.S > r.S; });

  ll Q = 10;
  LOOP(Q) {
    REP(i, sample_size) {
      Sample_buff[i].S = Sample[i].S;
      Sample_buff[i].H = Sample[i].H;
    }
    REP(i, 200) {
      ll X = rand_parent_dist(rand_type_src);
      ll Y;
      do {
        Y = rand_parent_dist(rand_type_src);
      } while (X == Y);
      Data SampleA, SampleB;
      SampleA.H.resize(D);
      SampleB.H.resize(D);
      REP(j, D) {
        if (rand_prob_dist(rand_type_src) < 0.5) {
          SampleA.H[j] = Sample[X].H[j];
          SampleB.H[j] = Sample[Y].H[j];
        } else {
          SampleB.H[j] = Sample[X].H[j];
          SampleA.H[j] = Sample[Y].H[j];
        }
      }
      Sample_buff[sample_size + 2 * i].H = SampleA.H;
      Sample_buff[sample_size + 2 * i + 1].H = SampleB.H;
      Sample_buff[sample_size + 2 * i].S = SampleA.S;
      Sample_buff[sample_size + 2 * i + 1].S = SampleB.S;
    }
    REP(i, 50) {
      ll X = rand_parent_dist(rand_type_src);
      ll N = max(1ll, D / 70ll);
      Data SampleA;
      SampleA.H = Sample[X].H;
      REP(j, N) {
        SampleA.H[rand_day_dist(rand_type_src)] = rand_type_dist(rand_type_src);
      }
      Sample_buff[900 + i].S = SampleA.S;
      Sample_buff[900 + i].H = SampleA.H;
    }
    for (auto &x : Sample_buff)
      x.CalculateScore(C, S);
    sort(ALL(Sample_buff), [](Data &l, Data &r) { return l.S > r.S; });
    REP(i, sample_size) {
      Sample[i].S = Sample_buff[i].S;
      Sample[i].H = Sample_buff[i].H;
    }
  }

  for (auto &x : Sample[0].H)
    out(x + 1);
  return 0;
}