#include <bits/stdc++.h>
using namespace std;
char BUFFER[100000 + 5];
bool readn(int &n) { return scanf("%d", &n) == 1; }
bool readl(long long &n) { return scanf("%I64d", &n) == 1; }
bool readd(double &n) { return scanf("%lf", &n) == 1; }
bool reads(string &s) {
  s = "";
  int n = scanf("%s", BUFFER);
  if (n == 1) s = BUFFER;
  return n == 1;
}
bool readln(string &s) {
  char *valid = gets(BUFFER);
  if (valid) s = BUFFER;
  return ((bool)valid);
}
const int maxn = 1e6 + 10;
set<pair<int, int> > st;
vector<int> A[maxn];
int main() {
  int i, j, k, n, m;
  st.clear();
  for (i = 0; i < maxn; ++i) A[i].clear();
  long long ans = 0;
  cin >> n;
  set<pair<int, int> >::iterator p1, p2, p;
  for (i = 0; i < n; ++i) {
    readn(m);
    st.insert(make_pair(i, m));
    A[m].push_back(i);
  }
  for (i = 0; i < maxn; ++i) {
    if (((int)st.size()) < 3) break;
    for (j = 0; j < ((int)A[i].size()); ++j) {
      if (((int)st.size()) < 3) break;
      k = A[i][j];
      p1 = p2 = p = st.find(make_pair(k, i));
      if (p == st.begin() || p == (--st.end())) {
        ans += p->second;
        st.erase(p);
        continue;
      }
      p1--;
      p2++;
      ans += min(p1->second, p2->second);
      st.erase(p);
    }
  }
  cout << ans << endl;
  return 0;
}
