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: Accepted)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline int read()
{
	int s=0,f=1;char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const int maxn=20;

int n;

int a[maxn];

struct node {
	int l,r,fl,fr;
	node(int _l=0,int _r=0,int _fl=0,int _fr=0){ l=_l; r=_r; fl=_fl; fr=_fr;}
};

inline bool operator <(node a,node b)
{
	return a.l==b.l?a.r==b.r?a.fl==b.fl?a.fr<b.fr:a.fl<b.fl:a.r<b.r:a.l<b.l;
}

inline bool operator ==(node a,node b){ return a.l==b.l && a.r==b.r && a.fl==b.fl && a.fr==b.fr;}

inline void init()
{
	n=read();
	REP(i,1,n)a[i]=read();
}

map<node,ll>mp;

ll dfs(int l,int r,int fl,int fr)
{
	if(l==r-1)return 0;
	if(l+1==r-1)return 1ll*(fl+fr)*a[l+1];
	node x(l,r,fl,fr);
	if(mp.count(x))return mp[x];
	ll ans=1ll<<60;
	REP(k,l+1,r-1)
	{
		ll num=1ll*(fl+fr)*a[k];
		chkmin(ans,num+dfs(l,k,fl,fl+fr)+dfs(k,r,fl+fr,fr));
	}
	return mp[x]=ans;
}

inline void doing()
{
	printf("%lld\n",dfs(1,n,1,1)+a[1]+a[n]);
}

int main()
{
	init();
	doing();
	return 0;
}