Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. 

After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.

Input

The first line contains a single integer n (1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai (1 ≤ ai ≤ 106) — the values of the array elements.

Output

In a single line print a single integer — the maximum number of points Artem can get.

Examples

Input

5
3 1 5 2 6


Output

11


Input

5
1 2 3 4 5


Output

6


Input

5
1 100 101 100 1


Output

102
#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];
}
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];
}
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;
  }
  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;
}
