Problem p03007 (C++)

Buggy submission (status: Runtime Error):
// includes {{{
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
template <int n, class... T>
typename std::enable_if<(n >= sizeof...(T))>::type
__output_tuple(std::ostream &, std::tuple<T...> const &) {}
template <int n, class... T>
typename std::enable_if<(n < sizeof...(T))>::type
__output_tuple(std::ostream &os, std::tuple<T...> const &t) {
  os << (n == 0 ? "" : ", ") << std::get<n>(t);
  __output_tuple<n + 1>(os, t);
}
template <class... T>
std::ostream &operator<<(std::ostream &os, std::tuple<T...> const &t) {
  os << "(";
  __output_tuple<0>(os, t);
  os << ")";
  return os;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, std::pair<T, U> const &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::stack<T> &a) {
  os << "{";
  for (auto tmp = a; tmp.size(); tmp.pop())
    os << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  os << "}";
  return os;
}
template <class T, class Container, class Compare>
std::ostream &operator<<(std::ostream &os,
                         std::priority_queue<T, Container, Compare> a) {
  os << "{ (top) ";
  while (a.size())
    os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template <class T, class Container>
std::ostream &operator<<(std::ostream &os, std::queue<T, Container> a) {
  os << "{ ";
  while (a.size())
    os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
#ifdef DEBUG
#if !defined(DEBUG_OUT)
// #define DEBUG_OUT std::cerr
#endif
#define dump(...)                                                              \
  [&]() {                                                                      \
    auto __debug_tap = std::make_tuple(__VA_ARGS__);                           \
    DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = "              \
              << __debug_tap << std::endl;                                     \
  }()
template <class T> inline void dump2D(T &d, size_t sizey, size_t sizex) {
  for (size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << "\t";
    for (size_t j = 0; j < sizex; j++)
      DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t");
    DEBUG_OUT << std::endl;
  }
}
template <class T> inline void dump1D(T &d, size_t sizey) {
  for (size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " ");
  }
  DEBUG_OUT << std::endl;
}
template <
    class T,
    class = typename std::iterator_traits<decltype(begin(T()))>::value_type,
    class = typename std::enable_if<!std::is_same<T, std::string>::value>::type>
std::ostream &operator<<(std::ostream &os, const T &a) {
  os << "{";
  for (auto ite = begin(a); ite != end(a); ++ite)
    os << (ite == begin(a) ? "" : ", ") << *ite;
  os << "}";
  return os;
}
#else
#define dump(...) ((void)42)
#define dump2D(...) ((void)42)
#define dump1D(...) ((void)42)
template <
    class T,
    class = typename std::iterator_traits<decltype(begin(T()))>::value_type,
    class = typename std::enable_if<!std::is_same<T, std::string>::value>::type>
std::ostream &operator<<(std::ostream &os, const T &a) {
  for (auto ite = begin(a); ite != end(a); ++ite)
    os << (ite == begin(a) ? "" : " ") << *ite;
  return os;
}
#endif
// }}}

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n;
  cin >> n;
  vector<int> neg, pos;
  int zero = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < 0)
      neg.push_back(a);
    else if (a > 0)
      pos.push_back(a);
    else
      zero++;
  }
  if (neg.size() == 0 || pos.size() == 0)
    if (zero) {
      if (neg.size())
        for (int i = 0; i < zero; i++)
          neg.push_back(0);
      else
        for (int i = 0; i < zero; i++)
          pos.push_back(0);
    }
  vector<pair<int, int>> ans;
  int M = 0;
  if (pos.size() == 0) {
    dump(1);
    sort(begin(neg), end(neg));
    int m = neg.size();
    int x = neg.back();
    for (int i = 0; i < m - 1; i++) {
      ans.emplace_back(x, neg[i]);
      x = x - neg[i];
    }
    M = x;
  } else if (neg.size() == 0) {
    dump(2);
    sort(begin(pos), end(pos));
    int m = pos.size();
    if (m == 2) {
      ans.emplace_back(pos[1], pos[0]);
      M = pos[1] - pos[0];
    } else {
      int x = pos.front();
      for (int i = 1; i < m - 1; i++) {
        ans.emplace_back(x, pos[i]);
        x -= pos[i];
      }
      ans.emplace_back(pos.back(), x);
      M = pos.back() - x;
    }
  } else {
    dump(3);
    int x = neg[0];
    for (int i = 1; i < pos.size(); i++) {
      ans.emplace_back(x, pos[i]);
      x -= pos[i];
    }
    int y = pos[0];
    for (int i = 1; i < neg.size(); i++) {
      ans.emplace_back(y, neg[i]);
      y -= neg[i];
    }
    ans.emplace_back(y, x);
    M = y - x;
  }

  dump(pos, neg);
  dump(ans);
  assert(ans.size() == n - 1);
  cout << M << endl;
  for (int i = 0; i < n - 1; i++)
    cout << ans[i].first << " " << ans[i].second << "\n";
  return 0;
}


Fixed submission:
// includes {{{
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
template <int n, class... T>
typename std::enable_if<(n >= sizeof...(T))>::type
__output_tuple(std::ostream &, std::tuple<T...> const &) {}
template <int n, class... T>
typename std::enable_if<(n < sizeof...(T))>::type
__output_tuple(std::ostream &os, std::tuple<T...> const &t) {
  os << (n == 0 ? "" : ", ") << std::get<n>(t);
  __output_tuple<n + 1>(os, t);
}
template <class... T>
std::ostream &operator<<(std::ostream &os, std::tuple<T...> const &t) {
  os << "(";
  __output_tuple<0>(os, t);
  os << ")";
  return os;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, std::pair<T, U> const &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::stack<T> &a) {
  os << "{";
  for (auto tmp = a; tmp.size(); tmp.pop())
    os << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  os << "}";
  return os;
}
template <class T, class Container, class Compare>
std::ostream &operator<<(std::ostream &os,
                         std::priority_queue<T, Container, Compare> a) {
  os << "{ (top) ";
  while (a.size())
    os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template <class T, class Container>
std::ostream &operator<<(std::ostream &os, std::queue<T, Container> a) {
  os << "{ ";
  while (a.size())
    os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
#ifdef DEBUG
#if !defined(DEBUG_OUT)
// #define DEBUG_OUT std::cerr
#endif
#define dump(...)                                                              \
  [&]() {                                                                      \
    auto __debug_tap = std::make_tuple(__VA_ARGS__);                           \
    DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = "              \
              << __debug_tap << std::endl;                                     \
  }()
template <class T> inline void dump2D(T &d, size_t sizey, size_t sizex) {
  for (size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << "\t";
    for (size_t j = 0; j < sizex; j++)
      DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t");
    DEBUG_OUT << std::endl;
  }
}
template <class T> inline void dump1D(T &d, size_t sizey) {
  for (size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " ");
  }
  DEBUG_OUT << std::endl;
}
template <
    class T,
    class = typename std::iterator_traits<decltype(begin(T()))>::value_type,
    class = typename std::enable_if<!std::is_same<T, std::string>::value>::type>
std::ostream &operator<<(std::ostream &os, const T &a) {
  os << "{";
  for (auto ite = begin(a); ite != end(a); ++ite)
    os << (ite == begin(a) ? "" : ", ") << *ite;
  os << "}";
  return os;
}
#else
#define dump(...) ((void)42)
#define dump2D(...) ((void)42)
#define dump1D(...) ((void)42)
template <
    class T,
    class = typename std::iterator_traits<decltype(begin(T()))>::value_type,
    class = typename std::enable_if<!std::is_same<T, std::string>::value>::type>
std::ostream &operator<<(std::ostream &os, const T &a) {
  for (auto ite = begin(a); ite != end(a); ++ite)
    os << (ite == begin(a) ? "" : " ") << *ite;
  return os;
}
#endif
// }}}

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n;
  cin >> n;
  vector<int> neg, pos;
  int zero = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < 0)
      neg.push_back(a);
    else if (a > 0)
      pos.push_back(a);
    else
      zero++;
  }
  if (neg.size()) {
    for (int i = 0; i < zero; i++)
      neg.push_back(0);
  } else {
    for (int i = 0; i < zero; i++)
      pos.push_back(0);
  }
  assert(pos.size() + neg.size() == n);
  vector<pair<int, int>> ans;
  int M = 0;
  if (pos.size() == 0) {
    dump(1);
    sort(begin(neg), end(neg));
    int m = neg.size();
    int x = neg.back();
    for (int i = 0; i < m - 1; i++) {
      ans.emplace_back(x, neg[i]);
      x = x - neg[i];
    }
    M = x;
  } else if (neg.size() == 0) {
    dump(2);
    sort(begin(pos), end(pos));
    int m = pos.size();
    if (m == 2) {
      ans.emplace_back(pos[1], pos[0]);
      M = pos[1] - pos[0];
    } else {
      int x = pos.front();
      for (int i = 1; i < m - 1; i++) {
        ans.emplace_back(x, pos[i]);
        x -= pos[i];
      }
      ans.emplace_back(pos.back(), x);
      M = pos.back() - x;
    }
  } else {
    dump(3);
    int x = neg[0];
    for (int i = 1; i < pos.size(); i++) {
      ans.emplace_back(x, pos[i]);
      x -= pos[i];
    }
    int y = pos[0];
    for (int i = 1; i < neg.size(); i++) {
      ans.emplace_back(y, neg[i]);
      y -= neg[i];
    }
    ans.emplace_back(y, x);
    M = y - x;
  }

  dump(pos, neg);
  dump(ans);
  assert(ans.size() == n - 1);
  cout << M << endl;
  for (int i = 0; i < n - 1; i++)
    cout << ans[i].first << " " << ans[i].second << "\n";
  return 0;
}