#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005];
vector<pair<int, int> > v;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  stack<int> s;
  for (int i = 1; i <= n + 1; i++) {
    if (a[i] > a[i - 1]) {
      for (int j = a[i - 1]; j < a[i]; j++) s.push(i);
    } else if (a[i] < a[i - 1]) {
      for (int j = a[i]; j < a[i - 1]; j++) {
        v.push_back(pair<int, int>(s.top(), i - 1));
        s.pop();
      }
    }
  }
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << v[i].first << ' ' << v[i].second << endl;
  }
}
