#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 5, inf = 1e18;
long long n, a[maxn], ans;
vector<long long> vec;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    while (vec.size() > 1) {
      if (vec.back() <= a[i] && vec.back() <= vec[vec.size() - 2]) {
        vec.pop_back();
        ans += min(a[i], vec.back());
      } else
        break;
    }
    vec.push_back(a[i]);
  }
  for (long long i = 1; i < vec.size() - 1; i++)
    ans += min(vec[i - 1], vec[i + 1]);
  cout << ans << endl;
}
