Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <typeinfo>
#include <vector>

#define DIV 1000000007

using namespace std;

long long N, M;
long long A[101];
vector<long long> odd;
vector<long long> a;
vector<long long> b;
int main() {
  cin >> N >> M;
  long long tmp = 0;
  for (int i = 0; i < M; i++) {
    cin >> A[i];
    if (A[i] % 2 == 1) {
      tmp++;
      odd.push_back(A[i]);
    }
  }
  if (M == 1) {
    if (A[0] == 1) {
      return 1;
    }
    cout << A[0] << endl;
    cout << 2 << endl;
    cout << A[0] - 1 << " " << 1 << endl;
    return 0;
  }

  if (tmp > 2) {
    cout << "Impossible" << endl;
    return 0;
  }

  if (tmp == 0) {
    for (int i = 0; i < M; i++) {
      a.push_back(A[i]);
    }
  } else if (tmp == 1) {
    a.push_back(odd[0]);
    for (int i = 0; i < M; i++) {
      if (A[i] % 2 == 0) {
        a.push_back(A[i]);
      }
    }
  } else {
    a.push_back(odd[0]);
    for (int i = 0; i < M; i++) {
      if (A[i] % 2 == 0) {
        a.push_back(A[i]);
      }
    }
    a.push_back(odd[1]);
  }
  for (int i = 0; i < a.size(); i++) {
    if (i == 0) {
      if (a[0] == 1) {
        continue;
      }
      b.push_back(a[0] - 1);
    } else if (i == (a.size() - 1)) {
      b.push_back(a[i] + 1);
    } else {
      b.push_back(a[i]);
    }
  }

  for (int i = 0; i < a.size(); i++) {
    if (i != a.size() - 1) {
      cout << a[i] << " ";
    } else {
      cout << a[i] << endl;
    }
  }
  cout << b.size() << endl;
  for (int i = 0; i < b.size(); i++) {
    if (i != b.size() - 1) {
      cout << b[i] << " ";
    } else {
      cout << b[i] << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <typeinfo>
#include <vector>

#define DIV 1000000007

using namespace std;

long long N, M;
long long A[101];
vector<long long> odd;
vector<long long> a;
vector<long long> b;
int main() {
  cin >> N >> M;
  long long tmp = 0;
  for (int i = 0; i < M; i++) {
    cin >> A[i];
    if (A[i] % 2 == 1) {
      tmp++;
      odd.push_back(A[i]);
    }
  }
  if (M == 1) {
    if (A[0] == 1) {
      cout << A[0] << endl;
      cout << 1 << endl;
      cout << 1 << endl;
      return 0;
    }
    cout << A[0] << endl;
    cout << 2 << endl;
    cout << A[0] - 1 << " " << 1 << endl;
    return 0;
  }

  if (tmp > 2) {
    cout << "Impossible" << endl;
    return 0;
  }

  if (tmp == 0) {
    for (int i = 0; i < M; i++) {
      a.push_back(A[i]);
    }
  } else if (tmp == 1) {
    a.push_back(odd[0]);
    for (int i = 0; i < M; i++) {
      if (A[i] % 2 == 0) {
        a.push_back(A[i]);
      }
    }
  } else {
    a.push_back(odd[0]);
    for (int i = 0; i < M; i++) {
      if (A[i] % 2 == 0) {
        a.push_back(A[i]);
      }
    }
    a.push_back(odd[1]);
  }
  for (int i = 0; i < a.size(); i++) {
    if (i == 0) {
      if (a[0] == 1) {
        continue;
      }
      b.push_back(a[0] - 1);
    } else if (i == (a.size() - 1)) {
      b.push_back(a[i] + 1);
    } else {
      b.push_back(a[i]);
    }
  }

  for (int i = 0; i < a.size(); i++) {
    if (i != a.size() - 1) {
      cout << a[i] << " ";
    } else {
      cout << a[i] << endl;
    }
  }
  cout << b.size() << endl;
  for (int i = 0; i < b.size(); i++) {
    if (i != b.size() - 1) {
      cout << b[i] << " ";
    } else {
      cout << b[i] << endl;
    }
  }
}