Problem p03498 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/* Coding Space*/
vt<ll> v;
int n;
tuple<ll, int> area_max() {
  int index = 0;
  ll max_v = -LLINF;
  rep(i, n) {
    if (v[i] >= max_v) {
      index = i;
      max_v = v[i];
    }
  }
  return tuple<ll, int>{max_v, index};
}
tuple<ll, int> area_min() {
  int index = 0;
  ll min_v = LLINF;
  rep(i, n) {
    if (v[i] <= min_v) {
      index = i;
      min_v = v[i];
    }
  }
  return tuple<ll, int>{min_v, index};
}
vector<tii> ans;
int main() {
  cin >> n;
  rep(i, n) {
    ll in;
    cin >> in;
    v.pb(in);
  }

  for (int i = 0; i < n - 1; i++) {
    if (i < 0)
      i = 0;
    if (v[i + 1] - v[i] >= 0)
      continue;
    if (v[i + 1] > 0 || v[i] > 0)
      while (v[i + 1] - v[i] < 0) {
        ll a;
        int index;
        tie(a, index) = area_max();
        ans.pb(tii{index, i + 1});
        v[i + 1] += a;
      }
    else {
      while (v[i + 1] - v[i] < 0) {
        ll a;
        int index;
        tie(a, index) = area_min();
        ans.pb(tii{index, i});
        v[i] += a;
        i -= 2;
      }
    }
  }
  cout << ans.size() << endl;
  for (auto i : ans) {
    int a, b;
    tie(a, b) = i;
    cout << a + 1 << " " << b + 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/* Coding Space*/
vt<ll> v;
int n;
tuple<ll, int> area_max() {
  int index = 0;
  ll max_v = -LLINF;
  rep(i, n) {
    if (v[i] >= max_v) {
      index = i;
      max_v = v[i];
    }
  }
  return tuple<ll, int>{max_v, index};
}
tuple<ll, int> area_min() {
  int index = 0;
  ll min_v = LLINF;
  rep(i, n) {
    if (v[i] <= min_v) {
      index = i;
      min_v = v[i];
    }
  }
  return tuple<ll, int>{min_v, index};
}
vector<tii> ans;
int main() {
  cin >> n;
  rep(i, n) {
    ll in;
    cin >> in;
    v.pb(in);
  }
  int a, b;
  ll minv, maxv;
  tie(maxv, a) = area_max();
  tie(minv, b) = area_min();
  // 絶対値の大きい方を採用する
  if (llabs(maxv) > llabs(minv)) {
    rep(i, n) {
      ll add;
      int index;
      tie(add, index) = area_max();
      ans.pb(tii{index, i});
      v[i] += add;
    }
  } else {
    rep(i, n) {
      ll add;
      int index;
      tie(add, index) = area_min();
      ans.pb(tii{index, i});
      v[i] += add;
    }
  }
  if (v[0] > 0) {
    for (int i = 0; i < n - 1; i++) {
      if (v[i + 1] - v[i] > 0)
        continue;
      ll add;
      int in;
      tie(add, in) = area_max();
      ans.pb(tii{in, i + 1});
      v[i + 1] += add;
    }
  } else {
    for (int i = n - 1; i > 0; i--) {
      if (v[i] - v[i - 1] > 0)
        continue;
      ll add;
      int in;
      tie(add, in) = area_min();
      ans.pb(tii{in, i - 1});
      v[i - 1] += add;
    }
  }
  cout << ans.size() << endl;
  for (auto i : ans) {
    int a, b;
    tie(a, b) = i;
    cout << a + 1 << " " << b + 1 << endl;
  }
}