#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 2;
int n, a[MAXN], leftPnt[MAXN], rightPnt[MAXN];
priority_queue<pair<int, pair<int, int> > > heap;
vector<pair<int, int> > op;
int calc() {
  int res = 0;
  for (int i = 0; i <= n + 1; i = leftPnt[i] + 1) {
    heap.push(make_pair(a[i], make_pair(i, leftPnt[i])));
  }
  for (; !heap.empty(); heap.pop()) {
    int value = heap.top().first, x = heap.top().second.first,
        y = heap.top().second.second;
    if (!value || leftPnt[x] != y || rightPnt[y] != x) {
      continue;
    }
    int step = min(value - a[x - 1], value - a[y + 1]);
    res += step;
    for (int i = 0; i < step; i++) {
      op.push_back(make_pair(x, y));
    }
    if (a[x - 1] == value - step) {
      x = rightPnt[x - 1];
    }
    if (a[y + 1] == value - step) {
      y = leftPnt[y + 1];
    }
    leftPnt[x] = y;
    rightPnt[y] = x;
    heap.push(make_pair(value - step, make_pair(x, y)));
  }
  return res;
}
int main() {
  scanf("%d", &n);
  int prev = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == a[i - 1]) {
      continue;
    } else {
      leftPnt[prev] = i - 1;
      rightPnt[i - 1] = prev;
      prev = i;
    }
  }
  if (a[n]) {
    leftPnt[prev] = n;
    rightPnt[n] = prev;
    leftPnt[n + 1] = rightPnt[n + 1] = n + 1;
  } else {
    leftPnt[prev] = n + 1;
    rightPnt[n + 1] = prev;
  }
  printf("%d\n", calc());
  for (vector<pair<int, int> >::const_iterator it = op.begin(); it != op.end();
       it++) {
    printf("%d %d\n", it->first, it->second);
  }
  return 0;
}
