#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000,100000000000")
const long long INF = 1000000007;
const long double cp = 2 * asin(1.0);
const long double eps = 1e-9;
const long long mod = 1000000007;
using namespace std;
struct el {
  int value;
  int ind;
};
bool cmp(el a, el b) { return a.value < b.value; }
int main() {
  int n, val;
  cin >> n;
  vector<el> v(n);
  map<int, int> m;
  for (int(i) = 0; (i) < (n); i++) {
    cin >> val;
    el e = {val, i};
    v[i] = e;
    m[i] = val;
  }
  sort((v).begin(), (v).end(), &cmp);
  long long ans = 0;
  for (int(i) = 0; (i) < (n); i++) {
    map<int, int>::const_iterator pos = m.find(v[i].ind);
    if (pos != m.begin() && pos != --m.end())
      ans += min(prev(pos)->second, next(pos)->second);
    else if (m.size() > 2)
      ans += v[i].value;
    m.erase(pos);
  }
  cout << ans;
  return 0;
}
