#include <bits/stdc++.h>
using namespace std;
typedef struct t {
  int l, r;
  int min;
} T;
int n, N;
int a[100002 + 1];
bool on;
t tree[3 * 100002 + 2];
int k;
void def_tree(int v) {
  if (v >= k) return;
  def_tree(2 * v);
  def_tree(2 * v + 1);
  tree[v].min = tree[2 * v].min;
  if (tree[2 * v + 1].min != 1000002 &&
      a[tree[2 * v + 1].min] < a[tree[2 * v].min]) {
    tree[v].min = tree[2 * v + 1].min;
  }
  tree[v].l = tree[2 * v].l;
  tree[v].r = tree[2 * v + 1].r;
}
int find_zero(int l, int r, int v) {
  if (l > r) return l;
  if (l == tree[v].l && r == tree[v].r) {
    return tree[v].min;
  }
  if (r <= tree[2 * v].r) {
    return find_zero(l, r, 2 * v);
  }
  if (l >= tree[2 * v + 1].l) {
    return find_zero(l, r, 2 * v + 1);
  }
  int x, y;
  x = find_zero(l, tree[2 * v].r, 2 * v);
  y = find_zero(tree[2 * v + 1].l, r, 2 * v + 1);
  if (a[y] < a[x]) return y;
  return x;
}
int compute(int l, int r, int val) {
  if (l > r) return 0;
  int steps = 0;
  int now_min = a[find_zero(l, r, 1)];
  int i;
  if (on) {
    for (i = val; i < now_min; i++) {
      printf("%d %d\n", l, r);
    }
  }
  steps += now_min - val;
  val = now_min;
  int first, second;
  first = find_zero(l, r, 1);
  steps += compute(l, first - 1, val);
  second = find_zero(first + 1, r, 1);
  while (second < r && a[second] == val) {
    steps += compute(first + 1, second - 1, val);
    first = second;
    second = find_zero(first + 1, r, 1);
  }
  steps += compute(first + 1, r, val);
  return steps;
}
int main() {
  scanf("%d", &n);
  N = n;
  if (n == 1) {
    int x;
    int i;
    scanf("%d", &x);
    printf("%d\n", x);
    for (i = 0; i < x; i++) printf("1 1\n");
    return 0;
  }
  k = 1;
  while (k < n) k *= 2;
  int i;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i + 1]);
    tree[k + i].l = i + 1;
    tree[k + i].r = i + 1;
    tree[k + i].min = i + 1;
  }
  while (n < k) {
    tree[k + n].l = n + 1;
    tree[k + n].r = n + 1;
    tree[k + n].min = 1000002;
    n++;
  }
  def_tree(1);
  on = 0;
  int calls = compute(1, N, 0);
  printf("%d\n", calls);
  on = 1;
  compute(1, N, 0);
  return 0;
}
