#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int kol = 0;
  vector<pair<int, int> > s(n + 1);
  vector<pair<int, int> > res;
  int cur;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &cur);
    if (kol == 0) {
      s[kol].first = cur;
      s[kol].second = i;
      kol++;
    } else {
      if (cur > s[kol - 1].first) {
        s[kol].first = cur;
        s[kol].second = i;
        kol++;
      } else {
        while (kol && s[kol - 1].first > cur) {
          int r = i - 1;
          int l = s[kol - 1].second;
          int d;
          if (kol == 1) {
            d = s[0].first - cur;
            for (int j = 0; j < d; j++) res.push_back(make_pair(l, r));
            s[kol - 1].first = cur;
          } else if (cur > s[kol - 2].first) {
            d = s[kol - 1].first - cur;
            for (int j = 0; j < d; j++) res.push_back(make_pair(l, r));
            s[kol - 1].first = cur;
          } else {
            d = s[kol - 1].first - s[kol - 2].first;
            for (int j = 0; j < d; j++) res.push_back(make_pair(l, r));
            kol--;
          }
        }
      }
    }
  }
  while (kol > 0) {
    int l = s[kol - 1].second;
    int d;
    if (kol == 1)
      d = s[0].first;
    else
      d = s[kol - 1].first - s[kol - 2].first;
    for (int j = 0; j < d; j++) res.push_back(make_pair(l, n));
    kol--;
  }
  printf("%d\n", res.size());
  for (int i = 0; i < res.size(); i++)
    printf("%d %d\n", res[i].first, res[i].second);
  return 0;
}
