#include <bits/stdc++.h>
using namespace std;
const int maxn = 5 * 1000 * 100 + 5;
int n;
long long a[maxn], l[maxn], r[maxn];
set<pair<long long, int> > second;
long long javab = 0;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0)
      l[i] = -1;
    else
      l[i] = i - 1;
    if (i == n - 1)
      r[i] = -1;
    else
      r[i] = i + 1;
    second.insert(make_pair(a[i], i));
  }
  int st = 0, x, y, en = n - 1;
  while ((int)second.size() > 2) {
    x = second.begin()->first;
    y = second.begin()->second;
    second.erase(second.begin());
    if (y == st) {
      javab += x;
      st = r[st];
    } else if (y == en) {
      javab += x;
      en = l[en];
    } else {
      javab += min(a[l[y]], a[r[y]]);
      r[l[y]] = r[y];
      l[r[y]] = l[y];
    }
  }
  cout << javab << endl;
  return 0;
}
