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 (Python):

"""

https://atcoder.jp/contests/agc035/tasks/agc035_d

毎回1枚カードが減り、寄与が2倍になる
両端のカードは消せない → 寄与は1倍のまま

端から2枚目のカードしか選ばなくていいみたいなの無いかな？
端から2枚目を選ぶと、1つは端の寄与になり、もう一つは動の寄与になる
つまり、動の寄与の総和は変化しない
そうでないのを選ぶと、動の寄与に2倍かかる

→ 2**18 になっていい感じのオーダーにはなる

検証：
5枚で真ん中を最初に選ぶべきなのを作れるか？
0,X,Y,Z,0
0,X+Y,Y+Z,0
0,X+2Y+Z,Y+Z
X+2Y+Z,X+3Y+2Z
→2X+3Y+3Z or 3X+3Y+2Z (真ん中を選んだ場合)

0,X,Y,Z,0
X,X+Y,Z,0 → X,X+Y+Z,Z → 2X+Y+Z,X+Y+2Z → 3X+2Y+3Z
2X+Y,X+Y+Z,0 → 3X+2Y+Z,X+Y+Z → 4X+3Y+2Z

すなわち、真ん中を選んだ方がいい場合もある…
最後まで残した数は2倍だけの寄与で済む
最後まで残す数を決め打ちする？
最後の数で左右を分ける
→さらにその区間で最後に残すやつは3倍の寄与で済む

1 4 3 5 2 5 3 4 1
→こんな感じ

あとは分割統治で、
ある点で区切った場合の左右の最小を足して上に返せばよい
計算量は？
長さ1の場合1
2の場合 1+1 = 2
3の場合 2+1+1+2 = 6
4の場合 6+

dp[0] = 0
dp[1] = 1
dp[n] = 2*(dp[n-1]+dp[n-2]…+dp[0])
dp[16] = 9565938 なので十分可能

"""

from sys import stdin

def want(l,r,lb,rb):
    if l > r:
        return 0
    elif l == r:
        return A[l] * (lb+rb)
    
    nmin = float("inf")
    for i in range(l,r+1):
        nmin = min(nmin , A[i]*(lb+rb) + want(l,i-1,lb,lb+rb) + want(i+1,r,lb+rb,rb) )
    return nmin
    

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))

print (A[0] + A[-1] + want(1,N-2,1,1) )

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

memo={}
def dp(i,j,L,R):
    if (i,j,L,R) in memo:
        return memo[(i,j,L,R)]
    if i==j:
        memo[(i,j,L,R)]=0
        memo[(i,j,L,R)]=(L+R)*A[i]
        return (L+R)*A[i]
    elif i>j:
        return 0

    ans=10**18
    for k in range(i,j+1):
        test=dp(i,k-1,L,L+R)+dp(k+1,j,L+R,R)+A[k]*(L+R)
        ans=min(ans,test)

    memo[(i,j,L,R)]=0
    memo[(i,j,L,R)]=ans
    return ans

res=dp(1,N-2,1,1)
print(res+A[0]+A[-1])

Solution (Python):
from functools import lru_cache
 
N = int(input())
A = list(map(int, input().split()))
 
@lru_cache(None)
def f(l, r, xl, xr):
  if l+1 == r:
    return xl * A[l] + xr * A[r]
  tmp = float("inf")
  for m in range(l+1, r):
    v = f(l, m, xl, xl+xr) + f(m, r, xl+xr, xr) - (xl+xr) * A[m]
    tmp = min(tmp, v)
  return tmp
 
print(f(0, N-1, 1, 1))

Solution (Python):

import sys
sys.setrecursionlimit(10**6)

def kukan(l,r,el=1,er=1):
    em=el+er
    if l+1==r:
        return 0
    if l+2==r:
        return a[l+1]*em
    t=(l,r,el,er)
    if t in memo:
        return memo[t]
    re=10**11
    for m in range(l+1,r):
        tmp=kukan(l,m,el,em)+kukan(m,r,em,er)+a[m]*em
        if tmp<re:
            re=tmp
    memo[t]=re
    return re

n=int(input())
a=list(map(int,input().split()))
memo={}
print(a[0]+kukan(0,n-1)+a[-1])

Solution (Python):



from functools import lru_cache

N = int(input())
A = [int(i) for i in input().split()]

@lru_cache(maxsize=None)
def rec(l, r, xl, xr) :
    if l + 1 == r :
        return 0
    
    return min(rec(l, m, xl, xl+xr)+rec(m, r, xl+xr, xr)+A[m]*(xl+xr) for m in range(l+1, r))
    
print(A[0] + rec(0, N-1, 1, 1) + A[N-1])

Solution (Python):
from functools import lru_cache
N = int(input())
A = list(map(int, input().split()))

@lru_cache(maxsize=None)
def dfs(l, r, wl, wr):
    if r <= l + 1:
        return 0
    v = min(dfs(l, m, wl, wl+wr) + dfs(m, r, wl+wr, wr) + A[m] * (wl+wr) for m in range(l+1, r))
    return v

print(dfs(0, N-1, 1, 1) + A[0] + A[N-1])

Solution (Python):
from functools import lru_cache


def solve(n, aaa):
    @lru_cache(maxsize=None)
    def search_min(li, ri, lc, rc):
        w = ri - li
        if w == 1:
            return 0
        lrc = lc + rc
        if w == 2:
            return aaa[li + 1] * lrc
        if w == 3:
            a1, a2 = aaa[li + 1], aaa[li + 2]
            return (a1 + a2) * lrc + min(a1 * lc, a2 * rc)
        ret = min(aaa[i] * lrc + search_min(li, i, lc, lrc) + search_min(i, ri, lrc, rc) for i in range(li + 1, ri))
        return ret

    return search_min(0, n - 1, 1, 1) + aaa[0] + aaa[-1]


n = int(input())
aaa = list(map(int, input().split()))
print(solve(n, aaa))

Solution (Python):
from functools import lru_cache


def solve(n, aaa):
    @lru_cache(maxsize=None)
    def search_min(li, ri, lc, rc):
        w = ri - li
        base = aaa[li] * lc
        if w == 1:
            return base
        lrc = lc + rc
        if w == 2:
            return base + aaa[li + 1] * lrc
        if w == 3:
            a1, a2 = aaa[li + 1], aaa[li + 2]
            return base + (a1 + a2) * lrc + min(a1 * lc, a2 * rc)
        ret = min(search_min(li, i, lc, lrc) + search_min(i, ri, lrc, rc) for i in range(li + 1, ri))
        return ret

    return search_min(0, n - 1, 1, 1) + aaa[-1]


n = int(input())
aaa = list(map(int, input().split()))
print(solve(n, aaa))

Solution (Python):
from functools import lru_cache

n, a = int(input()), list(map(int, input().split()))

@lru_cache(maxsize=None)
def f(x, y, l, r):
    if r - l + 1 <= 2:
        return 0
    return min(a[i] * (x + y) + f(x, x + y, l, i) + f(x + y, y, i, r) for i in range(l + 1 , r))

print(a[0] + a[-1] + f(1, 1, 0, n - 1))