#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y);
bool isPrime(long long n);
long long modInv(long long a, long long b);
long long gcdExtended(long long a, long long b, long long *x, long long *y);
long long mpower(long long a, long long b, long long p);
bool sorted(vector<long long> v, long long n, long long strict);
const long long inf = (long long)1e17;
const long long N = (long long)3e5 + 5;
long long tree[N], v[N], n;
void build(long long ss, long long se, long long i) {
  if (ss == se) {
    tree[i] = v[ss];
    return;
  }
  long long mid = (ss + se) >> 1;
  build(ss, mid, 2 * i);
  build(mid + 1, se, 2 * i + 1);
  tree[i] = min(tree[2 * i], tree[2 * i + 1]);
}
long long query(long long ss, long long se, long long l, long long r,
                long long i) {
  if (l > se || r < ss) return inf;
  if (l <= ss && r >= se) {
    return tree[i];
  }
  long long mid = (ss + se) >> 1;
  return min(query(ss, mid, l, r, 2 * i), query(mid + 1, se, l, r, 2 * i + 1));
}
struct range {
  long long l, r, cnt;
};
range make_range(long long L, long long R, long long c) {
  range r;
  r.l = L;
  r.r = R;
  r.cnt = c;
  return r;
}
vector<range> ran;
vector<long long> pos[N];
void rec(long long l, long long r, long long del) {
  long long minn = query(0, n - 1, l, r, 1);
  if (minn - del) ran.push_back(make_range(l, r, minn - del));
  long long i = 1;
  auto lb = lower_bound(pos[minn].begin(), pos[minn].end(), l);
  auto ub = --upper_bound(pos[minn].begin(), pos[minn].end(), r);
  long long st = *lb, ed = *ub;
  i = lb - pos[minn].begin();
  i++;
  while (i <= (ub - pos[minn].begin())) {
    long long nl = pos[minn][i - 1] + 1;
    long long nr = pos[minn][i] - 1;
    if (nl <= nr && nl >= l && nr <= r) {
      rec(nl, nr, minn);
    }
    i++;
  }
  if (l <= st - 1) {
    rec(l, st - 1, minn);
  }
  if (ed + 1 <= r) {
    rec(ed + 1, r, minn);
  }
}
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
    pos[v[i]].push_back(i);
  }
  build(0, n - 1, 1);
  long long m = tree[1];
  rec(0, n - 1, 0);
  long long size = 0;
  for (auto i : ran) {
    size += i.cnt;
  }
  cout << size << endl;
  for (auto i : ran) {
    long long C = i.cnt;
    while (C > 0) {
      cout << i.l + 1 << ' ' << i.r + 1 << endl;
      C--;
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  clock_t start, end;
  start = clock();
  solve();
  end = clock();
  double time_taken = double(end - start) / (double)CLOCKS_PER_SEC;
  return 0;
}
long long modInv(long long a, long long m) {
  long long x, y;
  long long g = gcdExtended(a, m, &x, &y);
  long long res = (x % m + m) % m;
  return res;
}
long long gcdExtended(long long a, long long b, long long *x, long long *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
long long mpower(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  long long p = sqrt(n);
  for (long long i = 5; i <= p; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
bool sorted(vector<long long> v, long long n, long long strict) {
  if (n <= 1) return 1;
  for (long long i = 1; i < n; i++) {
    if (strict) {
      if (v[i] <= v[i - 1]) return 0;
    } else {
      if (v[i] < v[i - 1]) return 0;
    }
  }
  return 1;
}
