Problem p03394 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int LIM = 3e4 + 1;

vector<int> Solve(int n) {
  if (n == 3) {
    return {2, 5, 63};
  }
  vector<int> M3, M2;
  for (int i = 3; i < LIM; i += 3) {
    if (i % 2 != 0) {
      M3.push_back(i);
    }
  }
  for (int i = 2; i < LIM; i += 2) {
    M2.push_back(i);
  }
  /* cerr << M2.size() + M3.size() << endl;
  cerr << M2.size() << ", " << M3.size() << endl;
  int sum = accumulate(M2.begin(), M2.end(), 0);
  cerr << "D: " << sum << endl; */
  vector<int> ans;
  int of3 = min(n - ((n % 2) == 0 ? 2 : 3), (int)M3.size());
  ans.insert(ans.begin(), M3.begin(), M3.begin() + of3);
  n -= of3;
  int iter = 0;
  while (n > 1) {
    ans.push_back(M2[iter++]), n--;
  }
  int sum = accumulate(M2.begin(), M2.begin() + iter, 0);
  // cerr << sum % 3 << endl;
  while (iter < M2.size()) {
    if ((sum + M2[iter]) % 3 == 0) {
      ans.push_back(M2[iter]), n--;
      break;
    } else {
      iter++;
    }
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  auto ans = Solve(n);
  sort(ans.begin(), ans.end());
  {
    int g = ans[0];
    for (int i = 1; i < ans.size(); i++) {
      g = __gcd(g, ans[i]);
    }
    assert(g == 1);
    assert(ans.size() == n);
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
    assert(ans.size() == n);
    int sum = accumulate(ans.begin(), ans.end(), 0);
    for (int x : ans) {
      assert(x >= 1 and x < LIM);
      assert((x % 2) == (sum % 2) || (x % 3) == (sum % 3));
    }
  }
  for (int x : ans) {
    cout << x << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int LIM = 3e4 + 1;

vector<int> Solve(int n) {
  if (n == 3) {
    return {2, 5, 63};
  }
  vector<int> M3, M2;
  for (int i = 3; i < LIM; i += 3) {
    if (i % 2 != 0) {
      M3.push_back(i);
    }
  }
  for (int i = 2; i < LIM; i += 2) {
    M2.push_back(i);
  }
  /* cerr << M2.size() + M3.size() << endl;
  cerr << M2.size() << ", " << M3.size() << endl;
  int sum = accumulate(M2.begin(), M2.end(), 0);
  cerr << "D: " << sum << endl; */
  vector<int> ans;
  int of3 = min(n - ((n % 2) == 0 ? 2 : 3), (int)M3.size());
  ans.insert(ans.begin(), M3.begin(), M3.begin() + of3);
  n -= of3;
  int iter = 0;
  while (n > 1) {
    ans.push_back(M2[iter++]), n--;
  }
  int sum = accumulate(M2.begin(), M2.begin() + iter, 0);
  // cerr << sum % 3 << endl;
  while (iter < M2.size()) {
    if ((sum + M2[iter]) % 3 == 0) {
      ans.push_back(M2[iter]), n--;
      break;
    } else {
      iter++;
    }
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  auto ans = Solve(n);
  sort(ans.begin(), ans.end());
  if (ans.size() > 3) {
    int g = ans[0];
    for (int i = 1; i < ans.size(); i++) {
      g = __gcd(g, ans[i]);
    }
    assert(g == 1);
    assert(ans.size() == n);
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
    assert(ans.size() == n);
    int sum = accumulate(ans.begin(), ans.end(), 0);
    for (int x : ans) {
      assert(x >= 1 and x < LIM);
      assert((x % 2) == (sum % 2) || (x % 3) == (sum % 3));
    }
  }
  for (int x : ans) {
    cout << x << " ";
  }
  cout << endl;
  return 0;
}