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)

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N=4.8e6+2;
const int inf=1e9+9;

int n,a[25],cnt[N][25],dp[N];

template <typename _Tp> inline void IN(_Tp&x) {
    char ch;bool flag=0;x=0;
    while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    if(flag) x=-x;
}

inline int solve(int state) {
    if(!state) return 0;
    if(dp[state]!=2139062143) return dp[state];
    // int qaq=state;
    // while(qaq) printf("%d",qaq&1),qaq>>=1;
    // printf(":   ");
    int tot=0;
    for(int i=0;i<n;++i) if(state&(1<<i)) cnt[state][tot++]=i;
    if(tot==2) {return a[cnt[state][0]]+a[cnt[state][1]];}
    for(int i=1;i<tot-1;++i) {
        int nxts=state;
        nxts^=(1<<cnt[state][i]);
        a[cnt[state][i-1]]+=a[cnt[state][i]],a[cnt[state][i+1]]+=a[cnt[state][i]];
        // printf("\n %d -%d,%d> %d\n",state,cnt[i],i,nxts);
        dp[state]=min(solve(nxts),dp[state]);
        a[cnt[state][i-1]]-=a[cnt[state][i]],a[cnt[state][i+1]]-=a[cnt[state][i]];
    }return dp[state];
}

int main() {
    // freopen("D.in","r",stdin);
    // freopen("D.out","w",stdout);
    IN(n);
    memset(dp,127,sizeof(dp));
    // printf("hhw:  %d\n", dp[0]);
    for(int i=0;i<n;++i) IN(a[i]);
    int ans=dp[0];
    printf("%d\n",solve((1<<n)-1));
    /*for(int i=0;i<n;++i)
        for(int j=0;j<n;++j) if(i^j) {
            int state=(1<<i)|(1<<j);
            ans=min(ans,dp[state]);
        }
    printf("B:%d\n",dp[(1<<n)-1]);*/
    // for(int i=0;i<1<<n;++i) printf("%d\n",dp[i];
    return 0;
}