Problem p03007 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int N, a[100005], cnt[2];
vector<pii> ans;
int64 res = 0;
void Solve() {
  read(N);

  for (int i = 1; i <= N; ++i) {
    read(a[i]);
    if (a[i] < 0)
      cnt[0]++;
    else
      cnt[1]++;
  }
  if (cnt[0] && cnt[1]) {
    int s, t;
    for (int i = 1; i <= N; ++i)
      res += abs(a[i]);
    for (int i = 1; i <= N; ++i) {
      if (a[i] > 0)
        s = i;
      if (a[i] < 0)
        t = i;
    }
    for (int i = 1; i <= N; ++i) {
      if (a[i] >= 0 && i != s) {
        ans.pb(mp(a[t], a[i]));
        a[t] -= a[i];
      }
    }
    for (int i = 1; i <= N; ++i) {
      if (a[i] < 0) {
        ans.pb(mp(a[s], a[i]));
        a[s] -= a[i];
      }
    }
  } else if (cnt[0]) {
    int p = 1;
    for (int i = 2; i <= N; ++i) {
      if (a[i] > a[p])
        p = i;
    }
    res += a[p];
    for (int i = 1; i <= N; ++i) {
      if (i != p)
        res += abs(a[i]);
    }
    for (int i = 1; i <= N; ++i) {
      if (i != p) {
        ans.pb(mp(a[p], a[i]));
        a[p] -= a[i];
      }
    }
  } else {
    int p = 1, q;
    for (int i = 2; i <= N; ++i) {
      if (a[i] < a[p])
        p = i;
    }
    res -= a[p];
    for (int i = 1; i <= N; ++i) {
      if (i != p)
        res += a[i];
    }
    if (p == 1)
      q = 2;
    else
      q = 1;
    for (int i = 1; i <= N; ++i) {
      if (i != p && i != q) {
        ans.pb(mp(a[p], a[i]));
        a[p] -= a[i];
      }
    }
    ans.pb(mp(a[q], a[p]));
  }
  out(res);
  enter;
  for (auto t : ans) {
    out(t.fi);
    space;
    out(t.se);
    enter;
  }
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int N, a[100005], cnt[2];
vector<pii> ans;
int64 res = 0;
void Solve() {
  read(N);

  for (int i = 1; i <= N; ++i) {
    read(a[i]);
    if (a[i] < 0)
      cnt[0]++;
    else
      cnt[1]++;
  }
  if (cnt[0] && cnt[1]) {
    int s, t;
    for (int i = 1; i <= N; ++i)
      res += abs(a[i]);
    for (int i = 1; i <= N; ++i) {
      if (a[i] >= 0)
        s = i;
      if (a[i] < 0)
        t = i;
    }
    for (int i = 1; i <= N; ++i) {
      if (a[i] >= 0 && i != s) {
        ans.pb(mp(a[t], a[i]));
        a[t] -= a[i];
      }
    }
    for (int i = 1; i <= N; ++i) {
      if (a[i] < 0) {
        ans.pb(mp(a[s], a[i]));
        a[s] -= a[i];
      }
    }
  } else if (cnt[0]) {
    int p = 1;
    for (int i = 2; i <= N; ++i) {
      if (a[i] > a[p])
        p = i;
    }
    res += a[p];
    for (int i = 1; i <= N; ++i) {
      if (i != p)
        res += abs(a[i]);
    }
    for (int i = 1; i <= N; ++i) {
      if (i != p) {
        ans.pb(mp(a[p], a[i]));
        a[p] -= a[i];
      }
    }
  } else {
    int p = 1, q;
    for (int i = 2; i <= N; ++i) {
      if (a[i] < a[p])
        p = i;
    }
    res -= a[p];
    for (int i = 1; i <= N; ++i) {
      if (i != p)
        res += a[i];
    }
    if (p == 1)
      q = 2;
    else
      q = 1;
    for (int i = 1; i <= N; ++i) {
      if (i != p && i != q) {
        ans.pb(mp(a[p], a[i]));
        a[p] -= a[i];
      }
    }
    ans.pb(mp(a[q], a[p]));
  }
  out(res);
  enter;
  for (auto t : ans) {
    out(t.fi);
    space;
    out(t.se);
    enter;
  }
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
}