#include <bits/stdc++.h>
int value[100001], left[100001], right[100001], parent[100001];
int from[100001], to[100001], m;
void foo(int l, int r, int root, int d) {
  while (d < value[root]) from[m] = l, to[m] = r, m++, d++;
  if (left[root]) foo(l, root - 1, left[root], d);
  if (right[root]) foo(root + 1, r, right[root], d);
}
int main(int argc, char *argv[]) {
  int n, i, root, current;
  scanf("%d", &n);
  scanf("%d", &value[1]);
  current = 1;
  root = 1;
  for (i = 2; i <= n; i++) {
    scanf("%d", &value[i]);
    current = i - 1;
    while (current && value[current] > value[i]) current = parent[current];
    if (!current) {
      left[i] = root;
      parent[root] = i;
      root = i;
    } else {
      left[i] = right[current];
      right[current] = i;
      parent[i] = current;
    }
  }
  foo(1, n, root, 0);
  printf("%d\n", m);
  for (i = 0; i < m; i++) printf("%d %d\n", from[i], to[i]);
  return 0;
}
