#include <bits/stdc++.h>
using namespace std;
const int M = 500 * 1000 + 4, Inf = 1e9 + 10;
int a[M], b[M], nx[M], pr[M];
bool cmp(int x, int y) { return (a[x] < a[y]); }
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], b[i] = i, nx[i] = i + 1, pr[i] = i - 1;
  long long ans = 0;
  sort(b + 1, b + n + 1, cmp);
  int mn = 0;
  for (int i = 1; i <= n; i++) {
    int tmp = ans;
    int j = b[i];
    ans += (a[j] - mn) * max(n - i - 1, 0);
    mn = a[j];
    ans += max(min(a[pr[j]], a[nx[j]]) - mn, 0);
    pr[nx[j]] = pr[j];
    nx[pr[j]] = nx[j];
  }
  cout << ans << endl;
  return 0;
}
