Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

#define swap(a, b)                                                             \
  do {                                                                         \
    typeof(a) __tmp = (a);                                                     \
    (a) = (b);                                                                 \
    (b) = __tmp;                                                               \
  } while (0)
#define repd(i, a, b) for (typeof(b) i = a; i < (b); ++i)
#define rep(i, n) repd(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define YesNo(x) cout << ((x) ? "Yes" : "No") << endl
#define YESNO(x) cout << ((x) ? "YES" : "NO") << endl
#define absi(x) (((x) >= 0) ? (x) : (-(x)))

int main(int argc, const char *argv[]) {
  ll n;
  cin >> n;

  vector<ll> a, b;
  ll asum = 0;
  ll bsum = 0;
  rep(i, n) {
    ll tmp;
    cin >> tmp;

    if (tmp >= 0) {
      a.push_back(tmp);
      asum += tmp;
    } else {
      b.push_back(tmp);
      bsum += tmp;
    }
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  vector<pair<ll, ll>> ret(n - 1);
  if (b.size() > 0) { // 負の数がある
    cout << asum - bsum << endl;

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

    rep(i, b.size()) {
      cout << a.back() << " " << b[i] << endl;
      a.back() -= b[i];
    }
  } else {
    cout << asum - a[0] * 2 << endl;

    for (ll i = 1; i < a.size() - 1; ++i) {
      cout << a[0] << " " << a[i] << endl;
      a[0] -= a[i];
    }

    cout << a.back() << " " << a[0] << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

#define swap(a, b)                                                             \
  do {                                                                         \
    typeof(a) __tmp = (a);                                                     \
    (a) = (b);                                                                 \
    (b) = __tmp;                                                               \
  } while (0)
#define repd(i, a, b) for (typeof(b) i = a; i < (b); ++i)
#define rep(i, n) repd(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define YesNo(x) cout << ((x) ? "Yes" : "No") << endl
#define YESNO(x) cout << ((x) ? "YES" : "NO") << endl
#define absi(x) (((x) >= 0) ? (x) : (-(x)))

int main(int argc, const char *argv[]) {
  ll n;
  cin >> n;

  vector<ll> a, b;
  ll asum = 0;
  ll bsum = 0;
  rep(i, n) {
    ll tmp;
    cin >> tmp;

    if (tmp >= 0) {
      a.push_back(tmp);
      asum += tmp;
    } else {
      b.push_back(tmp);
      bsum += tmp;
    }
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  vector<pair<ll, ll>> ret(n - 1);
  if (b.size() > 0) { // 負の数がある
    if (a.size() == 0) {
      cout << -bsum + b.back() * 2 << endl;

      for (ll i = 0; i < b.size() - 1; ++i) {
        cout << b.back() << " " << b[i] << endl;
        b.back() -= b[i];
      }
      return 0;
    }

    cout << asum - bsum << endl;

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

    rep(i, b.size()) {
      cout << a.back() << " " << b[i] << endl;
      a.back() -= b[i];
    }
  } else {
    cout << asum - a[0] * 2 << endl;

    for (ll i = 1; i < a.size() - 1; ++i) {
      cout << a[0] << " " << a[i] << endl;
      a[0] -= a[i];
    }

    cout << a.back() << " " << a[0] << endl;
  }

  return 0;
}