Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
vector<int> G[2];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int x;
    cin >> x;
    G[x % 2].pb(x);
  }
  if (G[1].size() > 2) {
    cout << "Impossible\n";
    return 0;
  }
  vector<int> a, ans;
  if (G[1].size() == 2 && M == 2) {
    a.pb(G[1][0]);
    a.pb(G[1][1]);
    rep(_, N / 2) ans.pb(2);
  } else {
    // left
    if (G[1].size() >= 1) {
      int len = G[1][0];
      a.pb(len);
      rep(_, (len - 1) / 2) ans.pb(2);
    } else {
      assert(!G[0].empty());
      int len = G[0].back();
      G[0].pop_back();
      a.pb(len);
      ans.pb(len - 1);
    }

    for (int x : G[0]) {
      a.pb(x);
      ans.pb(x);
    }

    // right
    if (G[1].size() >= 2) {
      int len = G[1][1];
      a.pb(len);
      rep(_, len + 1) ans.pb(2);
    } else
      ans.pb(1);
  }
  for (int x : a)
    cout << x << " ";
  cout << "\n";
  cout << ans.size() << "\n";
  for (int x : ans)
    cout << x << " ";
  cout << "\n";

  assert(a.size() == M);
  int sum = 0;
  for (int x : ans)
    sum += x;
  assert(sum == N);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
vector<int> G[2];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int x;
    cin >> x;
    G[x % 2].pb(x);
  }
  if (G[1].size() > 2) {
    cout << "Impossible\n";
    return 0;
  }
  vector<int> a, ans;
  if (G[1].size() == 2 && M == 2) {
    a.pb(G[1][0]);
    a.pb(G[1][1]);
    rep(_, N / 2) ans.pb(2);
  } else {
    // left
    if (G[1].size() >= 1) {
      int len = G[1][0];
      a.pb(len);
      rep(_, (len - 1) / 2) ans.pb(2);
    } else {
      assert(!G[0].empty());
      int len = G[0].back();
      G[0].pop_back();
      a.pb(len);
      ans.pb(len - 1);
    }

    for (int x : G[0]) {
      a.pb(x);
      ans.pb(x);
    }

    // right
    if (G[1].size() >= 2) {
      int len = G[1][1];
      a.pb(len);
      ans.pb(len + 1);
    } else
      ans.pb(1);
  }
  for (int x : a)
    cout << x << " ";
  cout << "\n";
  cout << ans.size() << "\n";
  for (int x : ans)
    cout << x << " ";
  cout << "\n";

  assert(a.size() == M);
  int sum = 0;
  for (int x : ans)
    sum += x;
  assert(sum == N);
  return 0;
}