#include <bits/stdc++.h>
using namespace std;
pair<int, int> A[100010];
int B[100010];
int n;
pair<int, int> resp[100010];
int veces[100010];
int to, tam;
int main() {
  scanf("%d", &n);
  A[0].first = 0;
  A[0].second = 0;
  for (int i = 1; i < n + 1; i++) scanf("%d", &A[i].first), A[i].second = i;
  for (int i = 1; i < n + 1; i++) B[i] = A[i].first;
  B[0] = 0;
  B[n + 1] = 0;
  set<int> atras;
  A[n + 1].first = 0;
  A[n + 1].second = n + 1;
  sort(A, A + n + 2);
  tam = 0;
  to = 0;
  set<int>::iterator it;
  int pos = 0;
  for (int i = 0; i < n + 2; i++) {
    if (A[i].first == 0)
      atras.insert(A[i].second), pos++;
    else
      break;
  }
  int ind, fin;
  int agua = 0;
  int last = 0;
  int ind2;
  for (int i = pos; i < n + 2;) {
    it = atras.upper_bound(A[i].second);
    ind = *(it);
    it--;
    ind2 = *it;
    atras.insert(A[i].second);
    fin = ind - 1;
    veces[tam] = A[i].first - max(B[ind], B[ind2]);
    to += veces[tam];
    resp[tam].first = ind2 + 1;
    resp[tam++].second = fin;
    i++;
    while (i < n + 2 && A[i - 1].first == A[i].first && A[i].second <= fin)
      atras.insert(A[i].second), i++;
  }
  printf("%d\n", to);
  for (int i = 0; i < tam; i++) {
    for (int j = 0; j < veces[i]; j++)
      printf("%d %d\n", resp[i].first, resp[i].second);
  }
  return 0;
}
