Problem p03496 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool fl = true, flag = false;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct edge {
  ll to, cost;
};
string abc = "abcdefghijklmnopqrstuvwxyz";

int main() {
  int n, a[100], Min = INF, Max = 0, maxit = 0, minit = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (Min > a[i]) {
      Min = min(Min, a[i]);
      minit = i + 1;
    }
    if (Max < a[i]) {
      Max = max(Max, a[i]);
      maxit = i + 1;
    }
  }
  if (Min >= 0 && Max >= 0) {
    cout << n - 1 << endl;
    for (int i = 0; i < n - 1; i++) {
      cout << i + 1 << ' ' << i + 2 << endl;
    }
  } else if (Min <= 0 && Max <= 0) {
    cout << n - 1 << endl;
    for (int i = n - 1; i > 0; i--) {
      cout << i + 1 << ' ' << i << endl;
    }
  } else if (abs(Min) < abs(Max)) {
    cout << n + n - 1 << endl;
    for (int i = 0; i < n; i++) {
      cout << maxit << ' ' << i + 1 << endl;
    }
    for (int i = 0; i < n - 1; i++) {
      cout << i + 1 << ' ' << i + 2 << endl;
    }
  } else {
    cout << n + n - 1 << endl;
    for (int i = 0; i < n; i++) {
      cout << minit << ' ' << i + 1 << endl;
    }
    for (int i = n - 1; i > 0; i++) {
      cout << i + 1 << ' ' << i << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool fl = true, flag = false;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct edge {
  ll to, cost;
};
string abc = "abcdefghijklmnopqrstuvwxyz";

int main() {
  int n, a[100], Min = INF, Max = 0, maxit = 0, minit = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (Min > a[i]) {
      Min = min(Min, a[i]);
      minit = i + 1;
    }
    if (Max < a[i]) {
      Max = max(Max, a[i]);
      maxit = i + 1;
    }
  }
  if (Min >= 0 && Max >= 0) {
    cout << n - 1 << endl;
    for (int i = 0; i < n - 1; i++) {
      cout << i + 1 << ' ' << i + 2 << endl;
    }
  } else if (Min <= 0 && Max <= 0) {
    cout << n - 1 << endl;
    for (int i = n - 1; i > 0; i--) {
      cout << i + 1 << ' ' << i << endl;
    }
  } else if (abs(Min) < abs(Max)) {
    cout << n + n - 1 << endl;
    for (int i = 0; i < n; i++) {
      cout << maxit << ' ' << i + 1 << endl;
    }
    for (int i = 0; i < n - 1; i++) {
      cout << i + 1 << ' ' << i + 2 << endl;
    }
  } else {
    cout << n + n - 1 << endl;
    for (int i = 0; i < n; i++) {
      cout << minit << ' ' << i + 1 << endl;
    }
    for (int i = n - 1; i > 0; i--) {
      cout << i + 1 << ' ' << i << endl;
    }
  }
}