#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 20;
int tav(long long n, long long k) {
  long long res = 1;
  while (k > 0) {
    if (k & 1) {
      res *= n;
    }
    n *= n;
    k /= 2;
  }
  return res;
}
vector<long long> v;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long h;
    cin >> h;
    if (v.size() < 2) {
      v.push_back(h);
    } else {
      while (v.size() >= 2) {
        if (v.back() <= h && v[v.size() - 2] >= v.back()) {
          v.pop_back();
          ans += min(h, v.back());
        } else {
          break;
        }
      }
      v.push_back(h);
    }
  }
  sort(v.begin(), v.end(), greater<long long>());
  while (v.size() > 2) {
    ans += v.back();
    v.pop_back();
  }
  cout << ans << '\n';
  return 0;
}
