#include <bits/stdc++.h>
using namespace std;
const int N = 500001;
int n, a[N], pre[N], aft[N];
set<pair<int, int>> s;
long long ans;
int main() {
  std::ios::sync_with_stdio(false);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    s.insert(make_pair(a[i], i));
    pre[i] = i - 1;
    aft[i] = i + 1;
  }
  aft[n - 1] = -1;
  ans = 0;
  int sz = s.size();
  for (set<pair<int, int>>::iterator it = s.begin(); it != s.end(); it++) {
    if (sz <= 2) break;
    if (pre[it->second] != -1 && aft[it->second] != -1) {
      ans += (long long)min(a[pre[it->second]], a[aft[it->second]]);
      pre[aft[it->second]] = pre[it->second];
      aft[pre[it->second]] = aft[it->second];
    } else {
      ans += it->first;
      if (pre[it->second] != -1) aft[pre[it->second]] = aft[it->second];
      if (aft[it->second] != -1) pre[aft[it->second]] = pre[it->second];
    }
    sz--;
  }
  printf("%lld\n", ans);
  return 0;
}
