#include <bits/stdc++.h>
using namespace std;
int a[100000 + 10];
struct node {
  int x, y;
};
vector<node> p;
stack<int> st;
int main() {
  int n, i, h;
  node temp;
  while (~scanf("%d", &n)) {
    p.clear();
    for (; !st.empty(); st.pop())
      ;
    for (i = 0; i < n; i++) scanf("%d", &a[i]);
    for (h = i = a[n] = 0; i <= n; i++) {
      while (a[i] < h) {
        h--;
        temp.x = st.top();
        temp.y = i;
        p.push_back(temp);
        st.pop();
      }
      while (a[i] > h) {
        h++;
        st.push(i + 1);
      }
    }
    printf("%d\n", p.size());
    for (i = 0; i < p.size(); i++) printf("%d %d\n", p[i].x, p[i].y);
  }
  return 0;
}
