#include <bits/stdc++.h>
using namespace std;
int mini[500000], first[500000];
int n, sz, d[500000];
pair<int, int> Y[200000];
int init(int root, int bas, int son) {
  if (bas == son) return mini[root] = d[bas];
  return mini[root] = min(init(root * 2, bas, (bas + son) / 2),
                          init(root * 2 + 1, (bas + son) / 2 + 1, son));
}
int update(int root, int bas, int son, int upbas, int upson, int val) {
  if (son < upbas or upson < bas) return mini[root];
  if (upbas <= bas and son <= upson) {
    first[root] += val;
    return mini[root] = mini[root] + val;
  }
  return mini[root] = min(
             update(root * 2, bas, (bas + son) / 2, upbas, upson, val),
             update(root * 2 + 1, (bas + son) / 2 + 1, son, upbas, upson, val));
}
int query(int root, int bas, int son, int qbas, int qson) {
  if (son < qbas or qson < bas) return 9999999;
  if (qbas <= bas and son <= qson) return mini[root];
  return min(query(root * 2, bas, (bas + son) / 2, qbas, qson),
             query(root * 2 + 1, (bas + son) / 2 + 1, son, qbas, qson)) +
         first[root];
}
int bul(int bas, int son) {
  if (bas == son) return bas;
  if (query(1, 1, n, bas, (bas + son) / 2) == 0)
    return bul(bas, (bas + son) / 2);
  return bul((bas + son) / 2 + 1, son);
}
void dfs(int bas, int son) {
  if (bas > son) return;
  int t = query(1, 1, n, bas, son);
  update(1, 1, n, bas, son, -t);
  int k = bul(bas, son);
  for (int i = 1; i <= t; i++) Y[++sz] = make_pair(bas, son);
  dfs(bas, k - 1);
  dfs(k + 1, son);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> d[i];
  init(1, 1, n);
  dfs(1, n);
  cout << sz << endl;
  for (int i = 1; i <= sz; i++)
    cout << Y[i].first << " " << Y[i].second << endl;
}
