#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
int n, ans;
int a[maxn], s[maxn], e[maxn];
vector<int> v;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  v.push_back(0);
  for (int i = 1; i <= n; i++) {
    while (a[v.back()] > a[i]) {
      v.pop_back();
    }
    s[i] = v.back();
    v.push_back(i);
  }
  v.clear();
  a[n + 1] = -1;
  v.push_back(n + 1);
  for (int i = n; i >= 1; i--) {
    while (a[v.back()] >= a[i]) {
      v.pop_back();
    }
    e[i] = v.back();
    v.push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    int mx = max(a[s[i]], a[e[i]]);
    int c = a[i] - mx;
    ans += c;
  }
  printf("%d \n", ans);
  for (int i = 1; i <= n; i++) {
    int mx = max(a[s[i]], a[e[i]]);
    int c = a[i] - mx;
    for (int j = 0; j < c; j++) {
      printf("%d ", s[i] + 1);
      printf("%d \n", e[i] - 1);
    }
  }
  return 0;
}
