#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mod = 1e9 + 7;
int tree[N], p[N], l[N];
int par(int x) {
  if (p[x] == x) return x;
  return p[x] = par(p[x]);
}
void unite(int x, int y) {
  x = par(x);
  y = par(y);
  if (x == y) return;
  if (x > y) swap(x, y);
  p[x] = y;
  l[y] = min(l[y], l[x]);
}
int main() {
  int n, a;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &tree[i]);
    l[i] = p[i] = i;
  }
  vector<pair<int, int> > res;
  for (int i = 1; i <= n; ++i) {
    while (1) {
      int R = par(i);
      int L = l[R];
      for (int j = R + 1; j <= n; ++j) {
        if (tree[par(j)] == tree[par(R)]) {
          unite(par(j), par(R));
          j = par(j);
        } else {
          break;
        }
      }
      for (int j = L - 1; j > 0; --j) {
        if (tree[par(j)] == tree[par(R)]) {
          unite(par(j), par(R));
          j = l[par(j)];
        } else {
          break;
        }
      }
      R = par(i);
      L = l[R];
      if (tree[R] > max(tree[par(R + 1)], tree[par(L - 1)])) {
        tree[L]--;
        if (R > L) tree[R]--;
        res.push_back({L, R});
      } else {
        i = R;
        break;
      }
    }
  }
  printf("%d\n", (int)res.size());
  while (!res.empty()) {
    printf("%d %d\n", res.back().first, res.back().second);
    res.pop_back();
  }
  return 0;
}
