#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const int INF = 1 << 30;
const double EPS = 1e-7;
int N;
vector<int> hpos[MAX];
int data[MAX];
bool mark[MAX];
int lo[MAX];
int hi[MAX];
set<int> groups;
vector<pair<int, int> > ans;
int find(int x) { return data[x] < 0 ? x : data[x] = find(data[x]); }
int getSize(int x) { return -data[find(x)]; }
void join(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (getSize(x) < getSize(y)) swap(x, y);
  lo[x] = min(lo[x], lo[y]);
  hi[x] = max(hi[x], hi[y]);
  data[x] += data[y];
  data[y] = x;
  groups.erase(y);
}
void add(int pos) {
  data[pos] = -1;
  groups.insert(pos);
  lo[pos] = pos;
  hi[pos] = pos;
  mark[pos] = 1;
  if (pos > 0 && mark[pos - 1]) join(pos, pos - 1);
  if (pos + 1 < N && mark[pos + 1]) join(pos, pos + 1);
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < (N); ++i) {
    int x;
    scanf("%d", &x);
    hpos[x].push_back(i);
  }
  for (int i = MAX - 1; i > 0; --i) {
    for (int j = 0; j < (hpos[i].size()); ++j) add(hpos[i][j]);
    for (typeof(groups.begin()) it = groups.begin(); it != groups.end(); ++it)
      ans.push_back(make_pair(lo[*it], hi[*it]));
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < (ans.size()); ++i)
    printf("%d %d\n", ans[i].first + 1, ans[i].second + 1);
  return 0;
}
