Problem p03498 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i))
#define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r, v) auto r = (v)
#else
#define aut(r, v) __typeof(v) r = (v)
#endif
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset(m, v, sizeof(m))
#define INF 0x3f3f3f3f3f3f3f3fLL
#define SIZE 8080

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef pair<int, ll> pill;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
typedef complex<double> P;
long long int MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int N;
vi a;
int main() {
  cin >> N;
  int x;
  int hoge = 0, ma = 0;
  for (int i = 0; i < N; i++) {
    cin >> x;
    a.pb(x);
    if (ma < abs(x)) {
      hoge = i, ma = abs(x);
    }
  }
  if (a[hoge] > 0) {
    cout << 2 * N - 2 << endl;
    cout << hoge + 1 << " 2" << endl;
    cout << hoge + 1 << " 2" << endl;
    for (int i = 3; i <= N; i++) {
      cout << i - 1 << " " << i << endl;
      cout << i - 1 << " " << i << endl;
    }
  } else if (a[hoge] < 0) {
    cout << 2 * N - 2 << endl;
    cout << hoge + 1 << " " << N - 1 << endl;
    cout << hoge + 1 << " " << N - 1 << endl;
    for (int i = N - 3; i >= 0; i++) {
      cout << i << " " << i - 1 << endl;
      cout << i << " " << i - 1 << endl;
    }
  } else {
    cout << "0" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i))
#define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r, v) auto r = (v)
#else
#define aut(r, v) __typeof(v) r = (v)
#endif
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset(m, v, sizeof(m))
#define INF 0x3f3f3f3f3f3f3f3fLL
#define SIZE 8080

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef pair<int, ll> pill;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
typedef complex<double> P;
long long int MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int N;
vi a;
int main() {
  cin >> N;
  int x;
  int hoge = 0, ma = 0;
  for (int i = 0; i < N; i++) {
    cin >> x;
    a.pb(x);
    if (ma < abs(x)) {
      hoge = i, ma = abs(x);
    }
  }
  if (a[hoge] > 0) {
    cout << 2 * N - 2 << endl;
    cout << hoge + 1 << " 2" << endl;
    cout << hoge + 1 << " 2" << endl;
    for (int i = 3; i <= N; i++) {
      cout << i - 1 << " " << i << endl;
      cout << i - 1 << " " << i << endl;
    }
  } else if (a[hoge] < 0) {
    cout << 2 * N - 2 << endl;
    cout << hoge + 1 << " " << N - 1 << endl;
    cout << hoge + 1 << " " << N - 1 << endl;
    for (int i = N - 2; i >= 1; i--) {
      cout << i + 1 << " " << i << endl;
      cout << i + 1 << " " << i << endl;
    }
  } else {
    cout << "0" << endl;
  }
}