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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

int f(int a, int b) { return a || !b; }

bool check(vector<int> v) {
  if (v.empty())
    return true;
  for (int i = v.size() - 2; i >= 0; --i) {
    v[i] = f(v[i], v[i + 1]);
  }
  return v[0];
}

double calc(vector<int> v) {
  double res = 1;
  reverse(ALL(v));
  rep(i, v.size()) { res = pow(v[i], res); }
  return res;
}

vector<int> naive(vector<int> v) {
  sort(ALL(v));
  double Max = -1;
  vector<int> res = vector<int>(REV(v));
  do {
    double x = calc(v);
    if (Max < x || abs(Max - x) < 1e-8 && v < res) {
      Max = x, res = v;
    }
  } while (next_permutation(ALL(v)));
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> a, b;
  rep(i, n) {
    int x;
    cin >> x;
    if (x < 2)
      a.push_back(x);
    else
      b.push_back(x);
  }
  sort(ALL(a));
  rep(i, a.size()) {
    if (a[i] && i % 2) {
      swap(a[i - 1], a[i]);
      break;
    }
  }
  sort(ALL(b));

  if (b.size() && !check(a)) {
    int tmp = a.back();
    a.pop_back();
    a.push_back(b[0]);
    a.push_back(tmp);
    b.erase(b.begin());
  }

  int k = b.size();
  if (k > 1 && b[k - 1] == 3 && b[k - 2] == 2)
    swap(b[k - 1], b[k - 2]);

  for (auto x : a)
    b.push_back(x);

  rep(i, n) cout << b[i] << endl;

  // auto bb = naive(b);
  // rep(i, n) cout << bb[i] << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

int f(int a, int b) { return a || !b; }

bool check(vector<int> v) {
  for (int i = v.size() - 2; i >= 0; --i) {
    v[i] = f(v[i], v[i + 1]);
  }
  return v[0];
}

double calc(vector<int> v) {
  double res = 1;
  reverse(ALL(v));
  rep(i, v.size()) { res = pow(v[i], res); }
  return res;
}

vector<int> naive(vector<int> v) {
  sort(ALL(v));
  double Max = -1;
  vector<int> res = vector<int>(REV(v));
  do {
    double x = calc(v);
    if (Max < x || abs(Max - x) < 1e-8 && v < res) {
      Max = x, res = v;
    }
  } while (next_permutation(ALL(v)));
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> a, b;
  rep(i, n) {
    int x;
    cin >> x;
    if (x < 2)
      a.push_back(x);
    else
      b.push_back(x);
  }
  sort(ALL(a));
  rep(i, a.size()) {
    if (a[i] && i % 2) {
      swap(a[i - 1], a[i]);
      break;
    }
  }
  sort(ALL(b));

  int tmp = -1;
  if (b.size() && !check(a)) {
    tmp = b[0];
    auto bb = b;
    b.clear();
    rep(i, bb.size() - 1) b.push_back(bb[i]);
  }

  int k = b.size();
  if (k > 1 && b[k - 1] == 3 && b[k - 2] == 2)
    swap(b[k - 1], b[k - 2]);

  if (tmp > 0)
    b.push_back(tmp);
  for (auto x : a)
    b.push_back(x);

  rep(i, n) cout << b[i] << (i == n - 1 ? '\\n' : ' ');

  // auto bb = naive(b);
  // rep(i, n) cout << bb[i] << (i == n - 1 ? '\\n' : ' ');

  return 0;
}