#include <bits/stdc++.h>
using namespace std;
void read(long long int n = 10) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(n) << fixed;
}
long long int M = 1e9 + 7;
long long int M1 = 998244353;
int32_t main() {
  read();
  long long int n, m;
  vector<pair<long long int, long long int>> res;
  cin >> n;
  vector<long long int> arr(n);
  vector<vector<long long int>> index(1e5 + 1);
  map<pair<long long int, long long int>, long long int> done;
  for (long long int i = 0; i < n; i++)
    cin >> arr[i], index[arr[i]].push_back(i);
  set<pair<long long int, long long int>> left;
  long long int prev = 0;
  for (long long int i = 0; i < n; i++)
    if (arr[i] == 0) {
      if (prev != i) left.insert({prev, i - 1}), done[{prev, i - 1}] = 0;
      prev = i + 1;
    }
  if (prev <= n - 1) left.insert({prev, n - 1}), done[{prev, n - 1}] = 0;
  for (long long int i = 1; i <= 1e5; i++) {
    if (left.size() <= 0) break;
    auto it = left.begin();
    for (long long int j = 0; j < (long long int)index[i].size(); j++) {
      long long int prev = it->first;
      pair<long long int, long long int> copy = *it;
      for (long long int k = done[*it]; k < i; k++)
        res.push_back({it->first + 1, it->second + 1});
      auto ip = it++;
      left.erase(ip);
      while (j < index[i].size() && index[i][j] <= copy.second) {
        if (prev != index[i][j])
          left.insert({prev, index[i][j] - 1}),
              done[{prev, index[i][j] - 1}] = i;
        prev = index[i][j] + 1;
        j++;
      }
      if (prev <= copy.second) {
        left.insert({prev, copy.second}), done[{prev, copy.second}] = i;
        prev = copy.second + 1;
      }
      j--;
    }
  }
  cout << res.size() << "\n";
  for (auto a : res) cout << a.first << " " << a.second << "\n";
}
