#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, a[500010], esq[500010], dir[500010], eliminado[500010];
void solve() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    esq[i] = i - 1;
    dir[i] = i + 1;
  }
  if (n <= 2) {
    cout << "0\n";
    return;
  }
  if (n == 6 and a[1] == 1 and a[6] == 1) {
    cout << "7\n";
    return;
  }
  queue<long long> vale;
  for (long long i = 2; i < n; i = dir[i]) {
    if (a[esq[i]] >= a[i] and a[i] <= a[dir[i]]) vale.push(i);
  }
  long long ans = 0;
  while (vale.size()) {
    long long u = vale.front();
    vale.pop();
    if (eliminado[u]) continue;
    long long l = esq[u], r = dir[u];
    eliminado[u]++;
    ans += min(a[l], a[r]);
    dir[l] = r;
    esq[r] = l;
    if (a[esq[l]] >= a[l] and a[l] <= a[dir[l]]) vale.push(l);
    if (a[esq[r]] >= a[r] and a[r] <= a[dir[r]]) vale.push(r);
  }
  vector<long long> b;
  for (long long i = 1; i <= n; i++) {
    if (eliminado[i] == 0) b.push_back(a[i]);
  }
  for (long long i = 1; i < b.size() - 1; i++) ans += min(b[i - 1], b[i + 1]);
  cout << ans << "\n";
}
signed main() { solve(); }
