Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

#define MAX_N (100000)

using namespace std;

int main(int argc, char *argv[]) {
  // read inputs
  int N, as[MAX_N];
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &as[i]);
  }

  // divide into positive and negative
  vector<int> ps, ns;
  for (int i = 0; i < N; i++) {
    const int a = as[i];
    if (a >= 0) {
      ps.push_back(a);
    } else {
      ns.push_back(a);
    }
  }

  // solve
  int ans;
  vector<int> xs, ys;
  if (ns.size() == 0) {
    sort(ps.begin(), ps.end());
    const int n = ps[0] - ps[ps.size() - 1];
    ns.push_back(n);
    xs.push_back(ps[0]);
    ys.push_back(ps[ps.size() - 1]);
    vector<int> pds;
    for (int i = 1; i < ps.size() - 1; i++) {
      pds.push_back(ps[i]);
    }
    ps = pds;
  } else if (ps.size() == 0) {
    sort(ns.begin(), ns.end());
    const int p = ns[ns.size() - 1] - ns[0];
    xs.push_back(ns[ns.size() - 1]);
    ys.push_back(ns[0]);
    ps.push_back(p);
    vector<int> nds;
    for (int i = 1; i < ns.size() - 1; i++) {
      nds.push_back(ns[i]);
    }
    ns = nds;
  }
  if (ps.size() > 0 && ns.size() > 0) {
    for (int i = 1; i < ps.size(); i++) {
      const int x = ns[0], y = ps[i];
      xs.push_back(x);
      ys.push_back(y);
      ns[0] -= y;
    }
    for (int i = 0; i < ns.size(); i++) {
      const int x = ps[0], y = ns[i];
      xs.push_back(x);
      ys.push_back(y);
      ps[0] -= y;
    }
    ans = ps[0];
  } else {
    exit(-1);
  }

  // print ans
  printf("%d\n", ans);
  for (int i = 0; i < N - 1; i++) {
    printf("%d %d\n", xs[i], ys[i]);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

#define MAX_N (100000)

using namespace std;

int main(int argc, char *argv[]) {
  // read inputs
  int N, as[MAX_N];
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &as[i]);
  }

  // divide into positive and negative
  vector<int> ps, ns;
  for (int i = 0; i < N; i++) {
    const int a = as[i];
    if (a >= 0) {
      ps.push_back(a);
    } else {
      ns.push_back(a);
    }
  }

  // solve
  int ans;
  vector<int> xs, ys;
  if (ns.size() == 0) {
    sort(ps.begin(), ps.end());
    const int n = ps[0] - ps[ps.size() - 1];
    ns.push_back(n);
    xs.push_back(ps[0]);
    ys.push_back(ps[ps.size() - 1]);
    vector<int> pds;
    for (int i = 1; i < ps.size() - 1; i++) {
      pds.push_back(ps[i]);
    }
    ps = pds;
  } else if (ps.size() == 0) {
    sort(ns.begin(), ns.end());
    const int p = ns[ns.size() - 1] - ns[0];
    xs.push_back(ns[ns.size() - 1]);
    ys.push_back(ns[0]);
    ps.push_back(p);
    vector<int> nds;
    for (int i = 1; i < ns.size() - 1; i++) {
      nds.push_back(ns[i]);
    }
    ns = nds;
  }
  if (ps.size() > 0 && ns.size() > 0) {
    for (int i = 1; i < ps.size(); i++) {
      const int x = ns[0], y = ps[i];
      xs.push_back(x);
      ys.push_back(y);
      ns[0] -= y;
    }
    for (int i = 0; i < ns.size(); i++) {
      const int x = ps[0], y = ns[i];
      xs.push_back(x);
      ys.push_back(y);
      ps[0] -= y;
    }
    ans = ps[0];
  } else if (ps.size() > 0) {
    ans = ps[0];
  } else if (ns.size() > 0) {
    ans = -ns[0];
    int tmp = xs[0];
    xs[0] = ys[0];
    ys[0] = tmp;
  } else {
    exit(-1);
  }

  // print ans
  printf("%d\n", ans);
  for (int i = 0; i < N - 1; i++) {
    printf("%d %d\n", xs[i], ys[i]);
  }

  return 0;
}