Problem p03007 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define INF 1 << 30
#define LINF 1LL << 62
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
const int MOD = 1000000007;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<ll> a(n);
  bool pos = true;
  bool neg = true;
  ll sum = 0;
  ll dsum = 0;
  ll mi = LINF;
  ll ma = -LINF;
  vector<ll> ps, ns;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    dsum += abs(a[i]);
    mi = min(mi, a[i]);
    ma = max(ma, a[i]);
    if (a[i] < 0)
      pos = false;
    if (a[i] > 0)
      neg = false;
    if (a[i] >= 0)
      ps.pb(a[i]);
    else
      ns.pb(a[i]);
  }
  sort(all(ps));
  sort(all(ns));
  reverse(all(ns));
  if (n == 2) {
    cout << ma - mi << endl;
    cout << ma << " " << mi << endl;
    return 0;
  }
  if (pos) {
    cout << sum - 2 * mi << endl;
    ll tmp = mi;
    FOR(i, 1, n - 1) {
      cout << tmp << " " << ps[i] << endl;
      tmp -= ps[i];
    }
    cout << ps[n - 1] << " " << tmp << endl;
    return 0;
  }
  if (neg) {
    cout << -(sum - 2 * ma) << endl;
    ll tmp = ma;
    FOR(i, 1, n) {
      cout << tmp << " " << ns[i] << endl;
      tmp -= ns[i];
    }
    return 0;
  }
  cout << dsum << endl;
  rep(i, ps.size() - 1) {
    cout << ns[0] << " " << ps[i] << endl;
    ;
    ns[0] -= ps[i];
  }
  rep(i, ns.size()) {
    cout << ps[ps.size() - 1] << " " << ns[i] << endl;
    ps[ps.size() - 1] -= ns[i];
  }
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define INF 1 << 30
#define LINF 1LL << 62
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
const int MOD = 1000000007;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<ll> a(n);
  bool pos = true;
  bool neg = true;
  ll sum = 0;
  ll dsum = 0;
  ll mi = LINF;
  ll ma = -LINF;
  vector<ll> ps, ns;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    dsum += abs(a[i]);
    mi = min(mi, a[i]);
    ma = max(ma, a[i]);
    if (a[i] < 0)
      pos = false;
    if (a[i] >= 0)
      neg = false;
    if (a[i] >= 0)
      ps.pb(a[i]);
    else
      ns.pb(a[i]);
  }
  sort(all(ps));
  sort(all(ns));
  reverse(all(ns));
  if (n == 2) {
    cout << ma - mi << endl;
    cout << ma << " " << mi << endl;
    return 0;
  }
  if (pos) {
    cout << sum - 2 * mi << endl;
    ll tmp = mi;
    FOR(i, 1, n - 1) {
      cout << tmp << " " << ps[i] << endl;
      tmp -= ps[i];
    }
    cout << ps[n - 1] << " " << tmp << endl;
    return 0;
  }
  if (neg) {
    cout << -(sum - 2 * ma) << endl;
    ll tmp = ma;
    FOR(i, 1, n) {
      cout << tmp << " " << ns[i] << endl;
      tmp -= ns[i];
    }
    return 0;
  }
  cout << dsum << endl;
  rep(i, ps.size() - 1) {
    cout << ns[0] << " " << ps[i] << endl;
    ;
    ns[0] -= ps[i];
  }
  rep(i, ns.size()) {
    cout << ps[ps.size() - 1] << " " << ns[i] << endl;
    ps[ps.size() - 1] -= ns[i];
  }
}