#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
struct trip {
  int a, b, c;
  trip(int a, int b, int c) : a(a), b(b), c(c) {}
  bool operator<(const trip& o) const { return a < o.a; }
};
struct trip2 {
  int a, b, c;
  trip2(int a, int b, int c) : a(a), b(b), c(c) {}
  bool operator<(const trip2& o) const {
    if (c == o.c) {
      return a < o.a;
    }
    return c > o.c;
  }
};
set<trip> st;
set<trip2> st2;
int a[MAXN], N;
vector<pair<int, int> > ans;
int main(int argc, char* argv[]) {
  scanf("%d", &N);
  for (int i = 0, _i = N; i < _i; ++i) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < N;) {
    int j;
    for (j = i; j < N && a[i] == a[j]; j++) {
      ;
    }
    st.insert(trip(i, j - 1, a[i]));
    st2.insert(trip2(i, j - 1, a[i]));
    i = j;
  }
  while (st2.size() > 1) {
    trip2 mx = *st2.begin();
    st2.erase(st2.find(mx));
    set<trip>::iterator mid = st.find(trip(mx.a, mx.b, mx.c));
    set<trip>::iterator le = mid, ri = mid;
    ++ri;
    int mxval = 0;
    int newlo = mx.a, newhi = mx.b;
    bool remr = false, reml = false;
    if (ri != st.end()) {
      newhi = ri->b;
      mxval = ri->c;
      remr = true;
    }
    if (le != st.begin()) {
      --le;
      if (mxval == le->c) {
        newlo = le->a;
        reml = true;
      } else if (le->c > mxval) {
        newhi = mx.b;
        newlo = le->a;
        mxval = le->c;
        remr = false;
        reml = true;
      }
    }
    if (remr) {
      st2.erase(st2.find(trip2(ri->a, ri->b, ri->c)));
      st.erase(ri);
    }
    if (reml) {
      st2.erase(st2.find(trip2(le->a, le->b, le->c)));
      st.erase(le);
    }
    for (int i = 0, _i = mid->c - mxval; i < _i; ++i) {
      ans.push_back(pair<int, int>(mid->a + 1, mid->b + 1));
    }
    st.erase(mid);
    st.insert(trip(newlo, newhi, mxval));
    st2.insert(trip2(newlo, newhi, mxval));
  }
  for (int i = 0, _i = st2.begin()->c; i < _i; ++i) {
    ans.push_back(pair<int, int>(st2.begin()->a + 1, st2.begin()->b + 1));
  }
  printf("%d\n", ans.size());
  for (int i = 0, _i = ans.size(); i < _i; ++i) {
    printf("%d %d\n", ans[i].first, ans[i].second);
  }
  return 0;
}
