#include <bits/stdc++.h>
using namespace std;
int a[100005];
vector<pair<int, int> > v;
vector<pair<int, int> > ans;
int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  v.push_back(make_pair(a[0], 0));
  int last = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] > last) {
      v.push_back(make_pair(a[i], i));
      last = a[i];
    } else {
      if (a[i] < last) {
        if (v.size() == 1) {
          int diff = last - a[i];
          for (int j = 0; j < diff; j++)
            ans.push_back(make_pair(v[0].second, i - 1));
          v.pop_back();
          v.push_back(make_pair(a[i], 0));
          last = a[i];
        } else {
          pair<int, int> p = v[v.size() - 2];
          if (a[i] > p.first) {
            int diff = last - a[i];
            for (int j = 0; j < diff; j++)
              ans.push_back(make_pair(v.back().second, i - 1));
            v.back().first = a[i];
            last = a[i];
          } else {
            if (a[i] == p.first) {
              int diff = last - a[i];
              for (int j = 0; j < diff; j++)
                ans.push_back(make_pair(v.back().second, i - 1));
              v.pop_back();
              last = a[i];
            } else {
              int diff = last - p.first;
              for (int j = 0; j < diff; j++)
                ans.push_back(make_pair(v.back().second, i - 1));
              v.pop_back();
              last = p.first;
              i--;
            }
          }
        }
      }
    }
  }
  for (int i = v.size() - 1; i > 0; i--) {
    int diff = v[i].first - v[i - 1].first;
    for (int j = 0; j < diff; j++) ans.push_back(make_pair(v[i].second, n - 1));
  }
  if (v[0].first != 0)
    for (int i = 0; i < v[0].first; i++)
      ans.push_back(make_pair(v[0].second, n - 1));
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); i++) {
    printf("%d %d\n", ans[i].first + 1, ans[i].second + 1);
  }
  return 0;
}
