#include <bits/stdc++.h>
using namespace std;
using namespace std;
int A[100002], L[100002], R[100002];
int father(int node) {
  if (A[node] == node) return node;
  return A[node] = father(A[node]);
}
bool IntheSameset(int a, int b) { return father(a) == father(b); }
void UnIon(int a, int b) { A[father(a)] = father(b); }
int arr[100002];
bool Inseg[100002];
int main() {
  int n;
  cin >> n;
  memset(Inseg, 0, sizeof Inseg);
  vector<pair<int, int> > indx, ans;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    indx.push_back(make_pair(arr[i], i));
    A[i] = L[i] = R[i] = i;
  }
  sort(indx.rbegin(), indx.rend());
  for (int i = 0; i < indx.size(); i++) {
    int val = indx[i].first;
    int loc = indx[i].second;
    if (loc + 1 < n && Inseg[loc + 1]) {
      int l = L[father(loc + 1)];
      int r = R[father(loc + 1)];
      int val2 = arr[father(loc + 1)];
      while (val2 > val) {
        ans.push_back(make_pair(l, r));
        val2--;
      }
      L[father(loc + 1)] = loc;
      R[loc] = R[father(loc + 1)];
      UnIon(loc + 1, loc);
    }
    if (loc > 0 && Inseg[loc - 1]) {
      int l = L[father(loc - 1)];
      int r = R[father(loc - 1)];
      int val2 = arr[father(loc - 1)];
      while (val2 > val) {
        ans.push_back(make_pair(l, r));
        val2--;
      }
      R[father(loc - 1)] = loc;
      L[loc] = L[father(loc - 1)];
      UnIon(loc - 1, loc);
    }
    Inseg[loc] = 1;
  }
  int Min = indx.back().first;
  memset(Inseg, 0, sizeof Inseg);
  for (int i = 0; i < n; i++) {
    int idx = father(i);
    if (Inseg[idx] == 0 && arr[idx] == Min) {
      Inseg[idx] = 1;
      int val = arr[idx];
      while (val > 0) {
        ans.push_back(make_pair(L[idx], R[idx]));
        val--;
      }
    }
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
  return 0;
}
