#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 17;
int n, a[MAXN];
vector<pair<int, int> > ans;
stack<pair<int, int> > s;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  s.push({0, 0});
  for (int i = 1; i <= n + 1; i++) {
    while (s.size() && s.top().first > a[i]) {
      ans.push_back({s.top().second, i - 1});
      s.pop();
    }
    for (int j = s.top().first + 1; j <= a[i]; j++) s.push({j, i});
  }
  cout << ans.size() << endl;
  for (auto i : ans) cout << i.first << ' ' << i.second << endl;
  return 0;
}
