Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std; // Here's a small hint that helped motivate my solution.
typedef long long int ll;
int main() {
  // Suppose we have three terms x, y, and z. After subtracting z from y, we get
  // y-z, and after subtracting
  // that from x, we get x-y+z. As you can see, the z is being added now, since
  // it was subtracted twice. In general, subtracting a number an even number of
  // times is equivalent to adding that number. Can we take advantage of this
  // somehow?
  ll no;
  cin >> no;
  ll a;
  vector<ll> p, n;
  for (int i = 0; i < no; i++) {
    cin >> a;
    if (a > 0)
      p.push_back(a);
    if (a < 0)
      n.push_back(a);
  }
  ll b;
  int flag = 0;
  if (p.size() == 0) {
    sort(n.rbegin(), n.rend());
    flag = 1;
    a = n[0];
    b = n[1];
    ll val = n[0] - n[1];
    p.push_back(val);
    n.erase(n.begin(), n.begin() + 2);
  }
  if (n.size() == 0) {
    if (p.size() == 2) {
      sort(p.begin(), p.end());
      cout << p[1] - p[0] << "\n" << p[1] << " " << p[0] << endl;
      return 0;
    }
    if (p.size() > 2) {
      sort(p.begin(), p.end());
      a = p[0];
      b = p[1];
      ll val = p[0] - p[1];
      n.push_back(val);
      p.erase(p.begin(), p.begin() + 2);
      flag = 1;
    }
  }
  // 	for(int i=0;i<p.size();i++) cout<<p[i];
  // 	cout<<endl;
  // 	for(int i=0;i<n.size();i++) cout<<n[i];
  // 	cout<<endl;
  ll sum = 0;
  ll s = 0;
  for (int i = 0; i < p.size(); i++)
    sum += p[i];
  for (int i = 0; i < n.size(); i++)
    s += n[i];
  s *= -1;
  sum += s;
  cout << sum << endl;
  if (flag)
    cout << a << " " << b << endl;
  if (p.size() != 0 && n.size() != 0) {

    ll val = n[0];
    for (int i = 0; i < p.size() - 1; i++) {
      cout << val << " " << p[i] << endl;
      val = val - p[i];
    }
    cout << p[p.size() - 1] << " " << val << endl;
    val = p[p.size() - 1] - val;
    for (int i = 1; i < n.size(); i++) {
      cout << val << " " << n[i] << endl;
      val = val - n[i];
    }
    return 0;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std; // Here's a small hint that helped motivate my solution.
typedef long long int ll;
int main() {
  // Suppose we have three terms x, y, and z. After subtracting z from y, we get
  // y-z, and after subtracting
  // that from x, we get x-y+z. As you can see, the z is being added now, since
  // it was subtracted twice. In general, subtracting a number an even number of
  // times is equivalent to adding that number. Can we take advantage of this
  // somehow?
  ll no;
  cin >> no;
  ll a;
  vector<ll> p, n;
  for (int i = 0; i < no; i++) {
    cin >> a;
    if (a >= 0)
      p.push_back(a);
    if (a < 0)
      n.push_back(a);
  }
  ll b;
  int flag = 0;
  if (p.size() == 0) {
    sort(n.rbegin(), n.rend());
    flag = 1;
    a = n[0];
    b = n[1];
    ll val = n[0] - n[1];
    p.push_back(val);
    n.erase(n.begin(), n.begin() + 2);
  }
  if (n.size() == 0) {
    if (p.size() == 2) {
      sort(p.begin(), p.end());
      cout << p[1] - p[0] << "\n" << p[1] << " " << p[0] << endl;
      return 0;
    }
    if (p.size() > 2) {
      sort(p.begin(), p.end());
      a = p[0];
      b = p[1];
      ll val = p[0] - p[1];
      n.push_back(val);
      p.erase(p.begin(), p.begin() + 2);
      flag = 1;
    }
  }
  // 	for(int i=0;i<p.size();i++) cout<<p[i];
  // 	cout<<endl;
  // 	for(int i=0;i<n.size();i++) cout<<n[i];
  // 	cout<<endl;
  ll sum = 0;
  ll s = 0;
  for (int i = 0; i < p.size(); i++)
    sum += p[i];
  for (int i = 0; i < n.size(); i++)
    s += n[i];
  s *= -1;
  sum += s;
  cout << sum << endl;
  if (flag)
    cout << a << " " << b << endl;
  if (p.size() != 0 && n.size() != 0) {

    ll val = n[0];
    for (int i = 0; i < p.size() - 1; i++) {
      cout << val << " " << p[i] << endl;
      val = val - p[i];
    }
    cout << p[p.size() - 1] << " " << val << endl;
    val = p[p.size() - 1] - val;
    for (int i = 1; i < n.size(); i++) {
      cout << val << " " << n[i] << endl;
      val = val - n[i];
    }
    return 0;
  }
}