Problem p03007 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
typedef long long ll;
typedef long long lint;
#define REP(i, n) for (long long(i) = 0; (i) < (n); ++i)
#define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
const long long MOD = static_cast<long long>(1e9) + 7LL;
const long long INF = 1234567891234567890LL;
signed main() {
  ll N;
  cin >> N;
  multiset<ll> POS, NEG, ZER;
  stringstream ss;
  REP(i, N) {
    ll x;
    cin >> x;
    if (x > 0)
      POS.insert(x);
    else if (x < 0)
      NEG.insert(x);
    else
      ZER.insert(x);
  }
  if (NEG.size() == 0) {
    if (ZER.size()) {
      ll h = *POS.begin();
      NEG.insert(-h);
      POS.erase(POS.begin());
      ZER.erase(ZER.begin());
      ss << 0 << " " << h << "\n";
    } else {
      ll l = *POS.begin();
      ll h = *--POS.end();
      NEG.insert(l - h);
      POS.erase(POS.begin());
      POS.erase(--POS.end());
      ss << l << " " << h << "\n";
    }
  } else if (POS.size() == 0) {
    if (ZER.size()) {
      ll h = *NEG.begin();
      POS.insert(-h);
      NEG.erase(NEG.begin());
      ZER.erase(ZER.begin());
      ss << 0 << " " << h << "\n";
    } else {
      ll l = *NEG.begin();
      ll h = *--NEG.end();
      POS.insert(h - l);
      NEG.erase(NEG.begin());
      NEG.erase(--NEG.end());
      ss << h << " " << l << "\n";
    }
  }
  ll pos = *POS.begin();
  POS.erase(POS.begin());
  ll neg = *NEG.begin();
  NEG.erase(NEG.begin());
  for (auto &x : NEG) {
    ss << pos << " " << x << "\n";
    pos -= x;
  }
  for (auto &x : POS) {
    ss << neg << " " << x << "\n";
    neg -= x;
  }
  for (auto &x : ZER) {
    ss << pos << " " << x << "\n";
  }
  ss << pos << " " << neg << "\n";
  cout << pos - neg << endl;
  cout << ss.str();
  cout << flush;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
typedef long long ll;
typedef long long lint;
#define REP(i, n) for (long long(i) = 0; (i) < (n); ++i)
#define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
const long long MOD = static_cast<long long>(1e9) + 7LL;
const long long INF = 1234567891234567890LL;
signed main() {
  ll N;
  cin >> N;
  multiset<ll> POS, NEG, ZER;
  stringstream ss;
  if (N == 2) {
    ll x, y;
    cin >> x >> y;
    if (x < y)
      swap(x, y);
    cout << x - y << endl;
    cout << x << " " << y << endl;
    return 0;
  }
  REP(i, N) {
    ll x;
    cin >> x;
    if (x > 0)
      POS.insert(x);
    else if (x < 0)
      NEG.insert(x);
    else
      ZER.insert(x);
  }
  if (NEG.size() == 0) {
    if (ZER.size()) {
      ll h = *POS.begin();
      NEG.insert(-h);
      POS.erase(POS.begin());
      ZER.erase(ZER.begin());
      ss << 0 << " " << h << "\n";
    } else {
      ll l = *POS.begin();
      ll h = *--POS.end();
      NEG.insert(l - h);
      POS.erase(POS.begin());
      POS.erase(--POS.end());
      ss << l << " " << h << "\n";
    }
  } else if (POS.size() == 0) {
    if (ZER.size()) {
      ll h = *NEG.begin();
      POS.insert(-h);
      NEG.erase(NEG.begin());
      ZER.erase(ZER.begin());
      ss << 0 << " " << h << "\n";
    } else {
      ll l = *NEG.begin();
      ll h = *--NEG.end();
      POS.insert(h - l);
      NEG.erase(NEG.begin());
      NEG.erase(--NEG.end());
      ss << h << " " << l << "\n";
    }
  }
  ll pos = *POS.begin();
  POS.erase(POS.begin());
  ll neg = *NEG.begin();
  NEG.erase(NEG.begin());
  for (auto &x : NEG) {
    ss << pos << " " << x << "\n";
    pos -= x;
  }
  for (auto &x : POS) {
    ss << neg << " " << x << "\n";
    neg -= x;
  }
  for (auto &x : ZER) {
    ss << pos << " " << x << "\n";
  }
  ss << pos << " " << neg << "\n";
  cout << pos - neg << endl;
  cout << ss.str();
  cout << flush;
  return 0;
}