Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#ifdef __GNUC__
#pragma GCC optimize(2)
#endif

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#pragma warning(disable : 4996)
#pragma warning(error : 4715 4700 4703 4533 4789 4717)
#endif

using namespace std;

namespace Bigcat {
template <typename T> inline void max_s(T &a, const T &b) { a = max(a, b); }
template <typename T> inline void min_s(T &a, const T &b) { a = min(a, b); }

template <typename T> void sort_v(T &x) { sort(x.begin(), x.end()); }
template <typename T> void erase_dup(T &x) {
  x.erase(unique(x.begin(), x.end()), x.end());
} // erase duplicate
template <typename T, typename V> int pos_l(const T &x, const V &v) {
  return lower_bound(x.begin(), x.end(), v) - x.begin();
}
template <typename T, typename V> int pos_u(const T &x, const V &v) {
  return upper_bound(x.begin(), x.end(), v) - x.begin();
}

inline unsigned rand_g() {
  static mt19937 mt_rand((int)time(0));
  return mt_rand();
}
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using PII = pair<int, int>;
using VPII = vector<PII>;
} // namespace Bigcat
#define Rep(i, a) for (int i = 0; i < (int)(a); i++)
#define Loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Rep_r(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define Size(v) ((int)v.size())

using namespace Bigcat;
const LL base = LL(1e9 + 7);
const LL INFL = 1LL << 62;
const int INF = 1 << 30;
const int MAX = int(2e5 + 100);

struct Node {
  int x, y, w;
};
bool operator<(const Node &n0, const Node &n1) { return n0.x < n1.y; }

void solve1() {

  LL result = 0;
  int n;

  cin >> n;
  VI za(n);
  Rep(i, n) { cin >> za[i]; }
  sort_v(za);
  vector<bool> zp(n);
  zp[0] = false;
  zp.back() = true;
  Loop(i, 1, n - 1) {
    if (za[i] < 0)
      zp[i] = false;
    else
      zp[i] = true;
  }
  VI zs1, zs2;
  Rep(i, n) {
    if (zp[i])
      result += za[i];
    else
      result -= za[i];
    if (zp[i]) {
      zs1.push_back(za[i]);
    } else {
      zs2.push_back(za[i]);
    }
  }
  cout << result << endl;
  if (Size(zs1) == 1) {
    int now = *zs1.begin();
    for (int x : zs2) {
      cout << now << ' ' << x << '\n';
      now -= x;
    }
    assert(now == result);
  } else {
    int now = *zs2.begin();
    Rep(i, Size(zs1) - 1) {
      int x = zs1[i];
      cout << now << ' ' << x << '\n';
      now -= x;
    }
    cout << zs1.back() << ' ' << now << '\n';
    now = zs1.back() - now;
    Loop(i, 1, Size(zs2)) {
      int x = zs1[i];
      cout << now << ' ' << x << '\n';
      now -= x;
    }
    assert(now == result);
  }
}
const bool _multi_test = false;
const int _inId = 1;
const int _outId = 0;
void io_init(char *s0) {
  string path(s0);
  int a = path.find("build\\");
  int b = path.find(
#ifdef _DEBUG
      "Debug\\"
#else
      "Release\\"
#endif
  );
  if (a > 0 && b > a) {
    path = path.substr(0, a) + path.substr(a + 6, b - a - 6);
  } else
    path = "";

  if (_inId > 0) {
    cerr << "redirect stdin to input " << _inId << endl;
    string s = "input";
    s += to_string(_inId);
    s += ".txt";
    if (path != "")
      s = path + s;
    auto r = freopen(s.c_str(), "r", stdin);
    if (r == nullptr) {
      cerr << "invalid input file " << s << endl;
      ;
      r = freopen("CON", "r", stdin); // to do  con??
    }
  }
  if (_outId > 0) {
    cerr << "redirect stdout to stdout " << _outId << endl;
    string s = "stdout";
    s += to_string(_outId);
    s += ".txt";
    if (path != "")
      s = path + s;
    auto r = freopen(s.c_str(), "w", stdout);
    if (r == nullptr) {
      cerr << "invalid output file " << s << endl;
      ;
      r = freopen("CON", "w", stdout);
    }
  }
}

const bool _local_test =
#ifdef _LOCAL_TEST
    true;
#else
    false;
#endif
void solve() {
  int t = 1;
  if (_multi_test)
    cin >> t;
  if (_local_test)
    cerr << " now base " << base << endl;
  for (int i = 0; i < t; ++i) {
    if (_local_test)
      cerr << " tasks: " << i + 1 << endl;
    // cout << "Case #" << i + 1 << ": ";
    solve1();
  }
  int x = 1;
}

signed main(signed argc, char *argv[]) {

  if (_local_test) {
    if (argc == 1) {
      io_init(argv[0]);
    }
  }
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0); // close sync between cout and printf
  cin.exceptions(cin.failbit);
  cout.precision(11);
  solve();
}


Fixed submission:
#ifdef __GNUC__
#pragma GCC optimize(2)
#endif

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#pragma warning(disable : 4996)
#pragma warning(error : 4715 4700 4703 4533 4789 4717)
#endif

using namespace std;

namespace Bigcat {
template <typename T> inline void max_s(T &a, const T &b) { a = max(a, b); }
template <typename T> inline void min_s(T &a, const T &b) { a = min(a, b); }

template <typename T> void sort_v(T &x) { sort(x.begin(), x.end()); }
template <typename T> void erase_dup(T &x) {
  x.erase(unique(x.begin(), x.end()), x.end());
} // erase duplicate
template <typename T, typename V> int pos_l(const T &x, const V &v) {
  return lower_bound(x.begin(), x.end(), v) - x.begin();
}
template <typename T, typename V> int pos_u(const T &x, const V &v) {
  return upper_bound(x.begin(), x.end(), v) - x.begin();
}

inline unsigned rand_g() {
  static mt19937 mt_rand((int)time(0));
  return mt_rand();
}
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using PII = pair<int, int>;
using VPII = vector<PII>;
} // namespace Bigcat
#define Rep(i, a) for (int i = 0; i < (int)(a); i++)
#define Loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Rep_r(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define Size(v) ((int)v.size())

using namespace Bigcat;
const LL base = LL(1e9 + 7);
const LL INFL = 1LL << 62;
const int INF = 1 << 30;
const int MAX = int(2e5 + 100);

struct Node {
  int x, y, w;
};
bool operator<(const Node &n0, const Node &n1) { return n0.x < n1.y; }

void solve1() {

  LL result = 0;
  int n;

  cin >> n;
  VI za(n);
  Rep(i, n) { cin >> za[i]; }
  sort_v(za);
  vector<bool> zp(n);
  zp[0] = false;
  zp.back() = true;
  Loop(i, 1, n - 1) {
    if (za[i] < 0)
      zp[i] = false;
    else
      zp[i] = true;
  }
  VI zs1, zs2;
  Rep(i, n) {
    if (zp[i])
      result += za[i];
    else
      result -= za[i];
    if (zp[i]) {
      zs1.push_back(za[i]);
    } else {
      zs2.push_back(za[i]);
    }
  }
  cout << result << endl;
  if (Size(zs1) == 1) {
    int now = *zs1.begin();
    for (int x : zs2) {
      cout << now << ' ' << x << '\n';
      now -= x;
    }
    assert(now == result);
  } else {
    int now = *zs2.begin();
    Rep(i, Size(zs1) - 1) {
      int x = zs1[i];
      cout << now << ' ' << x << '\n';
      now -= x;
    }
    cout << zs1.back() << ' ' << now << '\n';
    now = zs1.back() - now;
    Loop(i, 1, Size(zs2)) {
      int x = zs2[i];
      cout << now << ' ' << x << '\n';
      now -= x;
    }
    assert(now == result);
  }
}
const bool _multi_test = false;
const int _inId = 1;
const int _outId = 0;
void io_init(char *s0) {
  string path(s0);
  int a = path.find("build\\");
  int b = path.find(
#ifdef _DEBUG
      "Debug\\"
#else
      "Release\\"
#endif
  );
  if (a > 0 && b > a) {
    path = path.substr(0, a) + path.substr(a + 6, b - a - 6);
  } else
    path = "";

  if (_inId > 0) {
    cerr << "redirect stdin to input " << _inId << endl;
    string s = "input";
    s += to_string(_inId);
    s += ".txt";
    if (path != "")
      s = path + s;
    auto r = freopen(s.c_str(), "r", stdin);
    if (r == nullptr) {
      cerr << "invalid input file " << s << endl;
      ;
      r = freopen("CON", "r", stdin); // to do  con??
    }
  }
  if (_outId > 0) {
    cerr << "redirect stdout to stdout " << _outId << endl;
    string s = "stdout";
    s += to_string(_outId);
    s += ".txt";
    if (path != "")
      s = path + s;
    auto r = freopen(s.c_str(), "w", stdout);
    if (r == nullptr) {
      cerr << "invalid output file " << s << endl;
      ;
      r = freopen("CON", "w", stdout);
    }
  }
}

const bool _local_test =
#ifdef _LOCAL_TEST
    true;
#else
    false;
#endif
void solve() {
  int t = 1;
  if (_multi_test)
    cin >> t;
  if (_local_test)
    cerr << " now base " << base << endl;
  for (int i = 0; i < t; ++i) {
    if (_local_test)
      cerr << " tasks: " << i + 1 << endl;
    // cout << "Case #" << i + 1 << ": ";
    solve1();
  }
  int x = 1;
}

signed main(signed argc, char *argv[]) {

  if (_local_test) {
    if (argc == 1) {
      io_init(argv[0]);
    }
  }
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0); // close sync between cout and printf
  cin.exceptions(cin.failbit);
  cout.precision(11);
  solve();
}