#include <bits/stdc++.h>
using namespace std;
int n, a[500010], nb;
pair<int, int> link[500010], b[500010];
long long res;
bool removed[500010];
void remove(int i) {
  link[link[i].first].second = link[i].second;
  link[link[i].second].first = link[i].first;
  removed[i] = true;
}
bool hole(int i) {
  return a[link[i].first] >= a[i] && a[i] <= a[link[i].second];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  link[1].second = 2;
  link[n].first = n - 1;
  for (int i = 2; i <= n - 1; i++) {
    link[i] = make_pair(i - 1, i + 1);
  }
  for (int i = 2; i <= n - 1; i++) {
    if (!removed[i] && hole(i)) {
      int x = i;
      while (x != -1) {
        res += min(a[link[x].first], a[link[x].second]);
        remove(x);
        if (link[x].first > 1 && hole(link[x].first))
          x = link[x].first;
        else if (link[x].second < n && hole(link[x].second))
          x = link[x].second;
        else
          x = -1;
      }
    }
  }
  int x = link[1].second;
  while (x < n) {
    res += min(a[link[x].first], a[link[x].second]);
    x = link[x].second;
  }
  printf("%I64d", res);
}
