#include <bits/stdc++.h>
using namespace std;
const int limite = 1000000;
pair<int, int> v[limite];
int lenv = 1;
vector<pair<int, int> > sol;
void trata(int pos, int a) {
  if (v[lenv - 1].second < a) {
    v[lenv].first = pos;
    v[lenv].second = a;
    lenv++;
  } else {
    while (v[lenv - 1].second > a) {
      sol.push_back(pair<int, int>(v[lenv - 1].first, pos - 1));
      v[lenv - 1].second--;
      if (v[lenv - 1].second == v[lenv - 2].second) lenv--;
    }
  }
}
int main() {
  v[0].first = v[0].second = 0;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    trata(i, a);
  }
  trata(n + 1, 0);
  cout << int(sol.size()) << endl;
  for (int i = 0; i < int(sol.size()); i++)
    cout << sol[i].first << " " << sol[i].second << endl;
}
