Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func nextFloat64() (float64, error) {
	return strconv.ParseFloat(nextString(), 64)
}

var infty = math.MaxFloat64

func optimalBst(p []float64, q []float64, n int) ([][]float64, [][]int) {
	e := make([][]float64, n+2)
	for i := 0; i <= n+1; i++ {
		e[i] = make([]float64, n+1)
	}
	w := make([][]float64, n+2)
	for i := 0; i <= n+1; i++ {
		w[i] = make([]float64, n+1)
	}
	root := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		root[i] = make([]int, n+1)
	}

	for i := 1; i <= n+1; i++ {
		e[i][i-1] = q[i-1]
		w[i][i-1] = q[i-1]
	}
	for l := 1; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			e[i][j] = infty
			w[i][j] = w[i][j-1] + p[j] + q[j]
			for r := i; r <= j; r++ {
				t := e[i][r-1] + e[r+1][j] + w[i][j]
				if t < e[i][j] {
					e[i][j] = t
					root[i][j] = r
				}
			}
		}
	}

	return e, root
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	p := make([]float64, n+1)
	q := make([]float64, n+1)
	for i := 1; i <= n; i++ {
		p[i], _ = nextFloat64()
	}
	for i := 0; i <= n; i++ {
		q[i], _ = nextFloat64()
	}

	// fmt.Println(p)
	// fmt.Println(q)

	e, _ := optimalBst(p, q, n)

	// fmt.Println(root)
	fmt.Println(e[1][n])
}

Python: n=int(input())
p=[float(i) for i in input().split()]
q=[float(i) for i in input().split()]
pq = [q[-1]]*(2*n+1)
dp = {}
for i in range(n):
    pq[2*i] = q[i]
    pq[2*i+1] = p[i]
pq = [0.0]+pq
for j in range(len(pq)):
    for i in range(len(pq)-j):
        if j == 0:
            dp[(i,i+j)] = 0.0
        elif j == 1:
            dp[(i,i+j)] = pq[i+j]
        else:
            tmp = 1001
            for k in range(j):
                c = dp[(i,i+k)] + dp[(i+k+1,i+j)]+sum(pq[i+1:i+j+1])
                if c < tmp:
                    if j == len(pq)-1 and i+k % 2 == 1:
                        continue
                    tmp = c
            dp[(i,i+j)] = tmp
print(dp[(0,2*n+1)])

Python: import sys

n = int(sys.stdin.readline())

ky = list(map(float, sys.stdin.readline().split()))
dm = list(map(float, sys.stdin.readline().split()))
depth = [0]*n
ans = max(max(ky),max(dm))*(n*2+1)*(n+1)

print(ky, dm)


def traverse(d, s, e, note):
    global ans
    if s==e:
        depth[s]=d
        er = 0

        for i in range(n):         er += depth[i]*ky[i]
        er += (depth[0]+1)*dm[0]
        for i in range(1, n):      er += (max(depth[i-1], depth[i])+1)*dm[i]
        er += (depth[n-1]+1)*dm[n]
        
        if ans>er: ans = er
        return
    for m in range(s, e+1):
        depth[m]=d
        if s<=m-1: traverse(d+1,  s, m-1, 'left')
        if m+1<=e: traverse(d+1, m+1, e, 'right')


traverse(1, 0, n-1, '')
print(ans)

Python: global memo
global memo_pq
global p_list
global q_list



def mk_memo(i,j):#memo[i][j]を求める
    tmp_min=9999999
    for k in range(1,j-1):
        tmp=p_list[i+k] + memo[i][k] + memo_pq[i][k] + memo[i+k+1][j-k-1] + memo_pq[i+k+1][j-k-1]
        if(tmp < tmp_min):
            tmp_min=tmp
    if(j!=1):
        tmp_min=min([tmp_min, p_list[i] +2*q_list[i] + memo[i+1][j-1] +memo_pq[i+1][j-1]])
        tmp_min=min([tmp_min, p_list[i+j-1] +2*q_list[i+j] + memo[i][j-1] +memo_pq[i][j-1]])
    else:
        tmp_min=p_list[i] + 2*q_list[i] + 2*q_list[i+1]
    memo[i][j]=tmp_min



n=int(input())
p_list=list(map(float,input().split()))
q_list=list(map(float,input().split()))



memo=[[0]*(n+1-i) for i in range(n+1)]#memo[i][j]はlist[i]から始まるj個の部分集合に対する解
memo_pq=[[0]*(n+1-i) for i in range(n+1)]#memo_pq[i][j]はlist[i]から始まる
                                       #ｊ個の部分集合に対応する重みpと重みqの和
for p in range(n+1):
    for q in range(n+1-p):
        if(q==0):
            memo_pq[p][q]=q_list[p]
        else:
            memo_pq[p][q]=memo_pq[p][q-1] + p_list[p+q-1] + q_list[p+q]
    



for i in range(n):
    for j in range(1,2+i):
        mk_memo(n-1-i,j)

print(memo[0][-1])

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write

N = int(readline())
*P, = map(float, readline().split())
*Q, = map(float, readline().split())

K = [[None]*(N+1) for i in range(N+1)]
C = [[None]*(N+1) for i in range(N+1)]
S = [0]*(2*N+2)
for i in range(2*N+1):
    S[i+1] = S[i] + (P[i//2] if i % 2 else Q[i//2])

for i in range(N+1):
    C[i][i] = Q[i]
    K[i][i] = i

for l in range(1, N+1):
    for i in range(N+1-l):
        j = i+l
        k0 = K[i][j-1]; k1 = K[i+1][j]
        tmp = 1e30
        k2 = None
        for k in range(k0, min(k1+1, j)):
            v = C[i][k] + C[k+1][j]
            if v < tmp:
                k2 = k
                tmp = v
        K[i][j] = k2
        C[i][j] = tmp + (S[2*j+1] - S[2*i])
write("%.10f\n" % C[0][N])

Python: import sys

n = int(sys.stdin.readline())
a = [0.0] * (2*n+1)
b = list(map(float, sys.stdin.readline().split()))
for i,c in enumerate(b):    a[i*2+1] = c
b = list(map(float, sys.stdin.readline().split()))
for i,c in enumerate(b):    a[i*2] = c
depth = [0]*(n*2+1)
ans = max(a)*(n*2+1)*(n+1)
def traverse(d, s, e):
    global ans
    if s>=e:
        depth[s*2+1]=d
        er = 0
        for i in range(0,n*2+1,2):
            if i==0:      depth[i]=depth[i+1]+1
            elif i==n*2:  depth[i]=depth[i-1]+1
            else:         depth[i]=max(depth[i-1],depth[i+1])+1
        for i in range(n*2+1):
            er += (depth[i]+1)*a[i]
        if ans>er: ans = er
        return
    for m in range(s, e+1):
        depth[m*2+1]=d
        if s<=m-1: traverse(d+1,  s, m-1)
        if m+1<=e: traverse(d+1, m+1, e)
        
traverse(0, 0, n-1)
print(ans)

Python: def main():
    """最適二分探索木での探索一回あたりの期待値の最小値を求める
    """
    inf = 10*6
    n = int(input())
    # 内包よりはやい
    P = [0.0] + list(map(float, input().split()))
    Q = list(map(float, input().split()))
    E = [[0.0]*(n+1) for _ in range(n+1)]
    W = [[0.0]*(n+1) for _ in range(n+1)]
    for i in range(1, n+1):
        W[i][i] = Q[i-1] + P[i] + Q[i]
        E[i][i] = P[i] + (Q[i-1] + Q[i])*2
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            W[i][j] = W[i][j-1] + P[j] + Q[j]
    for d in range(1, n):
        for i in range(1, n-d+1):
            j = i + d
            e = Q[i-1] + E[i+1][j] + W[i][j] 
            for k in range(i+1, j):
                ne =  E[i][k-1] + E[k+1][j] + W[i][j]
                if ne < e:
                    e = ne
            ne = E[i][j-1] + Q[j] + W[i][j]
            if ne < e:
                e = ne
            E[i][j] = e
    print(E[1][-1])

if __name__ == "__main__":
    main()

Python: n=int(input())
A=[0.0]+[float(i) for i in input().split()]
B=[float(i) for i in input().split()]
W={}
E={}
for i in range(1,n+2):
    W[i,i-1] = B[i-1]
    E[i,i-1] = B[i-1],i
for i in range(1,n+1):
    for j in range(i,n+1):
        W[i,j] = W[i,j-1]+A[j]+B[j]
for i in range(1,n+1):
    E[i,i] = E[i,i-1][0] + E[i+1,i][0] + W[i,i],i
for h in range(1,n):
    for i in range(1,n-h+1):
        e = 1001,None
        j = i + h
        k0 = E[i,j-1][1]
        k1 = E[i+1,j][1]
        for r in range(k0,k1+1):
            tmp = E[i,r-1][0] + E[r+1,j][0] + W[i,j]
            if tmp < e[0]:
                e = tmp,r
        E[i,j] = e
print(E[1,n][0])

Python: n=int(input())
p=[float(i) for i in input().split()]
q=[float(i) for i in input().split()]
pq = [q[-1]]*2*(n+1)
dp = {}
for i in range(n):
    pq[2*i+1] = q[i]
    pq[2*i+2] = p[i]
for i in range(len(pq)):
    for j in range(len(pq)-i):
        if i == 0:
            dp[(j,j+i)] = 0
        elif i == 1:
            dp[(j,j+i)] = pq[j+i]
        else:
            tmp = 1001
            for k in range(i):
                c = dp[(j,j+k)] + dp[(j+k+1,j+i)]+sum(pq[j+1:j+i+1])
                if c < tmp:
                    tmp = c
            dp[(j,j+i)] = tmp
print(dp[(0,2*n+1)])