#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, n, now = 0;
  scanf("%d", &n);
  int a[n];
  vector<pair<int, int> > ans;
  stack<int> st;
  for (i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (i = 0; i < n; ++i) {
    while (a[i] > now) {
      st.push(i);
      ++now;
    }
    while (a[i] < now) {
      ans.push_back(make_pair(st.top(), i - 1));
      st.pop();
      --now;
    }
  }
  while (!st.empty()) {
    ans.push_back(make_pair(st.top(), n - 1));
    st.pop();
  }
  printf("%d\n", ans.size());
  for (i = 0; i < ans.size(); ++i)
    printf("%d %d\n", ans[i].first + 1, ans[i].second + 1);
  return 0;
}
