func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 500005;   const int inf = 100000000;   struct elem {     long long val;     int l, r;   };   struct V {     long long val;     int ind;   };   bool compare(V x, V y) {     if (x.val < y.val) return true;     if (x.val == y.val && x.ind < y.ind) return true;     return false;   }   V a[MAXN];   elem L[MAXN];   int main() {     int N;     cin >> N;     for (int i = 1; i <= N; i++) {       cin >> a[i].val;       a[i].ind = i;       L[i].val = a[i].val;       L[i].l = i - 1;       L[i].r = i + 1;     }     int GlobalLeft = 1;     int GlobalRight = N;     sort(a + 1, a + N + 1, compare);     long long ans = 0;     a[N + 1].val = inf;     int pntL = 1, pntR = 1;     int Left = N;     while (pntL <= N && Left > 2) {       pntR = pntL;       while (a[pntR].val == a[pntL].val) pntR++;       int NextL = pntR;       pntR--;       while (pntL <= pntR && a[pntL].ind == GlobalLeft && Left > 2) {         ans += L[GlobalLeft].val;         GlobalLeft = L[GlobalLeft].r;         pntL++;         Left--;       }       while (pntL <= pntR && a[pntR].ind == GlobalRight && Left > 2) {         ans += L[GlobalRight].val;         GlobalRight = L[GlobalRight].l;         pntR--;         Left--;       }       while (pntL <= pntR && Left > 2) {         ans += min(L[L[a[pntL].ind].l].val, L[L[a[pntL].ind].r].val);         L[L[a[pntL].ind].l].r = L[a[pntL].ind].r;         L[L[a[pntL].ind].r].l = L[a[pntL].ind].l;         pntL++;         Left--;       }       pntL = NextL;     }     cout << ans << endl;     return 0;   }   