#include <bits/stdc++.h>
using namespace std;
int n;
int a[111111];
stack<int> sh;
vector<pair<int, int> > res;
int main(void) {
  scanf("%d", &n);
  int h = 0;
  for (int i = 0; i <= n; i++) {
    if (i < n) {
      scanf("%d", &a[i]);
    } else {
      a[i] = 0;
    }
    if (a[i] == h) {
      continue;
    }
    if (a[i] > h) {
      while (h < a[i]) {
        h++;
        sh.push(i + 1);
      }
    } else {
      while (h > a[i]) {
        h--;
        res.push_back(pair<int, int>(sh.top(), i));
        sh.pop();
      }
    }
  }
  printf("%d\n", (int)res.size());
  for (size_t i = 0; i < res.size(); i++) {
    printf("%d %d\n", res[i].first, res[i].second);
  }
  return 48 - 48;
}
