Language: C++

//Relive your past life.
//Face your demons.
//The past is never dead,it is not even past.
//The memories are not only the key to the past but...also to the future.
//coded in Rusty Lake
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define ll   long long
#define pb   push_back
#define mp   make_pair
#define orz  100000000000007ll
using namespace std;
int n,p[20];
ll a[20],ans=orz,b[20],res;
ll solve(){
    for(int i=1;i<=n;++i)b[i]=a[i];
    for(int i=1;i<=n-2;++i){
        b[p[i]-1]+=b[p[i]];
        b[p[i]]+=b[p[i]+1];
        for(int j=p[i]+1;j<=n-i;++j)b[j]=b[j+1];
    }
    return b[1]+b[2];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%lld",a+i);
    for(int _=1;_;--_){
        for(int i=1;i<=n;++i)b[i]=a[i];
        for(int k=n;k>2;--k){
            for(int i=2;i<k;++i)p[i-1]=i;
            random_shuffle(p+1,p+k-1);
            pair<ll,ll> P=mp(orz,orz),Q;
            int o=0;
            for(int j=1;j<k-1;++j){
                Q=mp(b[p[j]],min(b[p[j]-1],b[p[j]+1]));
                if(Q<P)P=Q,o=p[j];
            }
            b[o-1]+=b[o];
            b[o]+=b[o+1];
            for(int i=o+1;i<k;++i)b[i]=b[i+1];
        }
        ans=min(ans,b[1]+b[2]);
    }
    for(int i=1;i<=n-2;++i)p[i]=2;
    if(n<=3){
        printf("%lld\n",solve());
        return 0;
    }
    res=solve();
    for(int _=100000;_;--_){
        int k=rand()%(n-3)+1;
        int o=rand()%(n-1-k)+2;
        if(o==p[k]){
            ++_;
            continue;
        }
        swap(p[k],o);
        ll _=solve();
        if(_>res)p[k]=o;
        else res=_;
    }
    printf("%lld\n",min(res,ans));
   // system("pause");
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:84:27: warning: 'void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = int*]' is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
   84 |             random_shuffle(p+1,p+k-1);
      |             ~~~~~~~~~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:9:
/usr/include/c++/14/bits/stl_algo.h:4492:5: note: declared here
 4492 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~
a.cc:111:12: warning: name-independent declarations only available with '-std=c++2c' or '-std=gnu++2c' [-Wc++26-extensions]
  111 |         ll _=solve();
      |            ^
a.cc:112:12: error: reference to '_' is ambiguous
  112 |         if(_>res)p[k]=o;
      |            ^
a.cc:111:12: note: candidates are: 'long long int _'
  111 |         ll _=solve();
      |            ^
a.cc:103:13: note:                 'int _'
  103 |     for(int _=100000;_;--_){
      |             ^
a.cc:113:18: error: reference to '_' is ambiguous
  113 |         else res=_;
      |                  ^
a.cc:111:12: note: candidates are: 'long long int _'
  111 |         ll _=solve();
      |            ^
a.cc:103:13: note:                 'int _'
  103 |     for(int _=100000;_;--_){
      |             ^