Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

int main(void) {
  int n;
  vector<ll> A;
  ll a, countP, countM;
  countP = 0;
  countM = 0;
  cin >> n;
  rep(i, n) {
    cin >> a;
    A.push_back(a);
    if (a >= 0) {
      countP++;
    }
    if (a <= 0) {
      countM++;
    }
  }

  sort(all(A));
  reverse(all(A));
  ll ans = 0;

  if (countP == 0) {
    rep(i, n) { ans += abs(A[i]); }
    ans -= abs(A[0]) * 2;
    cout << ans << endl;
    ll memo = A[0];
    rep(i, n) {
      if (i == 0)
        continue;
      cout << memo << " " << A[i] << endl;
      memo -= A[i];
    }
    return 0;
  } else if (countM == 0) {
    sort(all(A));
    rep(i, n) { ans += A[i]; }
    ans -= A[0] * 2;
    cout << ans << endl;
    ll memo = A[0];
    rep(i, n) {
      if (i == 0)
        continue;
      if (i == n - 1) {
        cout << A[i] << " " << memo << endl;
        return 0;
      }
      cout << memo << " " << A[i] << endl;
      memo -= A[i];
    }
  } else { // 降順
    rep(i, n) { ans += abs(A[i]); }
    cout << ans << endl;
    int idx = 1;
    while (A[idx] >= 0) {
      cout << A[n - 1] << " " << A[idx] << endl;
      A[n - 1] -= A[idx];
      idx++;
    }
    while (idx <= n - 2) {
      cout << A[0] << " " << A[idx] << endl;
      A[0] -= A[idx];
      idx++;
    }
    cout << A[0] << " " << A[n - 1] << endl;
    return 0;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

int main(void) {
  int n;
  vector<ll> A;
  ll a, countP, countM;
  countP = 0;
  countM = 0;
  cin >> n;
  rep(i, n) {
    cin >> a;
    A.push_back(a);
    if (a >= 0) {
      countP++;
    }
    if (a <= 0) {
      countM++;
    }
  }

  sort(all(A));
  reverse(all(A));
  ll ans = 0;

  if (countP == 0) {
    rep(i, n) { ans += abs(A[i]); }
    ans -= abs(A[0]) * 2;
    cout << ans << endl;
    ll memo = A[0];
    rep(i, n) {
      if (i == 0)
        continue;
      cout << memo << " " << A[i] << endl;
      memo -= A[i];
    }
    return 0;
  } else if (countM == 0) {
    sort(all(A));
    rep(i, n) { ans += A[i]; }
    ans -= A[0] * 2;
    cout << ans << endl;
    ll memo = A[0];
    rep(i, n) {
      if (i == 0)
        continue;
      if (i == n - 1) {
        cout << A[i] << " " << memo << endl;
        return 0;
      }
      cout << memo << " " << A[i] << endl;
      memo -= A[i];
    }
  } else { // 降順
    rep(i, n) { ans += abs(A[i]); }
    cout << ans << endl;
    int idx = 1;
    while (A[idx] >= 0 && idx <= n - 2) {
      cout << A[n - 1] << " " << A[idx] << endl;
      A[n - 1] -= A[idx];
      idx++;
    }
    while (idx <= n - 2) {
      cout << A[0] << " " << A[idx] << endl;
      A[0] -= A[idx];
      idx++;
    }
    cout << A[0] << " " << A[n - 1] << endl;
    return 0;
  }

  return 0;
}