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

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

typedef vector<int>::const_iterator iitr;

void solve(int n, iitr beg, iitr end) {
  vector<int> tmp;
  for (iitr itr = beg; itr != end; ++itr) {
    tmp.push_back(*itr);
  }
  sort(tmp.begin(), tmp.end());
  int cnt0 = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0, ff = 0;
  for (iitr itr = tmp.begin(); itr != tmp.end(); ++itr) {
    if (*itr == 0) {
      cnt0++;
    } else if (*itr == 1) {
      cnt1++;
    } else if (*itr == 2) {
      cnt2++;
    } else if (*itr == 3) {
      cnt3++;
    }
  }
  bool flag = false, flag2 = false;
  if (cnt0 % 2 == 1 && cnt1 == 0)
    flag = true;
  // 0,1以外の処理
  if (cnt0 + cnt1 + cnt2 + cnt3 == n && cnt2 > 0 && cnt3 == 1) {
    if (flag) {
      ff = 2;
      flag2 = true;
      rep(i, cnt2 - 2) cout << 2 << endl;
      cout << 3 << endl;
      if (cnt2 >= 2)
        cout << 2 << endl;
    } else {
      rep(i, cnt2 - 1) cout << 2 << endl;
      cout << 3 << endl << 2 << endl;
    }
  } else {
    if (flag) {
      if (cnt0 + cnt1 < n) {
        flag2 = true;
        ff = tmp[cnt0 + cnt1];
        solve(n, tmp.begin() + cnt0 + cnt1 + 1, tmp.end());
      }
    } else { // if (cnt2==0 && cnt3==0) {
      // cout << "flag43" << endl;
      for (iitr itr = beg + cnt0 + cnt1; itr != end; ++itr) {
        cout << *itr << endl;
      }
    }
    // else {
    // cout << "fda" << endl;
    //   solve(n,tmp.begin()+cnt0+cnt1,tmp.end());
    // }
  }
  // 0,1の処理
  if (!flag2 && (cnt0 % 2 == 0 || cnt1 == 0)) {
    rep(i, cnt0) cout << 0 << endl;
    rep(i, cnt1) cout << 1 << endl;
  } else if (!flag2) {
    rep(i, cnt0 - 1) cout << 0 << endl;
    cout << 1 << endl;
    cout << 0 << endl;
    rep(i, cnt1 - 1) cout << 1 << endl;
  } else {
    rep(i, cnt0 - 1) cout << 0 << endl;
    cout << ff << endl;
    cout << 0 << endl;
  }
}

int main() {
  int n, swflag = 0;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  if (n == 1) {
    cout << a[0] << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  solve(n, a.begin(), a.end());

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

typedef vector<int>::const_iterator iitr;

void solve(int n, iitr beg, iitr end) {
  vector<int> tmp;
  for (iitr itr = beg; itr != end; ++itr) {
    tmp.push_back(*itr);
  }
  sort(tmp.begin(), tmp.end());
  int cnt0 = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0;
  for (iitr itr = tmp.begin(); itr != tmp.end(); ++itr) {
    if (*itr == 0) {
      cnt0++;
    } else if (*itr == 1) {
      cnt1++;
    } else if (*itr == 2) {
      cnt2++;
    } else if (*itr == 3) {
      cnt3++;
    }
  }
  bool flag = false;
  if (cnt0 % 2 == 1 && cnt1 == 0)
    flag = true;
  // 0,1以外の処理
  if (cnt0 + cnt1 + cnt2 + cnt3 == n && cnt2 > 0 && cnt3 == 1) {
    if (flag) {
      rep(i, cnt2 - 2) cout << 2 << endl;
      cout << 3 << endl << 2 << endl;
      if (cnt2 >= 2)
        cout << 2 << endl;
    } else {
      rep(i, cnt2 - 1) cout << 2 << endl;
      cout << 3 << endl << 2 << endl;
    }
  } else {
    if (flag) {
      // cout << "flag" << endl;
      solve(n, tmp.begin() + cnt0 + cnt1 + 1, tmp.end());
      cout << tmp[cnt0 + cnt1] << endl;
    } else { // if (cnt2==0 && cnt3==0) {
      // cout << "flag43" << endl;
      for (iitr itr = beg + cnt0 + cnt1; itr != end; ++itr) {
        cout << *itr << endl;
      }
    }
    // else {
    // cout << "fda" << endl;
    //   solve(n,tmp.begin()+cnt0+cnt1,tmp.end());
    // }
  }
  // 0,1の処理
  if (cnt0 % 2 == 0 || cnt1 == 0) {
    rep(i, cnt0) cout << 0 << endl;
    rep(i, cnt1) cout << 1 << endl;
  } else {
    rep(i, cnt0 - 1) cout << 0 << endl;
    cout << 1 << endl;
    cout << 0 << endl;
    rep(i, cnt1 - 1) cout << 1 << endl;
  }
}

int main() {
  int n, swflag = 0;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  if (n == 1) {
    cout << a[0] << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  solve(n, a.begin(), a.end());

  return 0;
}