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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define PR pair<LL,LL>
const int N=20;
LL a[N];
vector< PR > vp[N][N];
PR calpr(PR u,PR v,LL t){
    LL num=u.sc+v.fr+t;
    return mp(u.fr+num,v.sc+num);
}
LL calval(PR t,LL u,LL v){
    return t.fr*u+t.sc*v;
}
void pus(int n,int l,int r,int u1,int v1,int u2,int v2){
    PR m1,m2;
    LL val1=1e18,val2=1e18;
    int len=r-l+1;
    for (int k=l;k<=r;k++){
        int l1=k-l,l2=r-k;
        for (auto u:vp[l1][l]) for (auto v:vp[l2][k+1]){
            PR t=calpr(u,v,a[k]);
            LL valt1=calval(t,u1,v1);
            if (valt1<val1) m1=t,val1=valt1;
            if (len>n-2) continue;
            LL valt2=calval(t,u2,v2);
            if (valt2<val2) m2=t,val2=valt2;
        }
    }
    vp[len][l].pb(m1);
    if (len==n-2) vp[len][l].pb(m2);
}
void work(int n){
    pus(n,1,n-2,1,2,1,3);
    pus(n,2,n-1,2,3,3,2);
    pus(n,3,n,  2,1,3,1);

    pus(n,1,n-1,1,2,0,0);
    pus(n,2,n,  2,1,0,0);

    pus(n,1,n,  1,1,0,0);
}
int main(){
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int n;cin>>n;
    for (int i=0;i<n;i++) scanf("%d",a+i);
    LL ans=a[0]+a[n-1];n-=2;

    for (int i=1;i<=n;i++) vp[1][i].pb(mp(a[i],a[i])),vp[0][i].pb(mp(0,0));
    vp[0][n+1].pb(mp(0,0));

    for (int p=2;p<=n-3;p++){
        for (int i=1;i+p-1<=n;i++){
            int j=i+p-1;
            for (int k=i;k<=j;k++){
                int l1=k-i,l2=j-k;
                for (auto u:vp[l1][i]) for (auto v:vp[l2][k+1]){
                    vp[p][i].pb(calpr(u,v,a[k]));
                }
            }
        }
    }

    work(n);
    PR p=vp[n][1][0];
    ans+=p.fr+p.sc;
    cout<<ans<<endl;

    return 0;
}