#include <bits/stdc++.h>
using namespace std;
vector<int> a;
bool cmp(int i, int j) { return a[i] < a[j]; }
int main() {
  int n;
  scanf("%d", &n);
  a.resize(n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  set<int> s;
  for (int i = 0; i < n; i++) s.insert(i);
  vector<int> v;
  for (int i = 0; i < n; i++) v.push_back(i);
  sort(v.begin(), v.end(), cmp);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    set<int>::iterator it = s.find(v[i]), l, r;
    if (it == s.begin()) continue;
    l = r = it;
    l--;
    r++;
    if (r == s.end()) continue;
    if (a[*it] > min(a[*l], a[*r])) continue;
    ans += (long long)min(a[*l], a[*r]);
    s.erase(it);
  }
  int i;
  for (i = n - 1; i >= 0; i--)
    if (s.find(v[i]) != s.end()) break;
  for (i--; i >= 0; i--)
    if (s.find(v[i]) != s.end()) break;
  for (i--; i >= 0; i--)
    if (s.find(v[i]) != s.end()) ans += a[v[i]];
  printf("%I64d", ans);
  return 0;
}
