alexjercan/bugnet huggingface dataset:
Problem p01411 (C++) — buggy submission, verdict: Runtime Error

#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
typedef long double ld;
ld dp[505][105][105] = {};

int main() {
  int h, n, p, m, K;
  cin >> h >> n >> p >> m >> K;
  vector<int> v(h + 1, -1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a] = b;
  }
  dp[h + 1][0][p] = 1.0;
  for (int i = h; i >= 1; i--) {
    for (int j = 0; j <= K; j++) {
      for (int k = 1; k <= n; k++) {
        if (v[i] == -1) {
          dp[i][j][k] = dp[i + 1][j][k];
          if (j >= 1) {
            if (k == 1 || k == n) {
              dp[i][j][k] += dp[i + 1][j - 1][k] * (ld)(n - 2);
            } else {
              dp[i][j][k] += dp[i + 1][j - 1][k] * (ld)(n - 3);
            }
            if (k != 1) {
              dp[i][j][k] += dp[i + 1][j - 1][k - 1];
            }
            if (k != n) {
              dp[i][j][k] += dp[i + 1][j - 1][k + 1];
            }
          }
        } else {
          if (k == v[i]) {
            dp[i][j][k] = dp[i + 1][j][k + 1];
          } else if (k == v[i] + 1) {
            dp[i][j][k] = dp[i + 1][j][k - 1];
          } else {
            dp[i][j][k] = dp[i + 1][j][k];
          }
        }
      }
    }
  }
  ld sm = 0.0;
  for (int i = 1; i <= n; i++) {
    sm += dp[1][K][i];
  }
  ld mx = 0.0;
  for (int i = 1; i <= n; i++) {
    mx = max(mx, dp[1][K][i] / sm);
  }
  cout << fixed << setprecision(15) << mx << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
typedef long double ld;
int main() {
  int h, n, p, m, K;
  cin >> h >> n >> p >> m >> K;
  vector<int> v(h + 1, -1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a] = b;
  }
  ld dp[505][105][105] = {};
  dp[h + 1][0][p] = 1.0;
  for (int i = h; i >= 1; i--) {
    for (int j = 0; j <= K; j++) {
      for (int k = 1; k <= n; k++) {
        if (v[i] == -1) {
          dp[i][j][k] = dp[i + 1][j][k];
          if (j >= 1) {
            if (k == 1 || k == n) {
              dp[i][j][k] += dp[i + 1][j - 1][k] * (ld)(n - 2);
            } else {
              dp[i][j][k] += dp[i + 1][j - 1][k] * (ld)(n - 3);
            }
            if (k != 1) {
              dp[i][j][k] += dp[i + 1][j - 1][k - 1];
            }
            if (k != n) {
              dp[i][j][k] += dp[i + 1][j - 1][k + 1];
            }
          }
        } else {
          if (k == v[i]) {
            dp[i][j][k] = dp[i + 1][j][k + 1];
          } else if (k == v[i] + 1) {
            dp[i][j][k] = dp[i + 1][j][k - 1];
          } else {
            dp[i][j][k] = dp[i + 1][j][k];
          }
        }
      }
    }
  }
  ld sm = 0.0;
  for (int i = 1; i <= n; i++) {
    sm += dp[1][K][i];
  }
  ld mx = 0.0;
  for (int i = 1; i <= n; i++) {
    mx = max(mx, dp[1][K][i] / sm);
  }
  cout << fixed << setprecision(15) << mx << endl;
  return 0;
}
Problem p01411 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MAX_H 505
#define MAX_N 105
int H, N, P, M, K;

int t[MAX_H];
int u[MAX_H];
int tmp[MAX_N];

double dp[2][MAX_H][MAX_N];

int total = 0;

int main() {
  cin >> H >> N >> P >> M >> K;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    t[a] = b;
  }
  int last;
  dp[H & 1][0][P] = 1;
  for (int i = H; i >= 1; i--) {
    int ai = i & 1, bi = 1 - ai;
    last = bi;
    for (int j = 0; j <= K; j++)
      for (int k = 1; k <= N; k++)
        dp[bi][j][k] = 0;

    if (t[i] > 0) {
      for (int j = 0; j <= K; j++) {
        for (int k = 1; k <= N; k++) {
          if (k == t[i]) {
            dp[bi][j][k + 1] += dp[ai][j][k];
          } else if (k == t[i] + 1) {
            dp[bi][j][k - 1] += dp[ai][j][k];
          } else {
            dp[bi][j][k] += dp[ai][j][k];
          }
        }
      }
      continue;
    }

    for (int j = 0; j <= K; j++) {
      for (int k = 1; k <= N; k++) {

        dp[bi][j][k] += dp[ai][j][k];
        double C = N - 1;
        if (k < N) {
          dp[bi][j + 1][k + 1] += dp[ai][j][k];
          C -= 1;
        }
        if (1 < k) {
          dp[bi][j + 1][k - 1] += dp[ai][j][k];
          C -= 1;
        }

        dp[bi][j + 1][k] += dp[ai][j][k] * C;
      }
    }
  }

  double maxm = 0, total = 0;
  for (int i = 1; i <= N; i++) {
    total += dp[last][K][i];
    maxm = max(maxm, dp[last][K][i]);
  }
  //  printf("%.10f\\n",maxm);
  //  printf("%.10f\\n",total);
  printf("%.10f\\n", maxm / total);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_H 505
#define MAX_N 105
int H, N, P, M, K;

int t[MAX_H];
int u[MAX_H];
int tmp[MAX_N];

double dp[MAX_H][MAX_H][MAX_N];

int total = 0;

int main() {
  cin >> H >> N >> P >> M >> K;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    t[a] = b;
  }

  dp[H][0][P] = 1;
  for (int i = H; i >= 1; i--) {

    if (t[i] > 0) {
      for (int j = 0; j <= K; j++) {
        for (int k = 1; k <= N; k++) {
          if (k == t[i]) {
            dp[i - 1][j][k + 1] += dp[i][j][k];
          } else if (k == t[i] + 1) {
            dp[i - 1][j][k - 1] += dp[i][j][k];
          } else {
            dp[i - 1][j][k] += dp[i][j][k];
          }
        }
      }
      continue;
    }

    for (int j = 0; j <= K; j++) {
      for (int k = 1; k <= N; k++) {

        if (dp[i][j][k] > 0) {
          // cout<<i<<' '<<j<<' '<<k<<' '<<dp[i][j][k]<<endl;
        }

        dp[i - 1][j][k] += dp[i][j][k];
        double C = N - 1;
        if (k < N) {
          dp[i - 1][j + 1][k + 1] += dp[i][j][k];
          C -= 1;
        }
        if (1 < k) {
          dp[i - 1][j + 1][k - 1] += dp[i][j][k];
          C -= 1;
        }

        dp[i - 1][j + 1][k] += dp[i][j][k] * C;
      }
    }
  }

  double maxm = 0, total = 0;
  for (int i = 1; i <= N; i++) {
    total += dp[0][K][i];
    maxm = max(maxm, dp[0][K][i]);
  }
  //  printf("%.10f\\n",maxm);
  //  printf("%.10f\\n",total);
  printf("%.10f\\n", maxm / total);
  return 0;
}
Problem p01411 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int H, N, P, M, K;

vector<int> stick(505, -1);

vector<vector<ld>> dp(505, vector<ld>(101, 0));

int main() {
  cin >> H >> N >> P >> M >> K;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    stick[a] = b;
  }

  dp[K][P] = 1;

  for (int i = H; i >= 1; i--) {
    if (stick[i] != -1) {
      vector<vector<ld>> next(505, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) {
        int st = stick[i];
        if (st == j)
          next[k][j + 1] += dp[k][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k][j];
        else
          next[k][j] += dp[k][j];
      }
      swap(dp, next);
    } else {
      vector<vector<ld>> next(505, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) { next[k][j] += dp[k][j]; }
      rep(j, 1, N) rep(k, 0, K) rep(st, 1, N - 1) {
        if (st == j)
          next[k][j + 1] += dp[k + 1][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k + 1][j];
        else
          next[k][j] += dp[k + 1][j];
      }
      swap(dp, next);
    }
  }

  ld result = 0;
  ld sum = 0;
  for (int i = 1; i <= N; i++) {
    sum += dp[0][i];
    result = max(result, dp[0][i]);
  }

  cout << fixed << setprecision(10) << result / sum << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int H, N, P, M, K;

vector<int> stick(505, -1);

vector<vector<ld>> dp(505, vector<ld>(101, 0));

int main() {
  cin >> H >> N >> P >> M >> K;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    stick[a] = b;
  }

  dp[K][P] = 1;

  for (int i = H; i >= 1; i--) {
    if (stick[i] != -1) {
      vector<vector<ld>> next(505, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) {
        int st = stick[i];
        if (st == j)
          next[k][j + 1] += dp[k][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k][j];
        else
          next[k][j] += dp[k][j];
      }
      swap(dp, next);
    } else {
      vector<vector<ld>> next(101, vector<ld>(101, 0));
      rep(j, 1, N) rep(k, 0, K) { next[k][j] += dp[k][j]; }
      rep(j, 1, N) rep(k, 0, K) rep(st, 1, N - 1) {
        if (st == j)
          next[k][j + 1] += dp[k + 1][j];
        else if (st + 1 == j)
          next[k][j - 1] += dp[k + 1][j];
        else
          next[k][j] += dp[k + 1][j];
      }
      swap(dp, next);
    }
  }

  ld result = 0;
  ld sum = 0;
  for (int i = 1; i <= N; i++) {
    sum += dp[0][i];
    result = max(result, dp[0][i]);
  }

  cout << fixed << setprecision(10) << result / sum << endl;

  return 0;
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, zero = 0, one = 0;
  cin >> n;

  vector<int> a;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (t == 0)
      zero++;
    else if (t == 1)
      one++;
    else
      a.push_back(t);
  }

  sort(a.begin(), a.end());
  int m = a.size();

  if ((zero & 1) == 0) {
    if (m > 1 && a[m - 2] == 2 && a[m - 1] == 3)
      swap(a[m - 1], a[m - 2]);
    for (int i = 0; i < m; i++)
      cout << a[i] << endl;
    while (zero--)
      cout << 0 << endl;
    while (one--)
      cout << 1 << endl;
  } else if (one) {
    if (m > 1 && a[m - 2] == 2 && a[m - 1] == 3)
      swap(a[m - 1], a[m - 2]);
    for (int i = 0; i < m; i++)
      cout << a[i] << endl;
    for (int i = 1; i < zero; i++)
      cout << 0 << endl;
    cout << 1 << endl;
    one--;
    cout << 0 << endl;
    while (one--)
      cout << 1 << endl;
  } else {
    if (m > 2 && a[m - 2] == 2 && a[m - 1] == 3)
      swap(a[m - 1], a[m - 2]);
    for (int i = 1; i < m; i++)
      cout << a[i] << endl;
    for (int i = 1; i < zero; i++)
      cout << 0 << endl;
    if (m)
      cout << a[0] << endl;
    cout << 0 << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, zero = 0, one = 0;
  cin >> n;

  vector<int> a;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (t == 0)
      zero++;
    else if (t == 1)
      one++;
    else
      a.push_back(t);
  }

  sort(a.begin(), a.end());
  int m = a.size();
  if (m > 1 && a[m - 2] == 2 && a[m - 1] == 3)
    swap(a[m - 1], a[m - 2]);

  if ((zero & 1) == 0) {
    for (int i = 0; i < m; i++)
      cout << a[i] << endl;
    while (zero--)
      cout << 0 << endl;
    while (one--)
      cout << 1 << endl;
  } else if (one) {
    for (int i = 0; i < m; i++)
      cout << a[i] << endl;
    for (int i = 1; i < zero; i++)
      cout << 0 << endl;
    cout << 1 << endl;
    one--;
    cout << 0 << endl;
    while (one--)
      cout << 1 << endl;
  } else {
    for (int i = 1; i < m; i++)
      cout << a[i] << endl;
    cout << a[0] << endl;
    while (zero--)
      cout << 0 << endl;
  }
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  int f[2] = {0};
  vector<int> v;
  for (int i = 0; i < N; i++) {
    if (a[i] >= 2) {
      v.push_back(a[i]);
    } else {
      f[a[i]]++;
    }
  }
  // 0 が奇数個のみのケース
  if (f[0] == N && v.size() == 0) {
    for (int i = 0; i < f[0]; i++) {
      cout << 0 << endl;
    }
    return 0;
  }

  sort(v.begin(), v.end());
  if (v.size() >= 2 && v[v.size() - 2] == 2 && v[v.size() - 1] == 3) {
    swap(v[v.size() - 2], v[v.size() - 1]);
  }

  if (f[0] % 2 == 1 && f[1] == 0) {
    int m = *min_element(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      if (v[i] == m) {
        v[i] = -1;
        break;
      }
    }
    int x = m;
    for (; f[0]; f[0]--) {
      if (f[0] == 1) {
        v.push_back(x);
      }
      v.push_back(0);
    }
  } else {
    int one, zero;
    one = f[1];
    zero = f[0];
    for (; f[0]; f[0]--) {
      v.push_back(0);
    }
    for (; f[1]; f[1]--) {
      v.push_back(1);
    }
    if (one && zero && zero % 2 == 1) {
      for (int i = 1; i < v.size(); i++) {
        if (v[i - 1] == 0 && v[i] == 1) {
          swap(v[i - 1], v[i]);
          break;
        }
      }
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == -1)
      continue;
    cout << v[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  if (N == 1 && a[0] == 0) {
    cout << 0 << endl;
    return 0;
  }

  int f[2] = {0};
  vector<int> v;
  for (int i = 0; i < N; i++) {
    if (a[i] >= 2) {
      v.push_back(a[i]);
    } else {
      f[a[i]]++;
    }
  }
  sort(v.begin(), v.end());
  if (v.size() >= 2 && v[v.size() - 2] == 2 && v[v.size() - 1] == 3) {
    swap(v[v.size() - 2], v[v.size() - 1]);
  }

  if (f[0] % 2 == 1 && f[1] == 0) {
    int m = *min_element(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      if (v[i] == m) {
        v[i] = -1;
        break;
      }
    }
    int x = m;
    for (; f[0]; f[0]--) {
      if (f[0] == 1) {
        v.push_back(x);
      }
      v.push_back(0);
    }
  } else {
    int one, zero;
    one = f[1];
    zero = f[0];
    for (; f[0]; f[0]--) {
      v.push_back(0);
    }
    for (; f[1]; f[1]--) {
      v.push_back(1);
    }
    if (one && zero && zero % 2 == 1) {
      for (int i = 1; i < v.size(); i++) {
        if (v[i - 1] == 0 && v[i] == 1) {
          swap(v[i - 1], v[i]);
          break;
        }
      }
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == -1)
      continue;
    cout << v[i] << endl;
  }
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;

  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  sort(A.begin(), A.end());

  vector<int> last;
  while (A.size() && A[0] == 0)
    last.push_back(0), A.erase(A.begin());
  if (A.size() && last.size() % 2)
    last.insert(last.end() - 1, A[0]), A.erase(A.begin());
  while (A.size() && A[0] == 1)
    last.push_back(1), A.erase(A.begin());
  if (A.size() >= 2 && A[A.size() - 1] == 3 && A[A.size() - 2] == 2)
    swap(A[A.size() - 2], A[A.size() - 1]);

  for (int i = 0; i < A.size(); i++)
    cout << A[i] << endl;
  for (int i = 0; i < last.size(); i++)
    cout << last[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;

  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  sort(A.begin(), A.end());

  vector<int> last;
  while (A.size() && A[0] == 0)
    last.push_back(0), A.erase(A.begin());
  if (A.size() && last.size() % 2)
    last.insert(last.end() - 2, A[0]), A.erase(A.begin());
  while (A.size() && A[0] == 1)
    last.push_back(1), A.erase(A.begin());
  if (A.size() >= 2 && A[A.size() - 1] == 3 && A[A.size() - 2] == 2)
    swap(A[A.size() - 2], A[A.size() - 1]);

  for (int i = 0; i < A.size(); i++)
    cout << A[i] << endl;
  for (int i = 0; i < last.size(); i++)
    cout << last[i] << endl;

  return 0;
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, a, sum0 = 0, sum1 = 0, sum3 = 0, sum2 = 0;
  vector<int> V, V0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    V.push_back(a);
    if (a == 2)
      sum2++;
    if (a == 3)
      sum3++;
  }
  sort(V.begin(), V.end());
  if (V[0] == 0) {
    for (int i = 0; i < N; i++) {
      sum0++;
      V.erase(V.begin());
      if (V[0] != 0 || V.empty())
        break;
    }
  }
  for (int i = 0; i < sum0; i++)
    V0.push_back(0);
  for (int i = 0; i < N; i++) {
    if (V[0] != 1 || V.empty())
      break;
    sum1++;
    V.erase(V.begin());
  }
  for (int i = 0; i < sum1; i++)
    V0.push_back(1);
  if (V0.size() >= 2 || sum0 != 0) {
    if (sum0 % 2 == 1) {
      if (sum1 == 0 && !V.empty()) {
        V0.insert(V0.end() - 1, V[0]);
        if (V[0] == 2)
          sum2--;
        V.erase(V.begin());
      } else if (sum1 != 0) {
        for (int i = 0; i < V0.size(); i++) {
          if (V0[i] != V0[i + 1]) {
            swap(V0[i], V0[i + 1]);
            break;
          }
        }
      }
    }
  }
  if (sum3 == 1 && sum2 != 0)
    swap(V[sum2 - 1], V[sum2]);

  for (int i = 0; i < V.size(); i++)
    cout << V[i] << endl;
  for (int i = 0; i < V0.size(); i++)
    cout << V0[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, a, sum0 = 0, sum1 = 0, sum3 = 0, sum2 = 0;
  vector<int> V, V0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    V.push_back(a);
    if (a == 2)
      sum2++;
    if (a == 3)
      sum3++;
  }
  sort(V.begin(), V.end());
  if (V[0] == 0) {
    for (int i = 0; i < N; i++) {
      sum0++;
      V.erase(V.begin());
      if (V[0] != 0 || V.empty())
        break;
    }
  }
  for (int i = 0; i < sum0; i++)
    V0.push_back(0);
  for (int i = 0; i < N; i++) {
    if (V[0] != 1 || V.empty())
      break;
    sum1++;
    V.erase(V.begin());
  }

  if (V0.size() % 2 == 1) {
    if (sum1 == 0) {
      V0.insert(V0.end() - 1, V[0]);
      if (V[0] == 2)
        sum2--;
      V.erase(V.begin());
    } else {
      V0.insert(V0.end() - 1, 1);
      for (int i = 1; i < sum1; i++)
        V0.push_back(1);
    }
  } else
    for (int i = 0; i < sum1; i++)
      V0.push_back(1);

  if (sum3 == 1 && sum2 != 0)
    swap(V[sum2 - 1], V[sum2]);

  for (int i = 0; i < V.size(); i++)
    cout << V[i] << endl;
  for (int i = 0; i < V0.size(); i++)
    cout << V0[i] << endl;
  return 0;
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  deque<int> A, Z, O;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a == 0)
      Z.push_back(0);
    else if (a == 1)
      O.push_back(1);
    else
      A.push_back(a);
  }
  sort(A.begin(), A.end());
  if (O.empty() && Z.size() & 1) {
    if (A.empty()) {
      for (auto &s : Z)
        cout << 0 << endl;
      return (0);
    }
    O.push_back(A.front());
    A.pop_front();
  }
  if (A.size() >= 2 && *-- --A.end() == 2 && *--A.end() == 3) {
    swap(A[A.size() - 2], A[A.size() - 1]);
  }
  while (!A.empty())
    cout << A.front() << endl, A.pop_front();
  if (Z.size() & 1) {
    Z.pop_front();
    while (!Z.empty())
      cout << 0 << endl, Z.pop_back();
    cout << O.back() << endl;
    O.pop_back();
    cout << 0 << endl;
    while (!O.empty())
      cout << 1 << endl, O.pop_back();
  } else {
    while (!Z.empty())
      cout << 0 << endl, Z.pop_back();
    while (!O.empty())
      cout << 1 << endl, O.pop_back();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  deque<int> A, Z, O;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a == 0)
      Z.push_back(0);
    else if (a == 1)
      O.push_back(1);
    else
      A.push_back(a);
  }
  sort(A.begin(), A.end());
  if (O.empty() && Z.size() & 1) {
    if (!A.empty()) {
      O.push_back(A.front());
      A.pop_front();
    }
  }
  if (A.size() >= 2 && *-- --A.end() == 2 && *--A.end() == 3) {
    swap(A[A.size() - 2], A[A.size() - 1]);
  }
  while (!A.empty())
    cout << A.front() << endl, A.pop_front();
  if (Z.size() & 1) {
    Z.pop_front();
    while (!Z.empty())
      cout << 0 << endl, Z.pop_back();
    cout << O.back() << endl;
    O.pop_back();
    cout << 0 << endl;
    while (!O.empty())
      cout << 1 << endl, O.pop_back();
  } else {
    while (!Z.empty())
      cout << 0 << endl, Z.pop_back();
    while (!O.empty())
      cout << 1 << endl, O.pop_back();
  }
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

string dp[101][101][2]; // zero, one, 0or1

int main() {
  for (int y = 1; y <= 100; y++)
    dp[0][y][1] = string(y, '1');
  for (int x = 1; x <= 100; x++) {
    if (x % 2) {
      dp[x][0][0] = string(x, '0');
    } else {
      dp[x][0][1] = string(x, '0');
    }
  }
  for (int x = 1; x <= 100; x++) {
    for (int y = 1; y <= 100; y++) {
      if (dp[x - 1][y][1] != "") {
        dp[x][y][0] = "0" + dp[x - 1][y][1];
      } else {
        dp[x][y][0] = "";
      }
      dp[x][y][1] = "1" + dp[x][y - 1][0];
      if (dp[x - 1][y][0] != "")
        dp[x][y][1] = min(dp[x][y][1], "0" + dp[x - 1][y][0]);
      if (dp[x][y - 1][1] != "")
        dp[x][y][1] = min(dp[x][y][1], "1" + dp[x][y - 1][1]);
    }
  }

  int N;
  cin >> N;
  vector<int> V;
  int zero = 0, one = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a == 0) {
      zero++;
    } else if (a == 1) {
      one++;
    } else {
      V.push_back(a);
    }
  }
  sort(V.begin(), V.end());
  string s;
  int victim = -1;
  if (zero % 2 == 1 && !one) {
    if (V.size() > 0) {
      victim = V.front();
      V.erase(V.begin());
      one++;
    }
  }
  int n = V.size();
  if (n >= 2 && V[n - 2] == 2 && V[n - 1] == 3)
    swap(V[n - 2], V[n - 1]);
  for (auto v : V) {
    cout << v << endl;
  }
  if (zero % 2 && !one) {
    s += dp[zero][one][0];
  } else {
    s += dp[zero][one][1];
  }
  for (auto c : s) {
    if (victim != -1 && c == '1')
      c = '0' + victim;
    cout << c << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

string dp[101][101][2]; // zero, one, 0or1

int main() {
  for (int y = 1; y <= 100; y++)
    dp[0][y][1] = string(y, '1');
  for (int x = 1; x <= 100; x++) {
    if (x % 2) {
      dp[x][0][0] = string(x, '0');
    } else {
      dp[x][0][1] = string(x, '0');
    }
  }
  for (int x = 1; x <= 100; x++) {
    for (int y = 1; y <= 100; y++) {
      if (dp[x - 1][y][1] != "") {
        dp[x][y][0] = "0" + dp[x - 1][y][1];
      } else {
        dp[x][y][0] = "";
      }
      dp[x][y][1] = "1" + dp[x][y - 1][0];
      if (dp[x - 1][y][0] != "")
        dp[x][y][1] = min(dp[x][y][1], "0" + dp[x - 1][y][0]);
      if (dp[x][y - 1][1] != "")
        dp[x][y][1] = min(dp[x][y][1], "1" + dp[x][y - 1][1]);
    }
  }

  int N;
  cin >> N;
  vector<int> V;
  int zero = 0, one = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a == 0) {
      zero++;
    } else if (a == 1) {
      one++;
    } else {
      V.push_back(a);
    }
  }
  sort(V.begin(), V.end());
  string s;
  if (zero % 2 == 1 && !one) {
    if (V.size() >= 2)
      swap(V.front(), V.back());
    s = '0' + V.back();
    V.pop_back();
  }
  int n = V.size();
  if (n >= 2 && V[n - 2] == 2 && V[n - 1] == 3)
    swap(V[n - 2], V[n - 1]);
  for (auto v : V) {
    cout << v << endl;
  }
  if (zero % 2 && !one) {
    s += dp[zero][one][0];
  } else {
    s += dp[zero][one][1];
  }
  for (auto c : s)
    cout << c << endl;

  return 0;
}