#include <bits/stdc++.h>
using namespace std;
int a[100005];
int s[100005];
int l[100005], r[100005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  int h = 0;
  a[n] = 0;
  int cnt = 0;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    if (a[i] > h) {
      while (h < a[i]) {
        s[cnt++] = i;
        h++;
      }
    } else if (a[i] < h) {
      while (h > a[i]) {
        h--;
        l[ans] = s[--cnt] + 1, r[ans] = i;
        ans++;
      }
    }
  }
  printf("%d\n", ans);
  for (int i = 0; i < ans; i++) {
    printf("%d %d\n", l[i], r[i]);
  }
  return 0;
}
