#include <bits/stdc++.h>
using namespace std;
set<int> s;
set<pair<int, int> > s2;
inline bool bad(set<int>::iterator it) {
  if (it == s.begin()) return true;
  it++;
  if (it == s.end()) return true;
  return false;
}
inline int getBef(set<int>::iterator it) {
  it--;
  return *it;
}
inline int getNex(set<int>::iterator it) {
  it++;
  return *it;
}
int a[500010];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s.insert(i);
    s2.insert(pair<int, int>(a[i], i));
  }
  long long ans = 0;
  int lstBegin = 0, lstEnd = 0;
  for (int i = 0; i < n; i++) {
    set<int>::iterator it = s.lower_bound(s2.begin()->second);
    if (bad(it))
      if (s.size() == 1)
        ans += min(lstBegin, lstEnd);
      else if (it == s.begin()) {
        ans += lstBegin;
        lstBegin = s2.begin()->first;
      } else {
        ans += lstEnd;
        lstEnd = s2.begin()->first;
      }
    else
      ans += min(a[getBef(it)], a[getNex(it)]);
    s.erase(s2.begin()->second);
    s2.erase(s2.begin());
  }
  cout << ans << endl;
}
