Problem:
Score : 1000 points

Problem Statement

There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.

Snuke will repeat the following operation until two cards remain:

Choose three consecutive cards from the stack.

Eat the middle card of the three.

For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.

Return the two cards to the original position in the stack, without swapping them.

Find the minimum possible sum of the integers written on the last two cards remaining.

Constraints

2 \leq N \leq 18

0 \leq A_i \leq 10^9 (1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sum of the integers written on the last two cards remaining.

Sample Input 1

4
3 1 4 2

Sample Output 1

16

We can minimize the sum of the integers written on the last two cards remaining by doing as follows:

Initially, the integers written on the cards are 3, 1, 4, and 2 from top to bottom.

Choose the first, second, and third card from the top. Eat the second card with 1 written on it, add 1 to each of the other two cards, and return them to the original position in the stack. The integers written on the cards are now 4, 5, and 2 from top to bottom.

Choose the first, second, and third card from the top. Eat the second card with 5 written on it, add 5 to each of the other two cards, and return them to the original position in the stack. The integers written on the cards are now 9 and 7 from top to bottom.

The sum of the integers written on the last two cards remaining is 16.

Sample Input 2

6
5 2 4 1 6 9

Sample Output 2

51

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

115

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

//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 _=100;_;--_){
        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 _=2000000;_;--_){
        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;
}