#include <bits/stdc++.h>
using namespace std;
int n, a[100005];
vector<pair<int, int> > ans;
int m[100005][18];
int query(int i, int j) {
  int k = (int)(log2(j - i + 1));
  if (a[m[i][k]] <= a[m[j - (1 << k) + 1][k]]) return m[i][k];
  return m[j - (1 << k) + 1][k];
}
void solve(int l, int r, int mi) {
  if (l > r) return;
  int id = query(l, r), vmi = a[id];
  for (int i = 0; i < vmi - mi; ++i)
    ans.push_back(pair<int, int>(l + 1, r + 1));
  solve(l, id - 1, vmi);
  solve(id + 1, r, vmi);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i) m[i][0] = i;
  for (int j = 1; (1 << j) <= n; ++j)
    for (int i = 0; i + (1 << j) - 1 < n; ++i)
      if (a[m[i][j - 1]] < a[m[i + (1 << (j - 1))][j - 1]])
        m[i][j] = m[i][j - 1];
      else
        m[i][j] = m[i + (1 << (j - 1))][j - 1];
  solve(0, n - 1, 0);
  printf("%d\n", int((ans).size()));
  for (int i = 0; i < int((ans).size()); ++i)
    printf("%d %d\n", ans[i].first, ans[i].second);
  return 0;
}
