#include <bits/stdc++.h>
using namespace std;
int n, a[500005], L[500005], R[500005];
struct Node {
  int id, vlu;
  Node() { id = 0, vlu = INT_MAX; }
  Node(int _i, int _v) : id(_i), vlu(_v) {}
};
Node _merge(Node p, Node q) { return p.vlu < q.vlu ? p : q; }
Node tree[500005 * 4];
void build(int node, int lo, int hi) {
  if (lo == hi) {
    tree[node] = Node(lo, a[lo]);
    return;
  }
  int mid = lo + (hi - lo) / 2, lft = node << 1, rgt = lft | 1;
  build(lft, lo, mid);
  build(rgt, mid + 1, hi);
  tree[node] = _merge(tree[lft], tree[rgt]);
}
void update(int node, int lo, int hi, int pos) {
  if (lo == hi) {
    tree[node].vlu = INT_MAX;
    return;
  }
  int mid = lo + (hi - lo) / 2, lft = node << 1, rgt = lft | 1;
  if (pos <= mid)
    update(lft, lo, mid, pos);
  else
    update(rgt, mid + 1, hi, pos);
  tree[node] = _merge(tree[lft], tree[rgt]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i), L[i] = i - 1, R[i] = i + 1;
  build(1, 1, n);
  long long res = 0;
  int lft = 1, rgt = n;
  for (int i = 1; i <= n - 2; i++) {
    int id = tree[1].id;
    if (id == rgt) {
      res += tree[1].vlu, rgt = L[rgt];
    } else if (id == lft) {
      res += tree[1].vlu, lft = R[lft];
    } else {
      res += min(a[L[id]], a[R[id]]);
      R[L[id]] = R[id];
      L[R[id]] = L[id];
    }
    update(1, 1, n, id);
  }
  printf("%lld\n", res);
  return 0;
}
