Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	// "github.com/pkg/profile"
)

func mkTbl(n int, ds []float64) [][]float64 {
	tbl := make([][]float64, n+1)
	for i := range tbl {
		tbl[i] = make([]float64, n+1)
	}
	for i := 0; i <= n; i++ {
		tbl[i][i] = ds[i]
	}
	return tbl
}

func mkWeights(n int, ps, ds []float64) [][]float64 {
	tbl := mkTbl(n, ds)
	for i := 0; i < n; i++ {
		w := ds[i]
		for j := i + 1; j <= n; j++ {
			w += ps[j] + ds[j]
			tbl[i][j] = w
		}
	}
	return tbl
}

func minCost(i, j int, tbl [][]float64) float64 {
	m := math.MaxFloat64
	for k := i + 1; k <= j; k++ {
		m = math.Min(m, tbl[i][k-1]+tbl[k][j])
	}
	return m
}

func solve(n int, ps, ds []float64) float64 {
	costs := mkTbl(n, ds)
	weights := mkWeights(n, ps, ds)
	for l := 1; l <= n; l++ {
		for i := 0; i <= n-l; i++ {
			j := i + l
			costs[i][j] = weights[i][j] + minCost(i, j, costs)
		}
	}
	return costs[0][n]
}

func main() {
	// defer profile.Start(profile.ProfilePath(".")).Stop()

	w := bufio.NewWriter(os.Stdout)
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)

	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	ps := make([]float64, n+1)
	ds := make([]float64, n+1)

	for i := 1; i <= n; i++ {
		s.Scan()
		ps[i], _ = strconv.ParseFloat(s.Text(), 64)
	}

	for i := 0; i < n+1; i++ {
		s.Scan()
		ds[i], _ = strconv.ParseFloat(s.Text(), 64)
	}

	if s.Err() != nil {
		log.Fatal("scan error")
	}

	fmt.Fprintf(w, "%.4f\n", solve(n, ps, ds))
	_ = w.Flush()
}

C++: #include <iostream>
#include <vector>
#include <float.h>

int main() {
	int n;
	std::cin >> n;
	std::vector<double> p(n, 0);
	std::vector<double> q(n + 1, 0);
	std::vector<std::vector<double>> CostMat(n, std::vector<double>(n, 0));

	for (int i = 0; i < n; i++) {
		std::cin >> p[i];
	}
	for (int i = 0; i < n + 1; i++) {
		std::cin >> q[i];
	}

	// 対角成分
	for (int i = 0; i < n; i++) {
		CostMat[i][i] = p[i] + 2.0 * q[i] + 2.0 * q[i + 1];
	}

	// 動的計画法により最小コストを求める
	for (int i = 1; i < n; i++) {
		for (int j = 0; j < n - i; j++) {
			double min = DBL_MAX;
			for (int l = 0; l <= i; l++) {
				double tmp = 0;
				if (l != i) {
					tmp += CostMat[j + l + 1][i + j];
				}
				if (l != 0) {
					tmp += CostMat[j][j + l - 1];
				}
				if (l == 0) {
					tmp += q[j];
				}
				if (l == i) {
					tmp += q[j + i + 1];
				}
				if (tmp < min) {
					min = tmp;
				}
			}
			//std::cout << min << std::endl;
			CostMat[j][i + j] = min;
			for (int k = j; k <= j + i; k++) {
				CostMat[j][i + j] += p[k] + q[k];
			}
			CostMat[j][i + j] += q[j + i + 1];
		}
	}

	std::cout << CostMat[0][n - 1] << std::endl;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;

const int NIL = -1;
const int MAX = 20001;
int root[510][510];
double w[510][510];
double e[510][510];

int main(void)
{
	int n;
	double P, Q, t;
	vector<double>p, q;
	cin >> n;
	p.push_back(0);
	for (int i = 0; i < n; i++) {
		cin >> P;
		p.push_back(P);
	}
	for (int i = 0; i < n + 1; i++) {
		cin >> Q;
		q.push_back(Q);
	}
	for (int i = 1; i <= n + 1; i++) {
		e[i][i - 1] = q[i - 1];
		w[i][i - 1] = q[i - 1];
	}
	for (int l = 1; l <= n; l++) {
		for (int i = 1; i <= n - l + 1; i++) {
			int j = i + l - 1;
			e[i][j] = MAX;
			w[i][j] = w[i][j - 1] + p[j] + q[j];
			for (int 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;
				}
			}
		}
	}
	printf("%.8lf\n", e[1][n]);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
double P[505],Q[505],dp[505][505],S[505][505],a1,a2;
int n,i,j,k,m;
int main(){
	cin>>n;
	for(i=1;i<=n;i++)scanf("%lf",&P[i]);
	for(i=0;i<=n;i++)scanf("%lf",&Q[i]);
	for(i=1;i<=n;i++){
		dp[i][i]=P[i]+2*(Q[i-1]+Q[i]);
		S[i][i]=P[i]+Q[i-1]+Q[i];
	}
	for(k=1;k<n;k++)
		for(i=1;i<=n-k;i++){
			j=i+k;
			S[i][j]=S[i][j-1]+P[j]+Q[j];
			a1=S[i][j]+Q[i-1]+dp[i+1][j];
			a2=S[i][j]+Q[j]+dp[i][j-1];
			dp[i][j]=min(a1,a2);
			for(m=i+1;m<j;m++)dp[i][j]=min(dp[i][j],S[i][j]+dp[i][m-1]+dp[m+1][j]);
		}
	printf("%.8lf\n",dp[1][n]);
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

static const int MAX = 500;
double p[MAX + 1];
double q[MAX + 1];
double w[MAX + 2][MAX + 2];
double e[MAX + 2][MAX + 2];
double r[MAX + 1][MAX + 1];

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> p[i];
    }
    for (int i = 0; i <= n; ++i)
    {
        cin >> q[i];
    }
    for (int i = 1; i != n + 2; ++i)
    {
        e[i][i-1] = q[i-1];
        w[i][i-1] = q[i-1];
    }

    for (int l = 1; l <= n; ++l)
    {
        for (int i = 1; i <= n - l + 1; ++i)
        {
            int j = i + l - 1;
            e[i][j] = (1 << 21);
            w[i][j] = w[i][j-1] + p[j] + q[j];
            for (int k = i; k <= j; ++k)
            {
                double t = e[i][k-1] + e[k+1][j] + w[i][j];
                if (t < e[i][j])
                {
                    e[i][j] = t;
                    r[i][j] = k;
                }
            }
        }
    }
    cout << fixed << setprecision(8) << e[1][n] << endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>

#define N 501

using namespace std;

int main() {
    int n;
    double p[N], q[N];
    double pSum[N][N], qSum[N][N];
    double d[N][N];
    double t;
    
    //標準入力から要素数を取得
    cin >> n;
    
    for(int i = 0; i < n; ++i) {
        //p[0]には代入しない
        //標準入力から探索確率を取得
        cin >> p[i];
    }
    
    for(int i = 0; i < n + 1; ++i) {
        //標準入力からダミーキーで探索終了する確率を取得
        cin >> q[i];
        
        //行列dの対角成分としてq[i]を格納
        d[i][i] = q[i];
    }
    
    //
    for(int i = 0; i < n; ++i) {
        pSum[i][i] = p[i];
        
        for(int j = i + 1; j < n; ++j) {
            pSum[i][j] = pSum[i][j - 1] + p[j];
        }
    }
    for(int i = 0; i < n + 1; ++i) {
        qSum[i][i] = q[i];
        for(int j = i + 1; j < n + 1; ++j) {
            qSum[i][j] = qSum[i][j - 1] + q[j];
        }
    }
    
    for(int i = 1; i < n + 1; ++i) {
        for(int j = 0; j < (n + 1) - i; ++j) {
            d[j][j + i] = __DBL_MAX__;
            
            for(int k = 0; k < i; ++k) {
                t = d[j][j + k] + d[j + k + 1][j + i] + pSum[j][j + i - 1] + qSum[j][j + i];
                d[j][j + i] = min(d[j][j + i], t);
            }
        }
    }
    
    printf("%.8lf\n", d[0][n]);
    
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<stdio.h>
using namespace std;

static const int INFTY = 20001;

double w[510][510];
double e[510][510];
int root[510][510];

int main(){

    int n;
    double P, Q, t; //グローバル変数にする必要がある
    vector<double> p, q;
    
    //入力処理
    cin >> n;
    p.push_back(0);
    for(int i=0; i<n; i++){
        cin >> P;
        p.push_back(P); 
    }
    for(int i=0; i<n+1; i++){
        cin >> Q;
        q.push_back(Q);
    }

    for(int i=1; i<=n+1; i++){
        e[i][i-1] = q[i-1];
        w[i][i-1] = q[i-1];
    }
    for(int l=1; l<=n; l++){
        for(int i=1; i<=n-l+1; i++){
            int j = i + l - 1;
            e[i][j] = 20001;
            w[i][j] = w[i][j-1] + p[j] + q[j];
            for(int k=i; k<=j; k++){
                t = e[i][k-1] + e[k+1][j] + w[i][j];    //期待値
                if(t < e[i][j]){    //最小値を探す
                    e[i][j] = t;
                    root[i][j] = k;
                }
            }
        }
    }
    printf("%.8lf\n", e[1][n]);
    return 0;
}