Problem p03007 (C++)

Buggy submission (status: Runtime Error):
// #include C/C++ {
#include <bits/stdc++.h>
// }
using namespace std;
// #typedef {
typedef long long int64;
typedef pair<int, int> PII;
typedef pair<int64, int64> PLL;
// }

// #parameter{
#ifdef _DEBUG

#define TYPE decltype
#define RF(filename)                                                           \
  { freopen((filename), "r", stdin); }
#define WF(filename)                                                           \
  { freopen((filename), "w", stdout); }
#define DF(filename)                                                           \
  { freopen((filename), "w", stderr); }
#define eprintf printf

#else

#define TYPE __typeof
#define RF(filename)                                                           \
  { ; }
#define WF(filename)                                                           \
  { ; }
#define DF(filename)                                                           \
  { ; }
#define eprintf(...)
#define fprintf(...)

#endif

// #define {
#define SZ(a) ((int)(a).size())
#define X first
#define Y second
#define MP make_pair
#define L(x) ((x) << 1)
#define R(x) ((x) << 1 | 1)
#define max3(x, y, z) (max(max((x), (y)), (z)))
#define min3(x, y, z) (min(min((x), (y)), (z)))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(it) (it).begin(), (it).end()
#define FILL(__space, __val) memset(__space, __val, sizeof(__space))
#define MOVE(__spaceTo, __spaceFrom)                                           \
  memmove(__spaceTo, __spaceFrom, sizeof(__spaceTo))
#define UNIQUE(__vector)                                                       \
  sort(ALL(__vector)), __vector.resize(unique(ALL(__vector)) - __vector.begin())
#define FOR(it, c)                                                             \
  for (TYPE((c).begin()) it = (c).begin(); it != (c).end(); it++)
/////////////////////////////////////////////////////////////
const double PI = acos(-1.0);
const double EPS = 1e-6;

#define MAX_N 100005
#define MAX_M 55
#define MOD (int)(1e9 + 7)
#define INF ((1 << 30) - 1)
#define BINF ((1LL << 62) - 1LL)
#define NONE -1
#define NIL 0
// }

/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
int main() {
  RF("input.txt");
  //  WF("output.txt");
  DF("err.txt");

  int N;
  scanf("%d", &N);
  vector<int> neg, pos;
  int zero = 0;
  for (int i = 0; i < N; i++) {
    int A;
    scanf("%d", &A);
    if (A > 0)
      pos.push_back(A);
    else if (A < 0)
      neg.push_back(A);
    else
      zero++;
  }
  while (zero > 0) {
    if (neg.size() < pos.size())
      neg.push_back(0);
    else
      pos.push_back(0);
    zero--;
  }
  sort(ALL(neg));
  sort(ALL(pos));
  deque<int> negd, posd;
  for (int &num : neg)
    negd.push_back(num);
  for (int &num : pos)
    posd.push_back(num);

  vector<PII> opes;
  while (negd.size() + posd.size() > 1) {
    if (negd.size() == 0) {
      int a = posd.front();
      posd.pop_front();
      int b = posd.front();
      posd.pop_front();
      negd.push_back(a - b);
      opes.emplace_back(a, b);
    } else if (posd.size() > 1) {
      int a = negd.front();
      negd.pop_front();
      int b = posd.front();
      posd.pop_front();
      negd.push_back(a - b);
      opes.emplace_back(a, b);
    } else {
      int a = posd.front();
      posd.pop_front();
      int b = negd.front();
      negd.pop_front();
      posd.push_back(a - b);
      opes.emplace_back(a, b);
    }
  }
  int res = posd.front();
  printf("%d\n", res);
  for (PII &ope : opes)
    printf("%d %d\n", ope.X, ope.Y);
  return 0;
}

Fixed submission:
// #include C/C++ {
#include <bits/stdc++.h>
// }
using namespace std;
// #typedef {
typedef long long int64;
typedef pair<int, int> PII;
typedef pair<int64, int64> PLL;
// }

// #parameter{
#ifdef _DEBUG

#define TYPE decltype
#define RF(filename)                                                           \
  { freopen((filename), "r", stdin); }
#define WF(filename)                                                           \
  { freopen((filename), "w", stdout); }
#define DF(filename)                                                           \
  { freopen((filename), "w", stderr); }
#define eprintf printf

#else

#define TYPE __typeof
#define RF(filename)                                                           \
  { ; }
#define WF(filename)                                                           \
  { ; }
#define DF(filename)                                                           \
  { ; }
#define eprintf(...)
#define fprintf(...)

#endif

// #define {
#define SZ(a) ((int)(a).size())
#define X first
#define Y second
#define MP make_pair
#define L(x) ((x) << 1)
#define R(x) ((x) << 1 | 1)
#define max3(x, y, z) (max(max((x), (y)), (z)))
#define min3(x, y, z) (min(min((x), (y)), (z)))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(it) (it).begin(), (it).end()
#define FILL(__space, __val) memset(__space, __val, sizeof(__space))
#define MOVE(__spaceTo, __spaceFrom)                                           \
  memmove(__spaceTo, __spaceFrom, sizeof(__spaceTo))
#define UNIQUE(__vector)                                                       \
  sort(ALL(__vector)), __vector.resize(unique(ALL(__vector)) - __vector.begin())
#define FOR(it, c)                                                             \
  for (TYPE((c).begin()) it = (c).begin(); it != (c).end(); it++)
/////////////////////////////////////////////////////////////
const double PI = acos(-1.0);
const double EPS = 1e-6;

#define MAX_N 100005
#define MAX_M 55
#define MOD (int)(1e9 + 7)
#define INF ((1 << 30) - 1)
#define BINF ((1LL << 62) - 1LL)
#define NONE -1
#define NIL 0
// }

/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
int main() {
  RF("input.txt");
  //  WF("output.txt");
  DF("err.txt");

  int N;
  scanf("%d", &N);
  vector<int> neg, pos;
  int zero = 0;
  for (int i = 0; i < N; i++) {
    int A;
    scanf("%d", &A);
    if (A > 0)
      pos.push_back(A);
    else if (A < 0)
      neg.push_back(A);
    else
      zero++;
  }
  while (zero > 0) {
    if (neg.size() < pos.size())
      neg.push_back(0);
    else
      pos.push_back(0);
    zero--;
  }
  sort(ALL(neg));
  sort(ALL(pos));
  deque<int> negd, posd;
  for (int &num : neg)
    negd.push_back(num);
  for (int &num : pos)
    posd.push_back(num);

  vector<PII> opes;
  while (negd.size() + posd.size() > 1) {
    if (negd.size() == 0) {
      int a = posd.front();
      posd.pop_front();
      int b = posd.front();
      posd.pop_front();
      negd.push_back(a - b);
      opes.emplace_back(a, b);
    } else if (posd.size() == 0) {
      int a = negd.back();
      negd.pop_back();
      int b = negd.back();
      negd.pop_back();
      posd.push_back(a - b);
      opes.emplace_back(a, b);
    } else if (posd.size() > 1) {
      int a = negd.front();
      negd.pop_front();
      int b = posd.front();
      posd.pop_front();
      negd.push_back(a - b);
      opes.emplace_back(a, b);
    } else {
      int a = posd.front();
      posd.pop_front();
      int b = negd.front();
      negd.pop_front();
      posd.push_back(a - b);
      opes.emplace_back(a, b);
    }
  }
  int res = posd.front();
  printf("%d\n", res);
  for (PII &ope : opes)
    printf("%d %d\n", ope.X, ope.Y);
  return 0;
}