func_code_string: #include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math )   long long comb(long long n, long long r) {     if (r < 0 || r > n) return 0;     if ((n - r) < r) r = n - r;     long long vec = 1;     for (long long i = n; i > n - r; --i) {       vec = vec * i;     }     for (long long i = 1; i < r + 1; ++i) {       vec = vec / i;     }     return vec;   }   template <typename T>   void press(T &v) {     v.erase(unique(v.begin(), v.end()), v.end());   }   long long small(vector<long long> &vec, long long first) {     return lower_bound(vec.begin(), vec.end(), first) - vec.begin();   }   long long osmall(vector<long long> &vec, long long first) {     return upper_bound(vec.begin(), vec.end(), first) - vec.begin();   }   long long big(vector<long long> &vec, long long first) {     return vec.size() - osmall(vec, first);   }   long long obig(vector<long long> &vec, long long first) {     return vec.size() - small(vec, first);   }   long long freq(vector<long long> &vec, long long first) {     return upper_bound(vec.begin(), vec.end(), first) -            lower_bound(vec.begin(), vec.end(), first);   }   long long maxa(vector<long long> &vec) {     return max_element(vec.begin(), vec.end()) - vec.begin();   }   long long mina(vector<long long> &vec) {     return min_element(vec.begin(), vec.end()) - vec.begin();   }   long long Set(long long qq, long long pos) { return (qq | (1 << pos)); }   long long chk(long long qq, long long pos) { return (qq & (1 << pos)); }   long long bits(long long qq) { return __builtin_popcount(qq); }   long long p2[32];   long long modpow(long long first, long long n, long long m) {     if (n == 0) return 1 % m;     long long u = modpow(first, n / 2, m);     u = (u * u) % m;     if (n % 2 == 1) u = (u * first) % m;     return u;   }   long long _pow(long long vec, long long b) {     if (!b) return 1;     long long temp = _pow(vec, b / 2);     temp = (temp * temp);     if (b % 2) return (vec * temp);     return temp;   }   long long cl(long long vec, long long b) {     if (vec % b) return 1 + vec / b;     return vec / b;   }   long long dx[] = {0, 0, -1, 1};   long long dy[] = {1, -1, 0, 0};   const long long MOD = 1e9 + 7;   const long long inf = 1e18;   const long long N = 1e4 + 5;   const long long K = 1e3 + 10;   void solve() {     vector<long long> v;     long long n, ans = 0;     cin >> n;     if (n == 1) {       cin >> n;       cout << 0 << endl;       return;     }     for (long long i = 0; i < n; i++) {       long long val;       long long s = v.size();       cin >> val;       while (s > 1 && v[s - 1] <= val && v[s - 1] <= v[s - 2]) {         ans += min(val, v[s - 2]);         v.pop_back();         s--;       }       v.push_back(val);     }     sort(v.begin(), v.end());     for (long long i = 0; i < v.size() - 2; i++) ans += v[i];     cout << ans << endl;   }   signed main() {     for (long long i = 0; i < 32; i++) p2[i] = (1 << i);     cout << fixed << setprecision(12);     long long t;     t = 1;     for (long long i = 1; i <= t; i++) {       solve();     }   }   