#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]" << endl; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int N = 100005;
int a[N];
int tr[4 * N];
vector<pair<int, int>> ans;
vector<int> v[N];
int n;
void build(int no, int l, int r) {
  if (l == r) {
    tr[no] = a[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(no << 1, l, mid);
  build(no << 1 | 1, mid + 1, r);
  tr[no] = min(tr[no << 1], tr[no << 1 | 1]);
}
int qry(int no, int l, int r, int st, int en) {
  if (l > en || r < st) return 1e9;
  if (l >= st && r <= en) return tr[no];
  int mid = (l + r) >> 1;
  return min(qry(no << 1, l, mid, st, en),
             qry(no << 1 | 1, mid + 1, r, st, en));
}
void f(int l, int r, int minus) {
  if (l > r) return;
  int mi = qry(1, 1, n, l, r);
  for (int i = 0; i < mi - minus; i++) ans.push_back({l, r});
  int idx = lower_bound(v[mi].begin(), v[mi].end(), l) - v[mi].begin();
  vector<int> ff;
  while (idx < (int)(v[mi].size()) && v[mi][idx] >= l && v[mi][idx] <= r)
    ff.push_back(v[mi][idx++]);
  for (auto x : ff) {
    f(l, x - 1, mi);
    l = x + 1;
  }
  f(l, r, mi);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    v[a[i]].push_back(i);
  }
  build(1, 1, n);
  int f1 = 0;
  int l = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (a[i]) {
      if (!f1) l = i;
      f1++;
    } else {
      if (f1) f(l, i - 1, 0);
      f1 = 0;
    }
  }
  cout << (int)(ans.size()) << "\n";
  for (auto p : ans) {
    cout << p.first << " " << p.second << "\n";
  }
  return 0;
}
