#include <bits/stdc++.h>
using namespace std;
const int MX = 100007;
vector<pair<int, int> > ans;
pair<int, int> table[100007][20];
void DnC(int le, int ri, int he) {
  if (le > ri) return;
  int range = log2(ri - le + 1);
  pair<int, int> now =
      min(table[le][range], table[ri - (1 << range) + 1][range]);
  for (int i = he; i < now.first; i++) {
    ans.push_back(make_pair(le + 1, ri + 1));
  }
  DnC(le, now.second - 1, now.first);
  DnC(now.second + 1, ri, now.first);
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> table[i][0].first;
    table[i][0].second = i;
  }
  for (int j = 1; (1 << j) <= N; j++) {
    for (int i = 0; i + (1 << j) <= N; i++) {
      table[i][j] = min(table[i][j - 1], table[i + (1 << (j - 1))][j - 1]);
    }
  }
  DnC(0, N - 1, 0);
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first << ' ' << ans[i].second << endl;
  }
}
