Problem p03498 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF = 1e18;
constexpr ll MOD = 998244353;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

template <typename T> T Pow(T a, T b) {
  T ret = 1;
  for (int i = 0; i < b; i++) {
    ret *= a;
  }
  return ret;
}

ll mod(ll val) {
  ll res = val % MOD;
  if (res < 0) {
    res += MOD;
  }
  return res;
}

ll RepeatSquaring(ll N, ll P, ll M) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = RepeatSquaring(N, P / 2, M);
    return t * t % M;
  }
  return N * RepeatSquaring(N, P - 1, M) % M;
}

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int minus = Inf;
  int plus = -Inf;
  int mitr = -1;
  int pitr = -1;
  for (int i = 0; i < N; i++) {
    if (vec.at(i) < 0) {
      if (vec.at(i) < minus) {
        minus = vec.at(i);
        mitr = i;
      }
    } else {
      if (vec.at(i) > plus) {
        plus = vec.at(i);
        pitr = i;
      }
    }
  }
  vector<P> ret;
  if (abs(minus) < plus) {
    for (int i = 0; i < N; i++) {
      vec.at(i) += vec.at(pitr);
      ret.push_back(P(pitr + 1, i + 1));
    }
    for (int i = 0; i < N - 1; i++) {
      ret.push_back(P(i + 1, i + 2));
    }
    cout << ret.size() << endl;
    for (int i = 0; i < ret.size(); i++) {
      cout << ret.at(i).first << " " << ret.at(i).second << endl;
    }
  } else {
    for (int i = 0; i < N; i++) {
      vec.at(i) += vec.at(mitr);
      ret.push_back(P(mitr + 1, i + 1));
    }
    for (int i = N - 1; i >= 1; i--) {
      ret.push_back(P(i + 1, i));
    }
    cout << ret.size() << endl;
    for (int i = 0; i < ret.size(); i++) {
      cout << ret.at(i).first << " " << ret.at(i).second << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF = 1e18;
constexpr ll MOD = 998244353;
const double PI = 3.1415926535897;
typedef pair<int, int> P;

template <typename T> T Pow(T a, T b) {
  T ret = 1;
  for (int i = 0; i < b; i++) {
    ret *= a;
  }
  return ret;
}

ll mod(ll val) {
  ll res = val % MOD;
  if (res < 0) {
    res += MOD;
  }
  return res;
}

ll RepeatSquaring(ll N, ll P, ll M) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = RepeatSquaring(N, P / 2, M);
    return t * t % M;
  }
  return N * RepeatSquaring(N, P - 1, M) % M;
}

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int minus = Inf;
  int plus = -Inf;
  int mitr = -1;
  int pitr = -1;
  for (int i = 0; i < N; i++) {
    if (vec.at(i) < 0) {
      if (vec.at(i) < minus) {
        minus = vec.at(i);
        mitr = i;
      }
    } else {
      if (vec.at(i) > plus) {
        plus = vec.at(i);
        pitr = i;
      }
    }
  }
  vector<P> ret;
  if (abs(minus) < plus || mitr == -1) {
    for (int i = 0; i < N; i++) {
      vec.at(i) += vec.at(pitr);
      ret.push_back(P(pitr + 1, i + 1));
    }
    for (int i = 0; i < N - 1; i++) {
      ret.push_back(P(i + 1, i + 2));
    }
    cout << ret.size() << endl;
    for (int i = 0; i < ret.size(); i++) {
      cout << ret.at(i).first << " " << ret.at(i).second << endl;
    }
  } else {
    for (int i = 0; i < N; i++) {
      vec.at(i) += vec.at(mitr);
      ret.push_back(P(mitr + 1, i + 1));
    }
    for (int i = N - 1; i >= 1; i--) {
      ret.push_back(P(i + 1, i));
    }
    cout << ret.size() << endl;
    for (int i = 0; i < ret.size(); i++) {
      cout << ret.at(i).first << " " << ret.at(i).second << endl;
    }
  }
}