#include <bits/stdc++.h>
using namespace std;
int n;
int a[100011];
pair<int, int> dp[5 * 100011];
void build(int idx = 1, int b = 1, int e = n) {
  if (b == e) {
    dp[idx] = pair<int, int>(a[b], b);
    return;
  }
  int mid = (b + e) / 2;
  build(idx * 2, b, mid);
  build(idx * 2 + 1, mid + 1, e);
  dp[idx] = min(dp[idx * 2], dp[idx * 2 + 1]);
}
pair<int, int> query(int l, int r, int idx = 1, int b = 1, int e = n) {
  if (b > r || l > e) return pair<int, int>(INT_MAX, -1);
  if (b >= l && r >= e) return dp[idx];
  int mid = (b + e) / 2;
  return min(query(l, r, idx * 2, b, mid),
             query(l, r, idx * 2 + 1, mid + 1, e));
}
vector<pair<int, int> > v;
void f(int l = 1, int r = n, int cur = 0) {
  if (l > r) return;
  pair<int, int> mn = query(l, r);
  for (int i = 0; i < (mn.first - cur); i++) v.push_back(pair<int, int>(l, r));
  f(l, mn.second - 1, mn.first);
  f(mn.second + 1, r, mn.first);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  build();
  f();
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++) {
    printf("%d %d\n", v[i].first, v[i].second);
  }
}
