#include <bits/stdc++.h>
using namespace std;
struct node {
  int i, j;
};
node nodes[101000];
int a[101000], n;
int le[101000], ri[101000];
vector<int> v[101000];
int findr(int x) {
  if (ri[x] == x) return x;
  return ri[x] = findr(ri[x]);
}
int findl(int x) {
  if (le[x] == x) return x;
  return le[x] = findl(le[x]);
}
int main() {
  int i, j, k, l, t;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    v[a[i]].push_back(i);
    le[i] = i;
    ri[i] = i;
  }
  for (i = 1; i < n; i++) {
    if (a[i] == a[i + 1]) {
      ri[i] = i + 1;
    }
  }
  for (i = n - 1; i > 1; i--) {
    if (a[i] == a[i - 1]) {
      le[i] = i - 1;
    }
  }
  int k1, l1;
  int c = 0;
  for (i = 101000; i > 0; i--) {
    for (j = 0; j < v[i].size(); j++) {
      k = v[i][j];
      if (k != findl(k)) continue;
      if (a[k] != i) continue;
      k = findl(k);
      l = findr(v[i][j]);
      nodes[c].i = k;
      nodes[c].j = l;
      c++;
      if (k != l) {
        a[k]--;
        a[l]--;
      } else
        a[k]--;
      if (k > 1) {
        k1 = findl(k - 1);
        if (a[k] == a[k1]) {
          le[k] = k1;
          ri[k1] = findr(k);
          ri[k - 1] = ri[k1];
        }
      }
      if (l < n) {
        l1 = findr(l + 1);
        if (a[l] == a[l1]) {
          ri[l] = l1;
          le[l1] = findl(l);
          le[l + 1] = le[l1];
        }
      }
      k = findl(k);
      ri[k] = findr(l);
      l = findr(l);
      le[l] = k;
      v[i - 1].push_back(k);
    }
  }
  printf("%d\n", c);
  for (i = 0; i < c; i++) {
    printf("%d %d\n", nodes[i].i, nodes[i].j);
  }
  return 0;
}
