Problem p03498 (C++)

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

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\n";
}
typedef long long ll;

signed main() {
  int n;
  cin >> n;
  int minn = INF;
  int minid = 0;
  int maxn = -INF;
  int maxid = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (minn > a[i]) {
      minn = a[i];
      minid = i;
    }
    if (maxn < a[i]) {
      maxn = a[i];
      maxid = i;
    }
  }
  if (abs(maxn) >= abs(minn)) {
    vector<pair<int, int>> vp(n);
    for (int i = 0; i < n; i++) {
      vp[i] = {maxid, i};
      a[i] += a[maxid];
    }
    int c = 0;
    for (int i = 0; i < n - 1; i++) {
      if (a[i] > a[i + 1]) {
        vp.push_back({i, i + 1});
        a[i + 1] += a[i];
      }
    }
    //  show(vp);
    assert(vp.size() <= 2 * n);
    cout << vp.size() << endl;
    for (int i = 0; i < vp.size(); i++) {
      cout << vp[i].first + 1 << " " << vp[i].second + 1 << endl;
    }

  } else {
    vector<pair<int, int>> vp(n);
    for (int i = 0; i < n; i++) {
      vp[i] = {minid, i};
      a[i] += a[minid];
    }

    int c = 0;
    for (int i = 0; i < n - 1; i++) {
      if (a[i] > a[i + 1]) {
        vp.push_back({i + 1, i});
        a[i] += a[i + 1];
      }
    }
    for (int i = 0; i < n - 1; i++) {
      assert(a[i] <= a[i + 1]);
    }

    // assert(vp.size() <= 2 * n);
    cout << vp.size() << endl;
    for (int i = 0; i < vp.size(); i++) {
      cout << vp[i].first + 1 << " " << vp[i].second + 1 << endl;
    }
  }

  return 0;
}

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

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\n";
}
typedef long long ll;

signed main() {
  int n;
  cin >> n;
  int minn = INF;
  int minid = 0;
  int maxn = -INF;
  int maxid = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (minn > a[i]) {
      minn = a[i];
      minid = i;
    }
    if (maxn < a[i]) {
      maxn = a[i];
      maxid = i;
    }
  }
  if (abs(maxn) >= abs(minn)) {
    vector<pair<int, int>> vp(n);
    for (int i = 0; i < n; i++) {
      vp[i] = {maxid, i};
      a[i] += a[maxid];
    }
    int c = 0;
    for (int i = 0; i < n - 1; i++) {
      if (a[i] > a[i + 1]) {
        vp.push_back({i, i + 1});
        a[i + 1] += a[i];
      }
    }
    //  show(vp);
    assert(vp.size() <= 2 * n);
    cout << vp.size() << endl;
    for (int i = 0; i < vp.size(); i++) {
      cout << vp[i].first + 1 << " " << vp[i].second + 1 << endl;
    }

  } else {
    vector<pair<int, int>> vp(n);
    for (int i = 0; i < n; i++) {
      vp[i] = {minid, i};
      a[i] += a[minid];
    }

    int c = 0;
    for (int i = n - 1; i >= 1; i--) {
      if (a[i] < a[i - 1]) {
        vp.push_back({i, i - 1});
        a[i - 1] += a[i];
      }
    }
    for (int i = 0; i < n - 1; i++) {
      assert(a[i] <= a[i + 1]);
    }

    // assert(vp.size() <= 2 * n);
    cout << vp.size() << endl;
    for (int i = 0; i < vp.size(); i++) {
      cout << vp[i].first + 1 << " " << vp[i].second + 1 << endl;
    }
  }

  return 0;
}