#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const long long maxn = 1e5 + 10, LG = 30, mod = 1e9 + 7, inf = 1e18;
vector<long long> st;
vector<pair<long long, long long> > baz;
vector<long long> v;
void run(long long x, long long i) {
  if (v.back() <= x)
    for (int j = v.back() + 1; j <= x; j++) st.push_back(i);
  else
    for (int j = x + 1; j <= v.back(); j++)
      baz.push_back({st.back(), i - 1}), st.pop_back();
  while (v.back() <= x) v.pop_back();
  v.push_back(x);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  v.push_back(inf), v.push_back(0);
  for (int i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    run(x, i);
  }
  run(0, n + 1);
  cout << (int)(baz.size()) << endl;
  for (auto x : baz) cout << x.first << ' ' << x.second << endl;
  return 0;
}
