Problem p03007 (C++)

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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, size_t size> void fill(T (&ary)[size], const T &val) {
  fill_n((T *)ary, size, val);
}

void solve() {
  // const i64 mod = 1'000'000'007;

  i64 N;
  cin >> N;
  vector<i64> A(N);
  rep(i, 0, N) { cin >> A[i]; }

  sort(rbegin(A), rend(A));

  i64 plus = 1, minus = 1;
  vector<i64> l = {A[0]}, r = {A[N - 1]};
  rep(i, 1, N - 1) {
    if (A[i] >= 0) {
      ++plus;
      l.emplace_back(A[i]);
    } else {
      --minus;
      r.emplace_back(A[i]);
    }
  }

  vector<i64> x, y;

  function<i64(i64)> dfs = [&](i64 sgn) {
    if (sgn == 0) {
      --plus;
      --minus;
      i64 a = dfs(1), b = dfs(-1);
      x.emplace_back(a);
      y.emplace_back(b);
      return a - b;
    }
    if (sgn == 1) {
      if (minus > 0) {
        --minus;
        i64 a = dfs(1), b = dfs(-1);
        x.emplace_back(a);
        y.emplace_back(b);
        return a - b;
      } else {
        i64 res = l.back();
        l.pop_back();
        return res;
      }
    } else {
      if (plus > 0) {
        --plus;
        i64 a = dfs(-1), b = dfs(1);
        x.emplace_back(a);
        y.emplace_back(b);
        return a - b;
      } else {
        i64 res = r.back();
        r.pop_back();
        return res;
      }
    }
  };

  i64 M = dfs(0);
  cout << M << endl;
  rep(i, 0, N - 1) { cout << x[i] << " " << y[i] << endl; }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, size_t size> void fill(T (&ary)[size], const T &val) {
  fill_n((T *)ary, size, val);
}

void solve() {
  // const i64 mod = 1'000'000'007;

  i64 N;
  cin >> N;
  vector<i64> A(N);
  rep(i, 0, N) { cin >> A[i]; }

  sort(rbegin(A), rend(A));

  i64 plus = 1, minus = 1;
  vector<i64> l = {A[0]}, r = {A[N - 1]};
  rep(i, 1, N - 1) {
    if (A[i] >= 0) {
      ++plus;
      l.emplace_back(A[i]);
    } else {
      ++minus;
      r.emplace_back(A[i]);
    }
  }

  vector<i64> x, y;

  function<i64(i64)> dfs = [&](i64 sgn) {
    if (sgn == 0) {
      --plus;
      --minus;
      i64 a = dfs(1), b = dfs(-1);
      x.emplace_back(a);
      y.emplace_back(b);
      return a - b;
    }
    if (sgn == 1) {
      if (minus > 0) {
        --minus;
        i64 a = dfs(1), b = dfs(-1);
        x.emplace_back(a);
        y.emplace_back(b);
        return a - b;
      } else {
        i64 res = l.back();
        l.pop_back();
        return res;
      }
    } else {
      if (plus > 0) {
        --plus;
        i64 a = dfs(-1), b = dfs(1);
        x.emplace_back(a);
        y.emplace_back(b);
        return a - b;
      } else {
        i64 res = r.back();
        r.pop_back();
        return res;
      }
    }
  };

  i64 M = dfs(0);
  cout << M << endl;
  rep(i, 0, N - 1) { cout << x[i] << " " << y[i] << endl; }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}