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

#include <bits/stdc++.h>

using namespace std;

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  for (int i = 0; i < vs.size(); i++) {
    os << vs[i] << endl;
  }
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

bool comp(int a, int b) {
  if (a == b)
    return true;
  if (a > b)
    return not comp(b, a);
  if (a == 1)
    return false;
  if (a == 2) {
    if (b == 3)
      return false;
    else
      return true;
  }
  return true;
}

int N;
vector<int> A;
void input() {
  cin >> N;
  A.clear();
  A.resize(N);
  cin >> A;
}

const int INF = 1 << 30;

int step(vector<int> &ans, vector<int> &stack, int prev) {
  int x = stack.back();
  stack.pop_back();
  if (comp(x, prev)) {
    vector<int> a;
    a.push_back(x);
    a.insert(a.end(), ans.begin(), ans.end());
    ans = a;
    return INF;
  } else {
    ans.push_back(x);
    if (prev == INF || x >= 30)
      return INF;
    real p = pow(real(prev), x);
    return (p >= INF ? INF : int(p));
  }
}

void solve() {
  vector<int> nA;
  int zcount = 0;
  int ocount = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] == 0)
      zcount++;
    else if (A[i] == 1)
      ocount++;
    else
      nA.push_back(A[i]);
  }
  sort(nA.begin(), nA.end());
  vector<int> ans;
  if (zcount % 2 == 0) {
    if (not nA.empty()) {
      int p = nA.back();
      nA.pop_back();
      ans.push_back(p);
      while (not nA.empty()) {
        p = step(ans, nA, p);
      }
    }
    for (int i = 0; i < zcount; i++)
      ans.push_back(0);
    for (int i = 0; i < ocount; i++)
      ans.push_back(1);
  } else {
    int s = -1;
    if (ocount >= 1) {
      s = 1;
      ocount--;
    } else if (not nA.empty()) {
      s = nA[0];
      nA.erase(nA.begin(), nA.begin() + 1);
    }
    if (not nA.empty()) {
      int p = nA.back();
      nA.pop_back();
      ans.push_back(p);
      while (not nA.empty()) {
        p = step(ans, nA, p);
      }
    }
    for (int i = 0; i < zcount - 1; i++)
      ans.push_back(0);
    if (s >= 0)
      ans.push_back(s);
    ans.push_back(0);
    for (int i = 0; i < ocount; i++)
      ans.push_back(1);
  }
  cout << ans;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  for (int i = 0; i < vs.size(); i++) {
    os << vs[i] << endl;
  }
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

bool comp(int a, int b) {
  if (a == b)
    return true;
  if (a > b)
    return not comp(b, a);
  if (a == 1)
    return false;
  if (a == 2) {
    if (b == 3)
      return false;
    else
      return true;
  }
  return true;
}

int N;
vector<int> A;
void input() {
  cin >> N;
  A.clear();
  A.resize(N);
  cin >> A;
}

const int INF = 1 << 30;

int step(vector<int> &ans, vector<int> &stack, int prev) {
  int x = stack.back();
  stack.pop_back();
  if (comp(x, prev)) {
    vector<int> a;
    a.push_back(x);
    a.insert(a.end(), ans.begin(), ans.end());
    ans = a;
    return INF;
  } else {
    ans.push_back(x);
    if (prev == INF || x >= 30)
      return INF;
    real p = pow(real(prev), x);
    return (p >= INF ? INF : int(p));
  }
}

void solve() {
  vector<int> nA;
  int zcount = 0;
  int ocount = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] == 0)
      zcount++;
    else if (A[i] == 1)
      ocount++;
    else
      nA.push_back(A[i]);
  }
  sort(nA.begin(), nA.end());
  vector<int> ans;
  if (zcount % 2 == 0) {
    int p = nA.back();
    nA.pop_back();
    ans.push_back(p);
    while (not nA.empty()) {
      p = step(ans, nA, p);
    }
    for (int i = 0; i < zcount; i++)
      ans.push_back(0);
    for (int i = 0; i < ocount; i++)
      ans.push_back(1);
  } else {
    int s = -1;
    if (ocount >= 1) {
      s = 1;
      ocount--;
    } else if (not nA.empty()) {
      s = nA[0];
      nA.erase(nA.begin(), nA.begin() + 1);
    }
    if (not nA.empty()) {
      int p = nA.back();
      nA.pop_back();
      ans.push_back(p);
      while (not nA.empty()) {
        p = step(ans, nA, p);
      }
    }
    for (int i = 0; i < zcount - 1; i++)
      ans.push_back(0);
    if (s >= 0)
      ans.push_back(s);
    ans.push_back(0);
    for (int i = 0; i < ocount; i++)
      ans.push_back(1);
  }
  cout << ans;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}