#include <bits/stdc++.h>
using namespace std;
struct node {
  int vl;
  int l, r;
  node *left, *right;
} typedef node;
long long int seq[600000];
node *root;
int query(node *cur, int l, int r) {
  if (r < cur->l || l > cur->r) return -1;
  if (cur->l >= l && cur->r <= r) return cur->vl;
  int vl = query(cur->left, l, r);
  if (vl == -1) return query(cur->right, l, r);
  int vr = query(cur->right, l, r);
  if (vr != -1 && seq[vr] > seq[vl]) return vr;
  return vl;
}
long long int calc(int l, int r) {
  if (r - l <= 1) return 0;
  int p = query(root, l + 1, r - 1);
  return calc(l, p) + calc(p, r) +
         min(seq[query(root, l, p - 1)], seq[query(root, p + 1, r)]);
}
void build(node *cur, int l, int r) {
  cur->l = l;
  cur->r = r;
  if (l == r) {
    cur->vl = l;
    return;
  }
  int m = (l + r) / 2;
  cur->left = new node();
  cur->right = new node();
  build(cur->left, l, m);
  build(cur->right, m + 1, r);
  cur->vl = cur->left->vl;
  if (seq[cur->right->vl] > seq[cur->left->vl]) cur->vl = cur->right->vl;
}
int main() {
  int n, i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%I64d", &seq[i]);
  root = new node();
  build(root, 0, n - 1);
  printf("%I64d\n", calc(0, n - 1));
  return 0;
}
