prithivMLmods/Coder-Stat huggingface dataset:
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  deque<int> neg, pos;
  for (int i = 0; i < n; ++i) {
    auto val = 0;
    cin >> val;
    if (val < 0)
      neg.push_back(val);
    else
      pos.push_back(val);
  }
  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end());

  vector<pair<int, int>> method;
  int negSize, posSize;
  for (negSize = neg.size(), posSize = pos.size();;
       negSize = neg.size(), posSize = pos.size()) {
    if (posSize == 1 && negSize == 0)
      break;
    if (negSize == 0) {
      neg.push_back(pos.front());
      pos.pop_front();
    }
    if (posSize == 0) {
      pos.push_front(neg.back());
      neg.pop_back();
    }
    if (negSize < posSize) {
      auto negVal = neg.back() - pos.front();
      method.push_back(make_pair(neg.back(), pos.front()));
      neg.pop_back();
      pos.pop_front();
      neg.push_back(negVal);
    } else {
      auto posVal = pos.front() - neg.back();
      method.push_back(make_pair(pos.front(), neg.back()));
      pos.pop_front();
      neg.pop_back();
      pos.push_front(posVal);
    }
  }

  cout << pos[0] << endl;
  for (auto m : method) {
    cout << m.first << " " << m.second << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  deque<int> neg, pos;
  for (int i = 0; i < n; ++i) {
    auto val = 0;
    cin >> val;
    if (val < 0)
      neg.push_back(val);
    else
      pos.push_back(val);
  }
  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end());

  vector<pair<int, int>> method;
  int negSize, posSize;
  for (negSize = neg.size(), posSize = pos.size();;
       negSize = neg.size(), posSize = pos.size()) {
    if (posSize + negSize == 1)
      break;
    if (negSize == 0) {
      neg.push_back(pos.front());
      pos.pop_front();
    }
    if (posSize == 0) {
      pos.push_front(neg.back());
      neg.pop_back();
    }
    if (negSize < posSize) {
      auto negVal = neg.back() - pos.front();
      method.push_back(make_pair(neg.back(), pos.front()));
      neg.pop_back();
      pos.pop_front();
      neg.push_back(negVal);
    } else {
      auto posVal = pos.front() - neg.back();
      method.push_back(make_pair(pos.front(), neg.back()));
      pos.pop_front();
      neg.pop_back();
      pos.push_front(posVal);
    }
  }

  cout << pos[0] << endl;
  for (auto m : method) {
    cout << m.first << " " << m.second << endl;
  }
  return 0;
}
Problem p03007 (C++)

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

Int n;
vector<Int> a;
using P = pair<Int, Int>;
vector<P> v;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  Int mi = 1e9, ma = -1e9, imi = -1, ima = -1;
  for (size_t i = 0; i < a.size(); ++i) {
    if (a[i] > ma) {
      ma = a[i];
      ima = i;
    }
  }
  for (size_t i = 0; i < a.size(); ++i) {
    if (a[i] < ma && a[i] < mi) {
      mi = a[i];
      imi = i;
    }
  }
  for (size_t i = 0; i < a.size(); ++i) {
    if (a[i] >= 0 && i != imi && i != ima) {
      v.push_back(P(a[imi], a[i]));
      a[imi] -= a[i];
    }
  }
  for (size_t i = 0; i < a.size(); ++i) {
    if ((a[i] < 0 || i == imi) && i != ima) {
      v.push_back(P(a[ima], a[i]));
      a[ima] -= a[i];
    }
  }
  cout << a[ima] << "\n";
  for (auto i : v)
    cout << i.first << " " << i.second << "\n";
}

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

Int n;
vector<Int> a;
using P = pair<Int, Int>;
vector<P> v;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  Int mi = 1e9, ma = -1e9, imi = -1, ima = -1;
  for (size_t i = 0; i < a.size(); ++i) {
    if (a[i] > ma) {
      ma = a[i];
      ima = i;
    }
  }
  for (size_t i = 0; i < a.size(); ++i) {
    if (i != ima && a[i] < mi) {
      mi = a[i];
      imi = i;
    }
  }
  for (size_t i = 0; i < a.size(); ++i) {
    if (a[i] >= 0 && i != imi && i != ima) {
      v.push_back(P(a[imi], a[i]));
      a[imi] -= a[i];
    }
  }
  for (size_t i = 0; i < a.size(); ++i) {
    if ((a[i] < 0 || i == imi) && i != ima) {
      v.push_back(P(a[ima], a[i]));
      a[ima] -= a[i];
    }
  }
  cout << a[ima] << "\n";
  for (auto i : v)
    cout << i.first << " " << i.second << "\n";
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
#define gcd __gcd
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
const int INF = 1e18, MOD = 1e9 + 7;
signed main() {
  INCANT;
  int n, a, mn, mx;
  deque<int> q;
  vector<pair<int, int>> res;
  cin >> n;
  rep(i, n) {
    cin >> a;
    q.push_back(a);
  }
  sort(all(q));
  mx = q.back();
  mn = q.front();
  q.pop_back();
  q.pop_front();
  while (q.front() < 0) {
    res.push_back({mx, q.front()});
    mx -= q.front();
    q.pop_front();
  }
  while (!q.empty()) {
    res.push_back({mn, q.back()});
    mn -= q.back();
    q.pop_back();
  }
  cout << mx - mn << endl;
  each(p, res) { cout << p.first << ' ' << p.second << endl; }
  cout << mx << ' ' << mn << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
#define gcd __gcd
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, T b) { return (a = max(a, b)) == b; }
template <class T> bool chmin(T &a, T b) { return (a = min(a, b)) == b; }
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
const int INF = 1e18, MOD = 1e9 + 7;
signed main() {
  INCANT;
  int n, a, mn, mx;
  deque<int> q;
  vector<pair<int, int>> res;
  cin >> n;
  rep(i, n) {
    cin >> a;
    q.push_back(a);
  }
  sort(all(q));
  mx = q.back();
  mn = q.front();
  q.pop_back();
  q.pop_front();
  while (q.front() < 0 && !q.empty()) {
    res.push_back({mx, q.front()});
    mx -= q.front();
    q.pop_front();
  }
  while (!q.empty()) {
    res.push_back({mn, q.back()});
    mn -= q.back();
    q.pop_back();
  }
  cout << mx - mn << endl;
  each(p, res) { cout << p.first << ' ' << p.second << endl; }
  cout << mx << ' ' << mn << endl;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> as(n);
  rep(i, 0, n) cin >> as[i];
  sort(as.begin(), as.end());

  ll ans;
  rep(i, 0, n) {
    if (i == 0)
      ans -= as[i];
    else if (i == n - 1)
      ans += as[i];
    else if (as[i] >= 0)
      ans += as[i];
    else
      ans -= as[i];
  }
  cout << ans << endl;

  if (as[n - 1] < 0) {
    ll x = as[n - 1];
    rep(i, 0, n - 1) {
      cout << x << " " << as[i] << endl;
      x -= as[i];
    }
    assert(x == ans);
    return 0;
  }
  if (as[0] > 0) {
    ll x = as[0];
    rep(i, 1, n - 1) {
      cout << x << " " << as[i] << endl;
      x -= as[i];
    }
    cout << as[n - 1] << " " << x << endl;
    x = as[n - 1] - x;
    assert(x == ans);
    return 0;
  }

  ll p = as[n - 1], q = as[0];
  rep(i, 1, n - 1) {
    if (as[i] <= 0) {
      cout << p << " " << as[i] << endl;
      p -= as[i];
    } else {
      cout << q << " " << as[i] << endl;
      q -= as[i];
    }
  }
  cout << p << " " << q << endl;
  p -= q;
  assert(p == ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> as(n);
  rep(i, 0, n) cin >> as[i];
  sort(as.begin(), as.end());

  ll ans = 0;
  rep(i, 0, n) {
    if (i == 0)
      ans -= as[i];
    else if (i == n - 1)
      ans += as[i];
    else if (as[i] >= 0)
      ans += as[i];
    else
      ans -= as[i];
  }
  cout << ans << endl;

  if (as[n - 1] < 0) {
    ll x = as[n - 1];
    rep(i, 0, n - 1) {
      cout << x << " " << as[i] << endl;
      x -= as[i];
    }
    assert(x == ans);
    return 0;
  }
  if (as[0] > 0) {
    ll x = as[0];
    rep(i, 1, n - 1) {
      cout << x << " " << as[i] << endl;
      x -= as[i];
    }
    cout << as[n - 1] << " " << x << endl;
    x = as[n - 1] - x;
    assert(x == ans);
    return 0;
  }

  ll p = as[n - 1], q = as[0];
  rep(i, 1, n - 1) {
    if (as[i] <= 0) {
      cout << p << " " << as[i] << endl;
      p -= as[i];
    } else {
      cout << q << " " << as[i] << endl;
      q -= as[i];
    }
  }
  cout << p << " " << q << endl;
  p -= q;
  assert(p == ans);
  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<int> num(n);
  for (int i = 0; i < n; i++) {
    cin >> num.at(i);
  }
  sort(num.begin(), num.end());
  vector<vector<long long>> ope(n - 1, vector<long long>(2));
  int plus = lower_bound(num.begin(), num.end(), 0) - num.begin();
  long long ans = num.at(0);
  int check = 0;
  if (plus == 0)
    plus++;
  for (int i = plus; i < n - 1; i++) {
    ope.at(check).at(0) = ans;
    ope.at(check).at(1) = num.at(i);
    check++;
    ans -= num.at(i);
  }
  ope.at(check).at(0) = num.at(n - 1);
  ope.at(check).at(1) = ans;
  check++;
  ans = num.at(n - 1) - ans;
  for (int i = 1; i < plus; i++) {
    ope.at(check).at(0) = ans;
    ope.at(check).at(1) = num.at(i);
    check++;
    ans -= num.at(i);
  }
  cout << ans << endl;
  for (vector<long long> a : ope) {
    cout << a.at(0) << " " << a.at(1) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<int> num(n);
  for (int i = 0; i < n; i++) {
    cin >> num.at(i);
  }
  sort(num.begin(), num.end());
  vector<vector<long long>> ope(n - 1, vector<long long>(2));
  int plus = lower_bound(num.begin(), num.end(), 0) - num.begin();
  long long ans = num.at(0);
  int check = 0;
  if (plus > n - 1)
    plus = n - 1;
  if (plus == 0)
    plus++;
  for (int i = plus; i < n - 1; i++) {
    ope.at(check).at(0) = ans;
    ope.at(check).at(1) = num.at(i);
    check++;
    ans -= num.at(i);
  }
  ope.at(check).at(0) = num.at(n - 1);
  ope.at(check).at(1) = ans;
  check++;
  ans = num.at(n - 1) - ans;
  for (int i = 1; i < plus; i++) {
    ope.at(check).at(0) = ans;
    ope.at(check).at(1) = num.at(i);
    check++;
    ans -= num.at(i);
  }
  cout << ans << endl;
  for (vector<long long> a : ope) {
    cout << a.at(0) << " " << a.at(1) << endl;
  }
  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int ans = 0;
  vector<P> astr;
  priority_queue<int> mq;
  priority_queue<int, vector<int>, greater<int>> pq;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < 0)
      mq.push(a);
    else
      pq.push(a);
  }

  if (mq.empty()) {
    int a, b;
    a = pq.top();
    pq.pop();
    b = pq.top();
    pq.pop();
    astr.push_back(P(a, b));
    mq.push(a - b);
  }

  if (pq.empty()) {
    int a, b;
    a = mq.top();
    mq.pop();
    b = mq.top();
    mq.pop();
    astr.push_back(P(a, b));
    pq.push(a - b);
  }

  ans = mq.top();
  mq.pop();
  int last = pq.top();
  pq.pop();
  while (!pq.empty()) {
    astr.push_back(P(ans, pq.top()));
    ans -= pq.top();
    pq.pop();
  }
  astr.push_back(P(last, ans));
  ans = last - ans;
  while (!mq.empty()) {
    astr.push_back(P(ans, mq.top()));
    ans -= mq.top();
    mq.pop();
  }

  cout << ans << '\n';
  for (auto a : astr)
    cout << a.first << " " << a.second << '\n';
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int ans = 0;
  vector<P> astr;
  priority_queue<int> mq;
  priority_queue<int, vector<int>, greater<int>> pq;

  if (n == 2) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    cout << b - a << endl;
    cout << b << " " << a << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < 0)
      mq.push(a);
    else
      pq.push(a);
  }

  if (mq.empty()) {
    int a, b;
    a = pq.top();
    pq.pop();
    b = pq.top();
    pq.pop();
    astr.push_back(P(a, b));
    mq.push(a - b);
  }

  if (pq.empty()) {
    int a, b;
    a = mq.top();
    mq.pop();
    b = mq.top();
    mq.pop();
    astr.push_back(P(a, b));
    pq.push(a - b);
  }

  ans = mq.top();
  mq.pop();
  int last = pq.top();
  pq.pop();
  while (!pq.empty()) {
    astr.push_back(P(ans, pq.top()));
    ans -= pq.top();
    pq.pop();
  }
  astr.push_back(P(last, ans));
  ans = last - ans;
  while (!mq.empty()) {
    astr.push_back(P(ans, mq.top()));
    ans -= mq.top();
    mq.pop();
  }

  cout << ans << '\n';
  for (auto a : astr)
    cout << a.first << " " << a.second << '\n';
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> plus, minus;
  rep(i, n) {
    if (a[i] >= 0)
      plus.emplace_back(a[i]);
    else
      minus.emplace_back(a[i]);
  }
  sort(plus.begin(), plus.end(), greater<int>());
  sort(minus.begin(), minus.end());

  if (minus.empty())
    minus.emplace_back(plus.back()), plus.pop_back();
  if (plus.empty())
    plus.emplace_back(minus.back()), minus.pop_back();

  int cur = minus[0];
  vector<P> sousa;

  for (int i = 0; i < (int)(plus.size() - 1); i++) {
    sousa.emplace_back(P(cur, plus[i]));
    cur = cur - plus[i];
  }
  sousa.emplace_back(P(plus.back(), cur));
  cur = plus.back() - cur;

  for (int i = 1; i < minus.size(); i++) {
    sousa.emplace_back(P(cur, minus[i]));
    cur = cur - plus[i];
  }

  cout << cur << endl;
  for (auto x : sousa)
    cout << x.first << " " << x.second << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> plus, minus;
  rep(i, n) {
    if (a[i] >= 0)
      plus.emplace_back(a[i]);
    else
      minus.emplace_back(a[i]);
  }
  sort(plus.begin(), plus.end(), greater<int>());
  sort(minus.begin(), minus.end());

  if (minus.empty())
    minus.emplace_back(plus.back()), plus.pop_back();
  if (plus.empty())
    plus.emplace_back(minus.back()), minus.pop_back();

  int cur = minus[0];
  vector<P> sousa;

  for (int i = 0; i < (int)(plus.size() - 1); i++) {
    sousa.emplace_back(P(cur, plus[i]));
    cur = cur - plus[i];
  }
  sousa.emplace_back(P(plus.back(), cur));
  cur = plus.back() - cur;

  for (int i = 1; i < minus.size(); i++) {
    sousa.emplace_back(P(cur, minus[i]));
    cur = cur - minus[i];
  }

  cout << cur << endl;
  for (auto x : sousa)
    cout << x.first << " " << x.second << "\n";

  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

int N, A[100000];
vector<pii> result;

int rec(queue<int> &p, queue<int> &n) {
  if (p.size() == 1) {
    int ret = A[p.front()];
    while (!n.empty()) {
      int v = n.front();
      n.pop();
      result.push_back({ret, A[v]});
      ret -= A[v];
    }
    return ret;
  }

  int h = p.front();
  p.pop();
  int r = rec(n, p);
  result.push_back({A[h], r});
  return A[h] - r;
}

int main() {
  bool all_positive = true, all_negative;
  cin >> N;
  REP(i, N) {
    cin >> A[i];
    all_positive &= A[i] > 0;
    all_negative &= A[i] <= 0;
  }
  sort(A, A + N);

  int s = 0, e = N;
  queue<int> p, n;
  if (all_positive) {
    n.push(0);
    s = 1;
  }
  if (all_negative) {
    p.push(N - 1);
    e = N - 1;
  }
  FOR(i, s, e) {
    if (A[i] > 0)
      p.push(i);
    else
      n.push(i);
  }

  cout << rec(p, n) << endl;
  for (auto p : result)
    cout << p.first << " " << p.second << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

int N, A[100000];
vector<pii> result;

int rec(queue<int> &p, queue<int> &n) {
  if (p.size() == 1) {
    int ret = A[p.front()];
    while (!n.empty()) {
      int v = n.front();
      n.pop();
      result.push_back({ret, A[v]});
      ret -= A[v];
    }
    return ret;
  }

  int h = p.front();
  p.pop();
  int r = rec(n, p);
  result.push_back({A[h], r});
  return A[h] - r;
}

int main() {
  bool all_positive = true, all_negative = true;
  cin >> N;
  REP(i, N) {
    cin >> A[i];
    all_positive &= A[i] > 0;
    all_negative &= A[i] <= 0;
  }
  sort(A, A + N);

  int s = 0, e = N;
  queue<int> p, n;
  if (all_positive) {
    n.push(0);
    s = 1;
  }
  if (all_negative) {
    p.push(N - 1);
    e = N - 1;
  }
  FOR(i, s, e) {
    if (A[i] > 0)
      p.push(i);
    else
      n.push(i);
  }

  cout << rec(p, n) << endl;
  for (auto p : result)
    cout << p.first << " " << p.second << endl;
  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int n;
vector<int> lz, mz;
vector<pair<int, int>> mv;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < 0)
      lz.push_back(a);
    else
      mz.push_back(a);
  }
  if (lz.size() == 0) {
    sort(mz.begin(), mz.end());
    while (mz.size() > 2) {
      mv.push_back({mz[0], mz.back()});
      mz[0] -= mz.back();
      mz.pop_back();
    }
    mv.push_back({mz.back(), mz[0]});
  } else if (mz.size() == 0) {
    sort(lz.begin(), lz.end());
    reverse(lz.begin(), lz.end());
    while (lz.size() > 2) {
      mv.push_back({lz[0], lz.back()});
      lz[0] -= mz.back();
      lz.pop_back();
    }
    mv.push_back({lz[0], lz.back()});
  } else {
    for (int i = 1; i < mz.size(); i++)
      mv.push_back({lz[0], mz[i]}), lz[0] -= mz[i];
    for (int i = 1; i < lz.size(); i++)
      mv.push_back({mz[0], lz[i]}), mz[0] -= lz[i];
    mv.push_back({mz[0], lz[0]});
  }
  cout << mv.back().first - mv.back().second << '\n';
  for (int i = 0; i < mv.size(); i++)
    cout << mv[i].first << ' ' << mv[i].second << '\n';
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int n;
vector<int> lz, mz;
vector<pair<int, int>> mv;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < 0)
      lz.push_back(a);
    else
      mz.push_back(a);
  }
  if (lz.size() == 0) {
    sort(mz.begin(), mz.end());
    while (mz.size() > 2) {
      mv.push_back({mz[0], mz.back()});
      mz[0] -= mz.back();
      mz.pop_back();
    }
    mv.push_back({mz.back(), mz[0]});
  } else if (mz.size() == 0) {
    sort(lz.begin(), lz.end());
    reverse(lz.begin(), lz.end());
    while (lz.size() > 2) {
      mv.push_back({lz[0], lz.back()});
      lz[0] -= lz.back();
      lz.pop_back();
    }
    mv.push_back({lz[0], lz.back()});
  } else {
    for (int i = 1; i < mz.size(); i++)
      mv.push_back({lz[0], mz[i]}), lz[0] -= mz[i];
    for (int i = 1; i < lz.size(); i++)
      mv.push_back({mz[0], lz[i]}), mz[0] -= lz[i];
    mv.push_back({mz[0], lz[0]});
  }
  cout << mv.back().first - mv.back().second << '\n';
  for (int i = 0; i < mv.size(); i++)
    cout << mv[i].first << ' ' << mv[i].second << '\n';
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ff first
#define ss second
#define N 100005

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  int a[n];
  int sum = 0;
  vector<int> pos, neg;
  pos.push_back(0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += abs(a[i]);
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end(), greater<int>());
  int ans;
  if (neg.size() > 0) {
    pos[0] = neg[0];
    neg.erase(neg.begin());
    if (pos.size() > 1)
      ans = sum;
    else
      ans = sum + 2 * pos[0];
  } else {
    pos.erase(pos.begin());
    ans = sum - 2 * pos[0];
  }

  int n1 = pos.size();
  cout << ans << "\n";
  int in = 0;
  int last;

  last = pos[0];
  for (int i = 0; i < n1 - 2; ++i) {
    cout << last << " " << pos[i + 1] << "\n";
    last = last - pos[i + 1];
  }
  if (n1 != 1) {
    cout << pos[n1 - 1] << " " << last << '\n';
    last = pos[n - 1] - last;
  }

  int n2 = neg.size();
  for (int i = 0; i < n2; ++i) {
    cout << last << " " << neg[i] << "\n";
    last = last - neg[i];
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ff first
#define ss second
#define N 100005

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  int a[n];
  int sum = 0;
  vector<int> pos, neg;
  pos.push_back(0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += abs(a[i]);
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(a[i]);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end(), greater<int>());
  int ans;
  if (neg.size() > 0) {
    pos[0] = neg[0];
    neg.erase(neg.begin());
    if (pos.size() > 1)
      ans = sum;
    else
      ans = sum + 2 * pos[0];
  } else {
    pos.erase(pos.begin());
    ans = sum - 2 * pos[0];
  }

  int n1 = pos.size();
  cout << ans << "\n";
  int in = 0;
  int last;

  last = pos[0];
  for (int i = 0; i < n1 - 2; ++i) {
    cout << last << " " << pos[i + 1] << "\n";
    last = last - pos[i + 1];
  }
  if (n1 != 1) {
    cout << pos[n1 - 1] << " " << last << '\n';
    last = pos[n1 - 1] - last;
  }

  int n2 = neg.size();
  for (int i = 0; i < n2; ++i) {
    cout << last << " " << neg[i] << "\n";
    last = last - neg[i];
  }
  return 0;
}
Problem p03007 (C++)

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

int main() {
  ll n, count = 0;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());

  if (n == 2) {
    cout << a[1] - a[0] << endl;
    cout << a[1] << " " << a[0] << endl;
    return 0;
  }

  vector<ll> x(n - 1), y(n - 1);
  queue<ll> plus;
  queue<ll> minus;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0)
      plus.push(a[i]);
    else
      minus.push(a[i]);
  }

  while (plus.size() > 1) {
    ll P = plus.front();
    ll M = minus.front();
    plus.pop();
    minus.pop();
    minus.push(M - P);
    x[count] = M;
    y[count] = P;
    count++;
  }

  while (minus.size() > 0) {
    ll P = plus.front();
    ll M = minus.front();
    plus.pop();
    minus.pop();
    plus.push(P - M);
    x[count] = P;
    y[count] = M;
    count++;
  }

  ll ans = plus.front();
  cout << ans << endl;

  for (int i = 0; i < n - 1; i++)
    cout << x[i] << " " << y[i] << endl;

  return 0;
}

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

int main() {
  ll n, count = 0;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());

  if (n == 2) {
    cout << a[1] - a[0] << endl;
    cout << a[1] << " " << a[0] << endl;
    return 0;
  }

  vector<ll> x(n - 1), y(n - 1);
  queue<ll> plus;
  queue<ll> minus;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0)
      plus.push(a[i]);
    else
      minus.push(a[i]);
  }

  if (plus.size() == 0) {
    for (int i = 0; i < n - 2; i++) {
      ll p = minus.front();
      minus.pop();
      minus.push(p);
    }
    ll minus1 = minus.front();
    minus.pop();
    ll minus2 = minus.front();
    minus.pop();
    plus.push(minus2 - minus1);
    x[count] = minus2;
    y[count] = minus1;
    count++;
  }

  if (minus.size() == 0) {
    ll plus1 = plus.front();
    plus.pop();
    ll plus2 = plus.front();
    plus.pop();
    minus.push(plus1 - plus2);
    x[count] = plus1;
    y[count] = plus2;
    count++;
  }

  while (plus.size() > 1) {
    ll P = plus.front();
    ll M = minus.front();
    plus.pop();
    minus.pop();
    minus.push(M - P);
    x[count] = M;
    y[count] = P;
    count++;
  }

  while (minus.size() > 0) {
    ll P = plus.front();
    ll M = minus.front();
    plus.pop();
    minus.pop();
    plus.push(P - M);
    x[count] = P;
    y[count] = M;
    count++;
  }

  ll ans = plus.front();
  cout << ans << endl;

  for (int i = 0; i < n - 1; i++)
    cout << x[i] << " " << y[i] << endl;

  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;

const int MAX_N = 1e5;
int n;
vector<int> a;
vector<P> ans;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d", &b);
    a.emplace_back(b);
  }
  sort(a.begin(), a.end());

  vector<int>::iterator c = upper_bound(a.begin(), a.end(), 0);
  if (c == a.begin())
    c++;
  vector<int>::iterator b = prev(c);
  ans.emplace_back(*c, *b);
  int d = *c - *b;
  a.erase(c);
  a.erase(b);
  while (!a.empty()) {
    if (a.back() > 0) {
      int e = ans.back().first;
      ans.back().first = ans.back().second;
      ans.back().second = e;
      d = -d;
    }
    if (a.back() >= d) {
      ans.emplace_back(a.back(), d);
      d = a.back() - d;
    } else {
      ans.emplace_back(d, a.back());
      d = d - a.back();
    }
    a.pop_back();
  }

  printf("%d\n", d);
  for (int i = 0; i < n - 1; i++) {
    printf("%d %d\n", ans[i].first, ans[i].second);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;

const int MAX_N = 1e5;
int n;
vector<int> a;
vector<P> ans;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int b;
    scanf("%d", &b);
    a.emplace_back(b);
  }
  sort(a.begin(), a.end());

  vector<int>::iterator c = upper_bound(a.begin(), a.end(), 0);
  if (c == a.begin())
    c++;
  if (c == a.end())
    c--;
  vector<int>::iterator b = prev(c);
  ans.emplace_back(*c, *b);
  int d = *c - *b;
  a.erase(c);
  a.erase(b);
  while (!a.empty()) {
    if (a.back() > 0) {
      int e = ans.back().first;
      ans.back().first = ans.back().second;
      ans.back().second = e;
      d = -d;
    }
    if (a.back() >= d) {
      ans.emplace_back(a.back(), d);
      d = a.back() - d;
    } else {
      ans.emplace_back(d, a.back());
      d = d - a.back();
    }
    a.pop_back();
  }

  printf("%d\n", d);
  for (int i = 0; i < n - 1; i++) {
    printf("%d %d\n", ans[i].first, ans[i].second);
  }
  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define F first
#define S second

using namespace std;
typedef pair<int, int> P;

const int MOD = 1000000007;
int INF = 100100100100100;

int a[100010];
int neg;
int ans = 0;
vector<P> res;
signed main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  sort(a, a + n);

  if (a[0] > 0) {
    for (int i = 1; i <= n - 2; i++) {
      res.pb(P(a[0], a[i]));
      a[0] -= a[i];
    }
    res.pb(P(a[n - 1], a[0]));
    ans = a[n - 1] - a[0];
  }

  else if (a[n - 1] < 0) {
    for (int i = n - 2; i >= 0; i++) {
      res.pb(P(a[n - 1], a[i]));
      a[n - 1] -= a[i];
    }
    ans = a[n - 1];
  }

  else {
    int ima = n - 2;
    while (a[ima] > 0) {
      res.pb(P(a[0], a[ima]));
      a[0] -= a[ima];
      ima--;
    }
    rep(i, ima + 1) {
      res.pb(P(a[n - 1], a[i]));
      a[n - 1] -= a[i];
    }
    ans = a[n - 1];
  }
  cout << ans << endl;
  rep(i, n - 1) { cout << res[i].F << " " << res[i].S << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define F first
#define S second

using namespace std;
typedef pair<int, int> P;

const int MOD = 1000000007;
int INF = 100100100100100;

int a[100010];
int neg;
int ans = 0;
vector<P> res;
signed main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  sort(a, a + n);

  if (a[0] > 0) {
    for (int i = 1; i <= n - 2; i++) {
      res.pb(P(a[0], a[i]));
      a[0] -= a[i];
    }
    res.pb(P(a[n - 1], a[0]));
    ans = a[n - 1] - a[0];
  }

  else if (a[n - 1] < 0) {
    for (int i = n - 2; i >= 0; i--) {
      res.pb(P(a[n - 1], a[i]));
      a[n - 1] -= a[i];
    }
    ans = a[n - 1];
  }

  else {
    int ima = n - 2;
    while (a[ima] > 0) {
      res.pb(P(a[0], a[ima]));
      a[0] -= a[ima];
      ima--;
    }
    rep(i, ima + 1) {
      res.pb(P(a[n - 1], a[i]));
      a[n - 1] -= a[i];
    }
    ans = a[n - 1];
  }
  cout << ans << endl;
  rep(i, n - 1) { cout << res[i].F << " " << res[i].S << endl; }
  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = (int)1e9 + 7;
int INF = (int)2e18;

signed main() {
  int N;
  cin >> N;
  deque<int> negative, positive;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    if (temp <= 0)
      negative.push_back(temp);
    else
      positive.push_back(temp);
  }
  sort(negative.begin(), negative.end());
  sort(positive.begin(), positive.end());
  vector<pair<int, int>> ans;
  if (negative.empty()) {
    int big = positive.back();
    int small = positive.front();
    positive.pop_back();
    positive.pop_front();
    ans.emplace_back(small, big);
    positive.push_back(small - big);
  }
  while (positive.size() != 1) {
    int big = positive.back();
    int small = negative.front();
    positive.pop_back();
    negative.pop_front();
    ans.emplace_back(small, big);
    negative.push_back(small - big);
  }
  while (!negative.empty()) {
    int big = positive.back();
    int small = negative.front();
    positive.pop_back();
    negative.pop_front();
    ans.emplace_back(big, small);
    positive.push_back(big - small);
  }
  cout << positive.front() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = (int)1e9 + 7;
int INF = (int)2e18;

signed main() {
  int N;
  cin >> N;
  deque<int> negative, positive;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    if (temp <= 0)
      negative.push_back(temp);
    else
      positive.push_back(temp);
  }
  sort(negative.begin(), negative.end());
  sort(positive.begin(), positive.end());
  vector<pair<int, int>> ans;
  if (negative.empty()) {
    int big = positive.back();
    int small = positive.front();
    positive.pop_back();
    positive.pop_front();
    ans.emplace_back(small, big);
    negative.push_back(small - big);
  } else if (positive.empty()) {
    int big = negative.front();
    int small = negative.back();
    negative.pop_back();
    negative.pop_front();
    ans.emplace_back(small, big);
    positive.push_back(small - big);
  }
  while (positive.size() != 1) {
    int big = positive.back();
    int small = negative.front();
    positive.pop_back();
    negative.pop_front();
    ans.emplace_back(small, big);
    negative.push_back(small - big);
  }
  while (!negative.empty()) {
    int big = positive.back();
    int small = negative.front();
    positive.pop_back();
    negative.pop_front();
    ans.emplace_back(big, small);
    positive.push_back(big - small);
  }
  cout << positive.front() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
}
Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  sort(A.begin(), A.end(), greater<long long>());
  vector<long long> sum(A);
  for (int i = 1; i < N; ++i)
    sum[i] += sum[i - 1];

  long long M = 0;
  int idx;
  // 正になる数を探索
  for (int i = 0; i < N - 1; ++i) {
    if (M < 2 * sum[i] - sum[N - 1]) {
      idx = i;
      M = 2 * sum[i] - sum[N - 1];
    }
  }
  cout << M << endl;

  int count = 0;
  while (count < N) {
    if (count < idx) {
      cout << A[idx + 1] << " " << A[count] << endl;
      A[idx + 1] -= A[count++];
    } else if (count == idx) {
      cout << A[idx] << " " << A[idx + 1] << endl;
      A[idx] -= A[idx + 1];
      count += 2;
    } else {
      cout << A[idx] << " " << A[count] << endl;
      A[idx] -= A[count++];
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  sort(A.begin(), A.end(), greater<long long>());
  vector<long long> sum(A);
  for (int i = 1; i < N; ++i)
    sum[i] += sum[i - 1];

  long long M = 0;
  int idx = 0;
  // 正になる数を探索
  for (int i = 0; i < N - 1; ++i) {
    if (M < 2 * sum[i] - sum[N - 1]) {
      idx = i;
      M = 2 * sum[i] - sum[N - 1];
    }
  }
  cout << M << endl;

  int count = 0;
  while (count < N) {
    if (count < idx) {
      cout << A[idx + 1] << " " << A[count] << endl;
      A[idx + 1] -= A[count++];
    } else if (count == idx) {
      cout << A[idx] << " " << A[idx + 1] << endl;
      A[idx] -= A[idx + 1];
      count += 2;
    } else {
      cout << A[idx] << " " << A[count] << endl;
      A[idx] -= A[count++];
    }
  }

  return 0;
}
Problem p03007 (C++)

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

int main() {
  int n;
  cin >> n;

  vector<long long> a;
  vector<long long> b;
  vector<pair<long long, long long>> anss;

  for (int i = 0; i < n; i++) {
    long long ab;
    cin >> ab;
    if (ab >= 0)
      a.push_back(ab);
    else
      b.push_back(ab);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  if (a.size() == 0) {
    a.push_back(b[b.size() - 1] - b[0]);
    anss.push_back(make_pair(b[b.size() - 1], b[0]));
    b.erase(b.begin());
    b.erase(b.end() - 1);
  }
  if (b.size() == 0) {
    b.push_back(a[0] - a[a.size() - 1]);
    anss.push_back(make_pair(a[0], a[a.size() - 1]));
    a.erase(a.begin());
    a.erase(a.end() - 1);
  }

  for (int i = 0; i < a.size() - 1; i++) {
    anss.push_back(make_pair(b[0], a[i]));
    b[0] -= a[i];
  }

  int maxa = a[a.size() - 1];
  int lenb = b.size();
  for (int i = 0; i < lenb; i++) {
    anss.push_back(make_pair(maxa, b[i]));
    maxa -= b[i];
  }

  cout << maxa << endl;
  for (auto p : anss) {
    cout << p.first << " " << p.second << endl;
  }
}

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

int main() {
  int n;
  cin >> n;

  vector<long long> a;
  vector<long long> b;
  vector<pair<long long, long long>> anss;

  if (n == 2) {
    long long x, y;
    cin >> x >> y;
    cout << max(x, y) - min(x, y) << endl;
    cout << max(x, y) << " " << min(x, y) << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    long long ab;
    cin >> ab;
    if (ab >= 0)
      a.push_back(ab);
    else
      b.push_back(ab);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  if (a.size() == 0) {
    a.push_back(b[b.size() - 1] - b[0]);
    anss.push_back(make_pair(b[b.size() - 1], b[0]));
    b.erase(b.begin());
    b.erase(b.end() - 1);
  }
  if (b.size() == 0) {
    b.push_back(a[0] - a[a.size() - 1]);
    anss.push_back(make_pair(a[0], a[a.size() - 1]));
    a.erase(a.begin());
    a.erase(a.end() - 1);
  }

  for (int i = 0; i < a.size() - 1; i++) {
    anss.push_back(make_pair(b[0], a[i]));
    b[0] -= a[i];
  }

  int maxa = a[a.size() - 1];
  int lenb = b.size();
  for (int i = 0; i < lenb; i++) {
    anss.push_back(make_pair(maxa, b[i]));
    maxa -= b[i];
  }

  cout << maxa << endl;
  for (auto p : anss) {
    cout << p.first << " " << p.second << endl;
  }
}
Problem p03007 (C++)

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

typedef pair<int, int> PII;

int main() {
  int N;
  cin >> N;
  vector<int> data;
  int pl = 0;
  int mn = 0;
  vector<int> pls, mns;

  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a > 0)
      pls.push_back(a);
    if (a < 0)
      mns.push_back(a);
  }
  sort(mns.begin(), mns.end());
  sort(pls.begin(), pls.end());
  reverse(pls.begin(), pls.end());
  if (pls.size() == 0) {
    int k = mns.back();
    mns.pop_back();
    pls.push_back(k);
  }
  if (mns.size() == 0) {
    int k = pls.back();
    pls.pop_back();
    mns.push_back(k);
  }

  vector<PII> output;

  while (pls.size() >= 2) {
    int k = mns.back();
    mns.pop_back();
    int j = pls.back();
    pls.pop_back();
    output.push_back(PII(k, j));
    k -= j;
    mns.push_back(k);
  }
  while (mns.size() >= 1) {
    int k = pls.back();
    pls.pop_back();
    int j = mns.back();
    mns.pop_back();
    output.push_back(PII(k, j));
    k -= j;
    pls.push_back(k);
  }
  cout << pls[0] << endl;
  for (auto elem : output) {
    int a = elem.first;
    int b = elem.second;
    cout << a << " " << b << endl;
  }

  return 0;
}

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

typedef pair<int, int> PII;

int main() {
  int N;
  cin >> N;
  vector<int> data;
  int pl = 0;
  int mn = 0;
  vector<int> pls, mns;

  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (a > 0)
      pls.push_back(a);
    else
      mns.push_back(a);
  }
  sort(mns.begin(), mns.end());
  sort(pls.begin(), pls.end());
  reverse(pls.begin(), pls.end());
  if (pls.size() == 0) {
    int k = mns.back();
    mns.pop_back();
    pls.push_back(k);
  }
  if (mns.size() == 0) {
    int k = pls.back();
    pls.pop_back();
    mns.push_back(k);
  }

  vector<PII> output;

  while (pls.size() >= 2) {
    int k = mns.back();
    mns.pop_back();
    int j = pls.back();
    pls.pop_back();
    output.push_back(PII(k, j));
    k -= j;
    mns.push_back(k);
  }
  while (mns.size() >= 1) {
    int k = pls.back();
    pls.pop_back();
    int j = mns.back();
    mns.pop_back();
    output.push_back(PII(k, j));
    k -= j;
    pls.push_back(k);
  }
  cout << pls[0] << endl;
  for (auto elem : output) {
    int a = elem.first;
    int b = elem.second;
    cout << a << " " << b << endl;
  }

  return 0;
}
Problem p03007 (C++)

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  int c = lower_bound(A.begin(), A.end(), 0) - A.begin();
  //
  vector<int> vec1(c);
  vector<int> vec2(N - c);

  vector<pair<int, int>> B;
  for (int i = 0; i < c; i++) {
    vec1[i] = A[i];
  }
  for (int j = c; j < N; j++) {
    vec2[j - c] = A[j];
  }

  if ((int)vec2.size() >= 2) {
    for (int i = 0; i <= N - 2 - c; i++) {

      B.push_back(make_pair(vec1[0], vec2[i]));
      vec1[0] -= vec2[i];
    }
  }

  if ((int)vec1.size() >= 2) {
    for (int j = 1; j <= c - 1; j++) {
      B.push_back(make_pair(vec2[N - 1 - c], vec1[j]));
      vec2[N - 1 - c] -= vec1[j];
    }
  }

  B.push_back(make_pair(vec2[N - 1 - c], vec1[0]));

  int M = vec2[N - 1 - c] - vec1[0];

  cout << M << endl;

  for (int i = 0; i < (int)B.size(); i++) {
    cout << B[i].first << " " << B[i].second << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  int c = lower_bound(A.begin(), A.end(), 0) - A.begin();
  //
  if (c <= 0) {
    // all of components are positive
    c = 1;
  } else if (c >= N) {
    // all of components are negative

    c = N - 1;
  }

  vector<int> vec1(c);
  vector<int> vec2(N - c);

  vector<pair<int, int>> B;
  for (int i = 0; i < c; i++) {
    vec1[i] = A[i];
  }
  for (int j = c; j < N; j++) {
    vec2[j - c] = A[j];
  }

  if ((int)vec2.size() >= 2) {
    for (int i = 0; i <= N - 2 - c; i++) {

      B.push_back(make_pair(vec1[0], vec2[i]));
      vec1[0] -= vec2[i];
    }
  }

  if ((int)vec1.size() >= 2) {
    for (int j = 1; j <= c - 1; j++) {
      B.push_back(make_pair(vec2[N - 1 - c], vec1[j]));
      vec2[N - 1 - c] -= vec1[j];
    }
  }

  B.push_back(make_pair(vec2[N - 1 - c], vec1[0]));

  int M = vec2[N - 1 - c] - vec1[0];

  cout << M << endl;

  for (int i = 0; i < (int)B.size(); i++) {
    cout << B[i].first << " " << B[i].second << endl;
  }

  return 0;
}
Problem p03007 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forn1(i, n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

template <typename T> T id(T b) { return b; };
template <class It> bool all(It f, It l) { return std::all_of(f, l, id<bool>); }
template <class It> bool any(It f, It l) { return std::any_of(f, l, id<bool>); }
template <typename T> void chmax(T &x, T y) {
  if (x < y)
    x = y;
}
template <typename T> void chmin(T &x, T y) {
  if (x > y)
    x = y;
}

const int MAX_N = 100000;
int n;
int a[MAX_N];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif

  cin >> n;
  forn(i, n) cin >> a[i];

  sort(a, a + n);

  int res = -a[0] + a[n - 1];
  for (int i = 1; i < n - 1; i++)
    res += abs(a[i]);

  cout << res << endl;

  int i = n - 2;
  int prev = a[0];
  while (i > 0 && a[i] >= 0) {
    cout << prev << " " << a[i] << "\n";
    prev -= a[i];
    i--;
  }

  cout << a[n - 1] << " " << prev << "\n";
  prev = a[n - 1] - prev;

  while (i > 0) {
    cout << prev << " " << a[i] << "\n";
    prev -= a[i];
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forn1(i, n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

template <typename T> T id(T b) { return b; };
template <class It> bool all(It f, It l) { return std::all_of(f, l, id<bool>); }
template <class It> bool any(It f, It l) { return std::any_of(f, l, id<bool>); }
template <typename T> void chmax(T &x, T y) {
  if (x < y)
    x = y;
}
template <typename T> void chmin(T &x, T y) {
  if (x > y)
    x = y;
}

const int MAX_N = 100000;
int n;
int a[MAX_N];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif

  cin >> n;
  forn(i, n) cin >> a[i];

  sort(a, a + n);

  int res = -a[0] + a[n - 1];
  for (int i = 1; i < n - 1; i++)
    res += abs(a[i]);

  cout << res << endl;

  int i = n - 2;
  int prev = a[0];
  while (i > 0 && a[i] >= 0) {
    cout << prev << " " << a[i] << "\n";
    prev -= a[i];
    i--;
  }

  cout << a[n - 1] << " " << prev << "\n";
  prev = a[n - 1] - prev;

  while (i > 0) {
    cout << prev << " " << a[i] << "\n";
    prev -= a[i];
    i--;
  }

  return 0;
}