Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 105;

int a[MAXN], n, m;

int ax[MAXN];
int bx[100005], btop = 0;

int main() {
  scanf("%d%d", &n, &m);
  int odd = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    odd += (a[i] & 1);
  }
  if (n & 1) {
    if (odd > 1) {
      cout << "Impossible" << endl;
      return 0;
    } else {
      int top = 0;
      for (int i = 1; i <= m; i++)
        if (a[i] & 1)
          ax[++top] = a[i];
      for (int i = 1; i <= m; i++)
        if (!(a[i] & 1))
          ax[++top] = a[i];
      int tmp = ax[1];
      while (tmp != 1)
        bx[++btop] = 2, tmp -= 2;
      for (int i = 2; i <= m; i++)
        bx[++btop] = ax[i];
      bx[++btop] = 1;
    }
  } else {
    if (odd > 2) {
      cout << "Impossible" << endl;
      return 0;
    } else {
      assert(odd == 0 || odd == 2);
      if (odd == 0) {
        memcpy(ax, a, sizeof a);
        if (a[1] > 2)
          bx[++btop] = a[1] - 2;
        bx[++btop] = 1;
        for (int i = 2; i <= m; i++)
          bx[++btop] = a[i];
        bx[++btop] = 1;
      } else if (odd == 2) {
        int flag = 0, top = 1;
        for (int i = 1; i <= m; i++) {
          if (a[i] & 1) {
            if (!flag)
              ax[1] = a[i];
            else
              ax[m] = a[i];
          } else
            ax[++top] = a[i];
        }
        int tmp = ax[1];
        while (tmp != 1)
          bx[++btop] = 2, tmp -= 2;
        for (int i = 2; i < m; i++)
          bx[++btop] = ax[i];
        tmp = ax[m] + 1;
        while (tmp)
          bx[++btop] = 2, tmp -= 2;
      }
    }
  }
  for (int i = 1; i <= m; i++)
    cout << ax[i] << " ";
  cout << endl;
  cout << btop << endl;
  for (int i = 1; i <= btop; i++)
    cout << bx[i] << " ";
  cout << endl;
  return 0;
}


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

const int MAXN = 105;

int a[MAXN], n, m;

int ax[MAXN];
int bx[100005], btop = 0;

int main() {
  scanf("%d%d", &n, &m);
  int odd = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    odd += (a[i] & 1);
  }
  if (n & 1) {
    if (odd > 1) {
      cout << "Impossible" << endl;
      return 0;
    } else {
      int top = 0;
      for (int i = 1; i <= m; i++)
        if (a[i] & 1)
          ax[++top] = a[i];
      for (int i = 1; i <= m; i++)
        if (!(a[i] & 1))
          ax[++top] = a[i];
      int tmp = ax[1];
      while (tmp != 1)
        bx[++btop] = 2, tmp -= 2;
      for (int i = 2; i <= m; i++)
        bx[++btop] = ax[i];
      bx[++btop] = 1;
    }
  } else {
    if (odd > 2) {
      cout << "Impossible" << endl;
      return 0;
    } else {
      assert(odd == 0 || odd == 2);
      if (odd == 0) {
        memcpy(ax, a, sizeof a);
        if (a[1] > 2)
          bx[++btop] = a[1] - 2;
        bx[++btop] = 1;
        for (int i = 2; i <= m; i++)
          bx[++btop] = a[i];
        bx[++btop] = 1;
      } else if (odd == 2) {
        int flag = 0, top = 1;
        for (int i = 1; i <= m; i++) {
          if (a[i] & 1) {
            if (!flag)
              ax[1] = a[i], flag = 1;
            else
              ax[m] = a[i];
          } else
            ax[++top] = a[i];
        }
        int tmp = ax[1];
        while (tmp != 1)
          bx[++btop] = 2, tmp -= 2;
        for (int i = 2; i < m; i++)
          bx[++btop] = ax[i];
        tmp = ax[m] + 1;
        while (tmp)
          bx[++btop] = 2, tmp -= 2;
      }
    }
  }
  for (int i = 1; i <= m; i++)
    cout << ax[i] << " ";
  cout << endl;
  cout << btop << endl;
  for (int i = 1; i <= btop; i++)
    cout << bx[i] << " ";
  cout << endl;
  return 0;
}