#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9, mod = 1e9 + 7;
const int N = 5e5 + 5;
set<pair<int, int> > S;
vector<pair<int, int> > v;
int h[N], a[N], x, n;
long long ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    S.insert(make_pair(i, a[i]));
    v.push_back(make_pair(a[i], i));
  }
  set<pair<int, int> >::iterator it, it2, it3;
  sort(v.begin(), v.end());
  for (__typeof(v.begin()) c = v.begin(); c != v.end(); c++) {
    int index = c->second;
    if (index == 1 || index == n) continue;
    it2 = S.lower_bound(make_pair(index, 0));
    it = it2;
    it3 = it2;
    it--;
    it3++;
    int cost = min(it->second, it3->second);
    if (it2->second > cost) continue;
    ans += cost;
    S.erase(it2);
  }
  vector<int> v2;
  for (__typeof(S.begin()) it = S.begin(); it != S.end(); it++)
    v2.push_back(it->second);
  sort(v2.begin(), v2.end());
  for (int i = 0; i <= (int)v2.size() - 3; i++) ans += v2[i];
  cout << ans << '\n';
  return 0;
}
