Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[100010] = {};
  int ans = 0;
  int pos = 0;
  int neg = 0;
  int zero = 0;
  vector<int> P;
  vector<int> M;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ans += abs(A[i]);
    if (A[i] > 0) {
      pos++;
      P.push_back(A[i]);
    }
    if (A[i] < 0) {
      neg++;
      M.push_back(A[i]);
    }
    if (A[i] == 0) {
      zero++;
      P.push_back(A[i]);
    }
  }
  sort(A, A + N);
  if (zero == 0 && pos == 0)
    ans += 2 * A[N - 1];
  if (zero == 0 && neg == 0)
    ans -= 2 * A[0];
  cout << ans << endl;
  if (zero == 0 && pos == 0) {
    int tmp = A[N - 1];
    for (int i = N - 2; i >= 0; i--) {
      cout << tmp << " " << A[i] << endl;
      tmp = tmp - A[i];
    }
  }
  if (zero == 0 && neg == 0) {
    int tmp = A[0];
    for (int i = 1; i < N - 1; i++) {
      cout << tmp << " " << A[i] << endl;
      tmp = tmp - A[i];
    }
    cout << A[N - 1] << " " << tmp << endl;
  } else if (pos + zero > neg && neg > 0) {
    cout << M[0] << " " << P[0] << endl;
    for (int i = 1; i < neg; i++) {
      cout << P[i] << " " << M[i] << endl;
    }
    int tmp = M[0] - P[0];
    for (int i = 1; i < neg; i++) {
      cout << tmp << " " << P[i] - M[i] << endl;
      tmp = tmp - P[i] + M[i];
    }
    for (int i = neg; i < pos + zero - 1; i++) {
      cout << tmp << " " << P[i] << endl;
      tmp = tmp - P[i];
    }
    cout << P[pos + zero - 1] << " " << tmp << endl;
  } else if (pos + zero < neg) {
    cout << P[0] << " " << M[0] << endl;
    for (int i = 1; i < pos + zero; i++) {
      cout << M[i] << " " << P[i] << endl;
    }
    int tmp = P[0] - M[0];
    for (int i = 1; i < pos + zero; i++) {
      cout << tmp << " " << M[i] - P[i] << endl;
      tmp = tmp - M[i] + P[i];
    }
    for (int i = pos + zero; i < neg; i++) {
      cout << tmp << " " << M[i] << endl;
      tmp = tmp - M[i];
    }
  } else if (pos + zero == neg) {
    cout << M[0] << " " << P[0] << endl;
    for (int i = 1; i < neg; i++) {
      cout << P[i] << " " << M[i] << endl;
    }
    int tmp = M[0] - P[0];
    for (int i = 1; i < neg - 1; i++) {
      cout << tmp << " " << P[i] - M[i] << endl;
      tmp = tmp - P[i] + M[i];
    }
    cout << P[pos + zero - 1] - M[pos + zero - 1] << " " << tmp << endl;
  } else if (neg == 0) {
    int tmp = A[0];
    for (int i = 1; i < N - 1; i++) {
      cout << tmp << " " << A[i] << endl;
      tmp = tmp - A[i];
    }
    cout << A[N - 1] << " " << tmp << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[100010] = {};
  int ans = 0;
  int pos = 0;
  int neg = 0;
  int zero = 0;
  vector<int> P;
  vector<int> M;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ans += abs(A[i]);
    if (A[i] > 0) {
      pos++;
      P.push_back(A[i]);
    }
    if (A[i] < 0) {
      neg++;
      M.push_back(A[i]);
    }
    if (A[i] == 0) {
      zero++;
      P.push_back(A[i]);
    }
  }
  sort(A, A + N);
  if (zero == 0 && pos == 0)
    ans += 2 * A[N - 1];
  if (zero == 0 && neg == 0)
    ans -= 2 * A[0];
  cout << ans << endl;
  if (zero == 0 && pos == 0) {
    int tmp = A[N - 1];
    for (int i = N - 2; i >= 0; i--) {
      cout << tmp << " " << A[i] << endl;
      tmp = tmp - A[i];
    }
  } else if (zero == 0 && neg == 0) {
    int tmp = A[0];
    for (int i = 1; i < N - 1; i++) {
      cout << tmp << " " << A[i] << endl;
      tmp = tmp - A[i];
    }
    cout << A[N - 1] << " " << tmp << endl;
  } else if (pos + zero > neg && neg > 0) {
    cout << M[0] << " " << P[0] << endl;
    for (int i = 1; i < neg; i++) {
      cout << P[i] << " " << M[i] << endl;
    }
    int tmp = M[0] - P[0];
    for (int i = 1; i < neg; i++) {
      cout << tmp << " " << P[i] - M[i] << endl;
      tmp = tmp - P[i] + M[i];
    }
    for (int i = neg; i < pos + zero - 1; i++) {
      cout << tmp << " " << P[i] << endl;
      tmp = tmp - P[i];
    }
    cout << P[pos + zero - 1] << " " << tmp << endl;
  } else if (pos + zero < neg) {
    cout << P[0] << " " << M[0] << endl;
    for (int i = 1; i < pos + zero; i++) {
      cout << M[i] << " " << P[i] << endl;
    }
    int tmp = P[0] - M[0];
    for (int i = 1; i < pos + zero; i++) {
      cout << tmp << " " << M[i] - P[i] << endl;
      tmp = tmp - M[i] + P[i];
    }
    for (int i = pos + zero; i < neg; i++) {
      cout << tmp << " " << M[i] << endl;
      tmp = tmp - M[i];
    }
  } else if (pos + zero == neg) {
    cout << M[0] << " " << P[0] << endl;
    for (int i = 1; i < neg; i++) {
      cout << P[i] << " " << M[i] << endl;
    }
    int tmp = M[0] - P[0];
    for (int i = 1; i < neg - 1; i++) {
      cout << tmp << " " << P[i] - M[i] << endl;
      tmp = tmp - P[i] + M[i];
    }
    cout << P[pos + zero - 1] - M[pos + zero - 1] << " " << tmp << endl;
  } else if (neg == 0) {
    int tmp = A[0];
    for (int i = 1; i < N - 1; i++) {
      cout << tmp << " " << A[i] << endl;
      tmp = tmp - A[i];
    }
    cout << A[N - 1] << " " << tmp << endl;
  }
}