Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()
struct revran {
  const i64 b, e;
  struct iter {
    i64 i;
    constexpr iter(const i64 i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr i64 operator*() const noexcept { return i; }
    constexpr bool operator!=(const iter x) const noexcept { return i != x.i; }
  };
  constexpr revran(const i64 e) noexcept : b(0), e(e) {}
  constexpr revran(const i64 b, const i64 e) noexcept : b(b), e(e) {}
  constexpr iter begin() const noexcept { return iter(e - 1); }
  constexpr iter end() const noexcept { return iter(b - 1); }
};

struct ran {
  const i64 b, e;
  struct iter {
    i64 i;
    constexpr iter(const i64 i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr i64 operator*() const noexcept { return i; }
    constexpr bool operator!=(const iter x) const noexcept { return i != x.i; }
  };
  constexpr ran(const i64 e) noexcept : b(0), e(e) {}
  constexpr ran(const i64 b, const i64 e) noexcept : b(b), e(e) {}
  constexpr iter begin() const noexcept { return iter(b); }
  constexpr iter end() const noexcept { return iter(e); }
  constexpr revran rev() const noexcept { return revran(b, e); }
};

constexpr ran rin(const i64 e) noexcept { return ran(1, e + 1); }
constexpr ran rin(const i64 b, const i64 e) noexcept { return ran(b, e + 1); }

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(all(A));
  i64 sum = A[N - 1] - A[0];
  int L = 0;
  for (int i = 1; i < N - 1 && A[i] <= 0; i++) {
    sum -= A[i];
    L = i;
  }
  for (int i = L + 1; i < N - 1; i++) {
    sum += A[i];
  }

  i64 mi = L + 1;
  i64 pl = N - mi;

  cout << sum << endl;
  vector<pair<i64, i64>> vec;

  i64 pi = N - 1;
  i64 mii = 0;
  i64 sw = 0;
  while (pl > 1) {
    if (sw == 0) {
      i64 LEFT = A[pi];
      i64 RIGHT = -(sum - LEFT);
      vec.push_back({LEFT, RIGHT});
      sum = RIGHT;
      pi--;
      pl--;
      swap(pl, mi);
      sw ^= 1;
    } else {
      i64 LEFT = A[mii];
      i64 RIGHT = -(sum - LEFT);
      vec.push_back({LEFT, RIGHT});
      sum = RIGHT;
      mii++;
      pl--;
      swap(pl, mi);
      sw ^= 1;
    }
  }
  for (int j = 0; j < mi; j++) {
    if (sw == 0) {
      i64 RIGHT = A[mii];
      mii++;
      i64 LEFT = sum + RIGHT;
      vec.push_back({LEFT, RIGHT});
      sum = LEFT;
    } else {
      i64 RIGHT = A[pl];
      pl--;
      i64 LEFT = sum + RIGHT;
      vec.push_back({LEFT, RIGHT});
      sum = LEFT;
    }
  }
  reverse(all(vec));
  for (auto p : vec) {
    cout << p.first << " " << p.second << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()
struct revran {
  const i64 b, e;
  struct iter {
    i64 i;
    constexpr iter(const i64 i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr i64 operator*() const noexcept { return i; }
    constexpr bool operator!=(const iter x) const noexcept { return i != x.i; }
  };
  constexpr revran(const i64 e) noexcept : b(0), e(e) {}
  constexpr revran(const i64 b, const i64 e) noexcept : b(b), e(e) {}
  constexpr iter begin() const noexcept { return iter(e - 1); }
  constexpr iter end() const noexcept { return iter(b - 1); }
};

struct ran {
  const i64 b, e;
  struct iter {
    i64 i;
    constexpr iter(const i64 i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr i64 operator*() const noexcept { return i; }
    constexpr bool operator!=(const iter x) const noexcept { return i != x.i; }
  };
  constexpr ran(const i64 e) noexcept : b(0), e(e) {}
  constexpr ran(const i64 b, const i64 e) noexcept : b(b), e(e) {}
  constexpr iter begin() const noexcept { return iter(b); }
  constexpr iter end() const noexcept { return iter(e); }
  constexpr revran rev() const noexcept { return revran(b, e); }
};

constexpr ran rin(const i64 e) noexcept { return ran(1, e + 1); }
constexpr ran rin(const i64 b, const i64 e) noexcept { return ran(b, e + 1); }

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(all(A));
  i64 sum = A[N - 1] - A[0];
  int L = 0;
  for (int i = 1; i < N - 1 && A[i] <= 0; i++) {
    sum -= A[i];
    L = i;
  }
  for (int i = L + 1; i < N - 1; i++) {
    sum += A[i];
  }

  i64 mi = L + 1;
  i64 pl = N - mi;

  cout << sum << endl;
  vector<pair<i64, i64>> vec;

  i64 pi = N - 1;
  i64 mii = 0;
  i64 sw = 0;
  while (pl > 1) {
    if (sw == 0) {
      i64 LEFT = A[pi];
      i64 RIGHT = -(sum - LEFT);
      vec.push_back({LEFT, RIGHT});
      sum = RIGHT;
      pi--;
      pl--;
      swap(pl, mi);
      sw ^= 1;
    } else {
      i64 LEFT = A[mii];
      i64 RIGHT = -(sum - LEFT);
      vec.push_back({LEFT, RIGHT});
      sum = RIGHT;
      mii++;
      pl--;
      swap(pl, mi);
      sw ^= 1;
    }
  }
  for (int j = 0; j < mi; j++) {
    if (sw == 0) {
      i64 RIGHT = A[mii];
      mii++;
      i64 LEFT = sum + RIGHT;
      vec.push_back({LEFT, RIGHT});
      sum = LEFT;
    } else {
      i64 RIGHT = A[pi];
      pi--;
      i64 LEFT = sum + RIGHT;
      vec.push_back({LEFT, RIGHT});
      sum = LEFT;
    }
  }
  reverse(all(vec));
  for (auto p : vec) {
    cout << p.first << " " << p.second << endl;
  }
}