#include <bits/stdc++.h>
using namespace std;
bool PIIfs(const pair<int, int>& a, const pair<int, int>& b) {
  return a.first < b.first || (a.first == b.first && a.second < b.second);
}
bool PIIsf(const pair<int, int>& a, const pair<int, int>& b) {
  return a.second < b.second || (a.second == b.second && a.first < b.first);
}
bool PIIFS(const pair<int, int>& a, const pair<int, int>& b) {
  return a.first > b.first || (a.first == b.first && a.second > b.second);
}
bool PIISF(const pair<int, int>& a, const pair<int, int>& b) {
  return a.second > b.second || (a.second == b.second && a.first > b.first);
}
bool PIIfS(const pair<int, int>& a, const pair<int, int>& b) {
  return a.first < b.first || (a.first == b.first && a.second > b.second);
}
bool PIIsF(const pair<int, int>& a, const pair<int, int>& b) {
  return a.second < b.second || (a.second == b.second && a.first > b.first);
}
bool PIIFs(const pair<int, int>& a, const pair<int, int>& b) {
  return a.first > b.first || (a.first == b.first && a.second < b.second);
}
bool PIISf(const pair<int, int>& a, const pair<int, int>& b) {
  return a.second > b.second || (a.second == b.second && a.first < b.first);
}
template <typename T>
inline T max(T a, T b, T c) {
  return max(max(a, b), c);
}
template <typename T>
inline T max(T a, T b, T c, T d) {
  return max(max(a, b, c), d);
}
template <typename T>
inline T max(T a, T b, T c, T d, T e) {
  return max(max(a, b, c, d), e);
}
template <typename T>
inline T min(T a, T b, T c) {
  return min(min(a, b), c);
}
template <typename T>
inline T min(T a, T b, T c, T d) {
  return min(min(a, b, c), d);
}
template <typename T>
inline T min(T a, T b, T c, T d, T e) {
  return min(min(a, b, c, d), e);
}
template <typename T>
inline void RD(T& x) {
  cin >> x;
}
template <typename T, typename U>
inline void RD(T& a, U& b) {
  return RD(a), RD(b);
}
template <typename T, typename U, typename V>
inline void RD(T& a, U& b, V& c) {
  return RD(a), RD(b), RD(c);
}
template <typename T, typename U, typename V, typename W>
inline void RD(T& a, U& b, V& c, W& d) {
  return RD(a), RD(b), RD(c), RD(d);
}
template <typename T, typename U, typename V, typename W, typename X>
inline void RD(T& a, U& b, V& c, W& d, X& e) {
  return RD(a), RD(b), RD(c), RD(d), RD(e);
}
vector<int> optima;
vector<int> dpoint;
int main() {
  std::ios::sync_with_stdio(false);
  int N;
  cin >> N;
  optima.push_back(0);
  long long sum = 0;
  int max1 = 0;
  int max2 = 0;
  int oldval = 0;
  int val = 0;
  bool incing = false;
  for (int i = 1; i <= N; i++) {
    cin >> val;
    if (val > max1) {
      max2 = max1;
      max1 = val;
    } else {
      max2 = max(max2, val);
    }
    sum += val;
    if (incing) {
      if (val < oldval) {
        optima.push_back(oldval);
        incing = false;
      } else {
      }
    } else {
      if (val > oldval) {
        incing = true;
        optima.push_back(oldval);
      } else {
      }
    }
    oldval = val;
  }
  if (incing) {
    optima.push_back(oldval);
  }
  if (N <= 2) {
    cout << "0\n";
    return 0;
  }
  int optima_size = optima.size();
  int half_size = optima_size >> 1;
  while (optima_size > 3) {
    for (int i = 0; i < (half_size); ++i) {
      sum += min(optima[2 * i], optima[2 * i + 2]) - optima[2 * i + 1];
    }
    vector<int> new_optima;
    new_optima.push_back(0);
    bool incing = false;
    int oldval = 0;
    int val = 0;
    for (int i = 0; i < optima_size; i += 2) {
      val = optima[i];
      if (incing) {
        if (val < oldval) {
          new_optima.push_back(oldval);
          incing = false;
        } else {
        }
      } else {
        if (val > oldval) {
          incing = true;
          new_optima.push_back(oldval);
        } else {
        }
      }
      oldval = val;
    }
    if (incing) {
      new_optima.push_back(oldval);
    }
    optima.swap(new_optima);
    optima_size = optima.size();
    half_size = optima_size >> 1;
  }
  sum -= max1;
  sum -= max2;
  cout << sum << '\n';
  return 0;
}
