#include <bits/stdc++.h>
using namespace std;
int n, r, ans;
int s[1002012];
int S[1002012], A[10002012], B[10002012];
inline void Deal(const int& l, const int& r, const int& x) {
  if (l > r)
    return;
  else {
    for (int j = 1; j <= s[S[l]] - x; ++j) A[ans] = S[l], B[ans++] = S[r];
    for (int i = l + 1; i <= r; ++i)
      for (int j = 1; j <= s[S[i]] - s[S[i - 1]]; ++j)
        A[ans] = S[i], B[ans++] = S[r];
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) scanf("%d", s + i);
  S[++r] = 1;
  for (int i = 2; i <= n; ++i) {
    int T = r;
    while (r && s[S[r]] > s[i]) --r;
    if (T > r) {
      ++r;
      if (s[i] == s[S[r]])
        Deal(r, T, s[i]);
      else
        Deal(r, T, s[i]), s[S[r]] = s[i], S[++r] = i;
    } else
      S[++r] = i;
  }
  Deal(1, r, 0);
  printf("%d\n", ans);
  for (int i = 0; i < ans; ++i) printf("%d %d\n", A[i], B[i]);
  return 0;
}
