#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
  int val;
  TreeNode* left;
  TreeNode* right;
  TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct ListNode {
  int val;
  ListNode* next;
  ListNode(int x) : val(x), next(NULL) {}
};
int nxt() {
  int x;
  cin >> x;
  return x;
}
long long nxtLong() {
  long long x;
  cin >> x;
  return x;
}
string nxtStr() {
  string x;
  cin >> x;
  return x;
}
char nxtChar() {
  char x;
  cin >> x;
  return x;
}
double nxtDouble() {
  double x;
  cin >> x;
  return x;
}
long double nxtLDouble() {
  long double x;
  cin >> x;
  return x;
}
TreeNode* makeTree(int n) {
  queue<TreeNode*> tree_nodes;
  TreeNode* root;
  if (n) {
    root = new TreeNode(nxt());
    tree_nodes.push(root);
  } else {
    return NULL;
  }
  while (!tree_nodes.empty()) {
    TreeNode* node = tree_nodes.front();
    tree_nodes.pop();
    bool left;
    cin >> left;
    if (left) {
      node->left = new TreeNode(nxt());
      tree_nodes.push(node->left);
    }
    bool right;
    cin >> right;
    if (right) {
      node->right = new TreeNode(nxt());
      tree_nodes.push(node->right);
    }
  }
  return root;
}
ListNode* makeLinkedList(int n) {
  ListNode* list = new ListNode(0);
  ListNode* dummy = list;
  for (int i = 0; i < n; ++i) {
    list->next = new ListNode(nxt());
    list = list->next;
  }
  return dummy->next;
}
void solve() {
  int n = nxt();
  vector<pair<int, int>> in(n + 1);
  vector<int> orig(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> in[i].first;
    in[i].second = i;
    orig[i] = in[i].first;
  }
  sort(in.begin() + 1, in.end(), [&](pair<int, int> a, pair<int, int> b) {
    if (a.first > b.first) {
      return true;
    } else {
      if (a.first == b.first) {
        if (a.second < b.second) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    }
  });
  vector<pair<int, int>> range(n + 1);
  vector<pair<pair<int, int>, int>> ans;
  for (int i = 1; i <= n; ++i) {
    range[i].first = i, range[i].second = i;
  }
  for (int i = 1; i <= n; ++i) {
    int cur_height = in[i].first, range_index = in[i].second;
    int l = range[range_index].first, r = range[range_index].second;
    int ma = INT_MIN;
    int min;
    if (l - 1 > 0) {
      ma = max(ma, orig[l - 1]);
      min = l - 1;
    }
    if (r + 1 <= n) {
      if (ma < orig[r + 1]) {
        ma = orig[r + 1];
        min = r + 1;
      } else {
        min = l - 1;
      }
    }
    if (ma != INT_MIN) {
      if (orig[min] == cur_height) {
        if (min == l - 1) {
          range[min].second = r;
        } else {
          range[min].first = l;
        }
      } else {
        ans.push_back({{l, r}, cur_height - orig[min]});
        if (min == l - 1) {
          range[min].second = r;
        } else {
          range[min].first = l;
        }
      }
    } else {
      ans.push_back({{l, r}, cur_height - 0});
      break;
    }
  }
  int to = 0;
  for (auto pr : ans) {
    to += pr.second;
  }
  cout << to << endl;
  for (auto pr : ans) {
    for (int i = 0; i < pr.second; ++i) {
      cout << pr.first.first << ' ' << pr.first.second << endl;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(9);
  solve();
  return 0;
}
