#include <bits/stdc++.h>
using namespace std;
template <typename T>
T POW(T B, T printf) {
  if (printf == 0) return 1;
  if (printf & 1)
    return B * POW(B, printf - 1);
  else
    return (POW(B, printf / 2) * POW(B, printf / 2));
}
template <typename T>
T BIGMOD(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
T DIS(T x1, T y1, T x2, T y2) {
  return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
T ANGLE(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T>
T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
T GCD(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
T LCM(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <typename T>
T EUCLIDE(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y);
}
int Check(int N, int pos) { return (N & (1 << pos)); }
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
double DEG(double x) { return (180.0 * x) / ((2.0 * acos(0.0))); }
void P_ARR(int *ar, int a, int b) {
  if (a > b) swap(a, b);
  if (a <= b) cout << ar[a];
  for (int i = a + 1; i <= b; i++) cout << " " << ar[i];
  cout << endl;
}
void P_BIN(int n) {
  if (n == 0) return;
  P_BIN(n / 2);
  printf("%d", n % 2);
}
int nxt[500007], prv[500007];
int ar[500007], aa[500007];
struct data {
  int id, mxval;
  data() {}
} tree[4 * 500007];
void build(int idx, int s, int e) {
  if (s == e) {
    tree[idx].mxval = ar[s];
    tree[idx].id = s;
    return;
  }
  int mid = (s + e) >> 1, l = (idx << 1), r = l | 1;
  build(l, s, mid);
  build(r, mid + 1, e);
  if (tree[l].mxval < tree[r].mxval)
    tree[idx] = tree[l];
  else
    tree[idx] = tree[r];
  return;
}
void update(int idx, int s, int e, int pos, int val) {
  if (s == e) {
    tree[idx].mxval = val;
    return;
  }
  int mid = (s + e) >> 1, l = (idx << 1), r = l | 1;
  if (pos <= mid)
    update(l, s, mid, pos, val);
  else
    update(r, mid + 1, e, pos, val);
  if (tree[l].mxval < tree[r].mxval)
    tree[idx] = tree[l];
  else
    tree[idx] = tree[r];
}
void show(int idx, int s, int e) {
  cout << s << " *** " << e << endl;
  if (s == e) {
    cout << tree[idx].mxval << " val " << endl;
    return;
  }
  int mid = (s + e) >> 1, l = (idx << 1), r = l | 1;
  show(l, s, mid);
  show(r, mid + 1, e);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int tc, cas = 1;
  int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y,
      z;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> ar[i];
  for (i = 1; i <= n; i++) {
    nxt[i] = i + 1;
    prv[i] = i - 1;
  }
  for (i = 1; i <= n; i++) aa[i] = min(ar[nxt[i]], ar[prv[i]]);
  build(1, 1, n);
  c = n - 2;
  long long int ans = 0;
  for (i = 1; i <= c; i++) {
    data tt = tree[1];
    x = prv[tt.id];
    y = nxt[tt.id];
    if (min(ar[x], ar[y]) == 0)
      ans += ar[tt.id];
    else
      ans += min(ar[x], ar[y]);
    nxt[x] = y;
    prv[y] = x;
    update(1, 1, n, tt.id, 1000000000);
  }
  cout << ans << endl;
  return 0;
}
