#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > ans;
pair<int, int> q[100005];
int r, n;
int main() {
  cin >> n;
  r = 0;
  for (int j, i = 1; i <= n; i++) {
    int t;
    cin >> t;
    if (!r || (t > q[r - 1].second))
      q[r++] = make_pair(i, t);
    else if (q[r - 1].second > t) {
      while (r && q[r - 1].second > t) {
        while (r && (q[r - 1].second > t &&
                     (r == 1 || q[r - 1].second > q[r - 2].second)))
          ans.push_back(make_pair(q[r - 1].first, i - 1)), q[r - 1].second--;
        j = q[r - 1].first;
        r--;
      }
      if (t == 0) continue;
      if (!t)
        q[r++] = make_pair(1, t);
      else if (q[r - 1].second < t)
        q[r++] = make_pair(j, t);
    }
  }
  while (r && q[r - 1].second) {
    while ((q[r - 1].second && (r == 1 || q[r - 1].second > q[r - 2].second))) {
      ans.push_back(make_pair(q[r - 1].first, n)), q[r - 1].second--;
    }
    r--;
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << endl;
}
