#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30);
int t[4 * 100002], n;
vector<int> v[100005];
void update(int node, int s, int e, int i, int value) {
  if (s > i || e < i) return;
  if (s == e) {
    t[node] = value;
    return;
  }
  update(node * 2, s, (s + e) / 2, i, value);
  update(node * 2 + 1, (s + e) / 2 + 1, e, i, value);
  t[node] = min(t[node * 2], t[node * 2 + 1]);
}
int query(int node, int s, int e, int S, int E) {
  if (S > e || E < s) return INF;
  if (s >= S && e <= E) return t[node];
  return min(query(node * 2, s, (s + e) / 2, S, E),
             query(node * 2 + 1, (s + e) / 2 + 1, e, S, E));
}
vector<pair<int, int> > res;
void process(int i, int j, int done) {
  if (i > j) return;
  int mn = query(1, 0, n - 1, i, j);
  if (mn) {
    if (done == -1) done = 0;
    for (int k = done; k < mn; ++k) res.push_back(make_pair(i, j));
  }
  int ti = i, tj = j;
  done = mn;
  while (i <= j) {
    int low = 0, high = v[mn].size() - 1;
    int mid, id = -1;
    while (low <= high) {
      mid = (low + high) / 2;
      if (v[mn][mid] >= i) {
        id = v[mn][mid];
        high = mid - 1;
      } else
        low = mid + 1;
    }
    process(i, id - 1, done);
    process(id + 1, j, done);
    break;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < 4 * 100001; ++i) t[i] = INF;
  int a;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a);
    update(1, 0, n - 1, i, a);
    v[a].push_back(i);
  }
  process(0, n - 1, -1);
  printf("%d\n", res.size());
  for (int i = 0; i < res.size(); ++i)
    printf("%d %d\n", res[i].first + 1, res[i].second + 1);
  return 0;
}
