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++: import java.io.PrintWriter
import java.util.*

data class PE(var p: Double, var e: Double)
data class Node(var center: PE, var left: PE, var right: PE, var lNode: Node?, var rNode: Node?) : Comparable<Node> {
    override fun compareTo(other: Node): Int {
        val a = center.e + center.p
        val o = other.center.e + other.center.p
        return if (a == o) 0
        else if( a < o ) -1
        else 1
    }
}

fun main(args: Array<String>) = with(PrintWriter(System.out)) {
    val n = readLine()!!.toInt()
    val p = readLine()!!.trim().split(' ').map { it.toDouble() }
    val q = readLine()!!.trim().split(' ').map { it.toDouble() }

    val list = mutableListOf<Node>()
    for (i in 0 until n) {
        val c = p[i]
        val l = q[i]
        val r = q[i+1]
        list.add(Node(PE(c+l+r, c+(l+r)*2), PE(l, l), PE(r, r), null, null))
    }

    if ( n == 1 ) {
        print(list[0].center.e)
        flush()
        return
    }

    list[0].rNode = list[1]
    list[n-1].lNode = list[n-2]
    for (i in 1 until n-1) {
        list[i].lNode = list[i-1]
        list[i].rNode = list[i+1]
    }

    // pop min e+p
    val queue = PriorityQueue<Node>(n)
    queue.addAll(list)

    while (queue.size != 1) {
        val minNode = queue.poll()

        if (minNode.rNode != null) {
            val rNode = minNode.rNode!!
            queue.remove(rNode)
            rNode.center.p = rNode.center.p - rNode.left.p + minNode.center.p
            rNode.center.e = rNode.center.e - rNode.left.e - rNode.left.p + minNode.center.e + minNode.center.p
            rNode.left = minNode.center
            rNode.lNode = minNode.lNode
            queue.add(rNode)
        }

        if (minNode.lNode != null) {
            val lNode = minNode.lNode!!
            queue.remove(lNode)
            lNode.center.p = lNode.center.p - lNode.right.p + minNode.center.p
            lNode.center.e = lNode.center.e - lNode.right.e - lNode.right.p + minNode.center.e + minNode.center.p
            lNode.right = minNode.center
            lNode.rNode = minNode.rNode
            queue.add(lNode)
        }
    }

    val minNode = queue.poll()
    println(minNode.center.e)
    flush()
}

C++: #include<iostream>
#include<algorithm>
#include<cfloat>
using namespace std;
int main(){
        int n;
        cin>>n;
        double p[n], q[n+1];
        for(int i=0;i<n;i++) cin>>p[i];
        for(int i=0;i<n+1;i++) cin>>q[i];

        double dp[n+1][n+1];
        double psum[n][n];
        double qsum[n+1][n+1];
        for(int i=0;i<n+1;i++) dp[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+i<n+1;j++){
                        dp[j][j+i] = DBL_MAX;
                        for(int k=0;k<i;k++){
                                double tmp = dp[j][j+k]+dp[j+k+1][j+i]+psum[j][j+i-1]+qsum[j][j+i];
                                dp[j][j+i] = min(dp[j][j+i], tmp);
                        }
                }
        }
        cout<<dp[0][n]<<endl;

        return 0;
}

C++: #include <bits/stdc++.h>
#include <cmath>

using namespace std;
typedef long long ll;
//typedef unsigned long long ll;

//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
//typedef pair<ll, int> P;
//typedef pair<ll, ll> P;
const double INF = 1e10;
//const ll INF = LONG_LONG_MAX / 10;
//const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
//const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

bool contain(set<char> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

typedef pair<double, double> P;

void print_line(vector<int> &line) {
    if (line.size() == 0ll) {
        cout << endl;
        return;
    }
    for (ll i = 0; i < line.size(); i++) {
        cout << line[i];
        if (i == line.size() - 1) cout << endl;
        else cout << ' ';
    }
}


typedef priority_queue<long long, vector<long long>, greater<long long>> PQ_ASK;
const int mod = 1000000007;

int main() {
    int n;


    cin >> n;
    vector<P> ks(n + 1, P(0, 0));
    vector<vector<double>> p_sum(n + 1, vector<double>(n + 1, 0));
    vector<vector<double>> q_sum(n + 1, vector<double>(n + 1, 0));
    vector<vector<double>> dp(n + 1, vector<double>(n + 1, 0));;

    for (int i = 0; i < n; ++i) cin >> ks[i].first;
    for (int i = 0; i < n + 1; ++i) cin >> ks[i].second;
    for (int i = 0; i < n + 1; ++i) dp[i][i] = ks[i].second;

    for (int i = 0; i < n; ++i) {
        p_sum[i][i] = ks[i].first;
        for (int j = i + 1; j < n; ++j) {
            p_sum[i][j] = p_sum[i][j - 1] + ks[j].first;
        }
    }

    for (int i = 0; i < n + 1; ++i) {
        q_sum[i][i] = ks[i].second;
        for (int j = i + 1; j < n + 1; ++j) {
            q_sum[i][j] = q_sum[i][j - 1] + ks[j].second;
        }
    }

    for (int i = 1; i < n + 1; ++i) {
        for (int j = 0; j < (n + 1) - i; ++j) {
            dp[j][j + i] = INF;
            for (int k = 0; k < i; ++k) {
                double t = dp[j][j + k] + dp[j + k + 1][j + i] + p_sum[j][j + i - 1] + q_sum[j][j + i];
                dp[j][j + i] = min(dp[j][j + i], t);
            }
        }
    }

    printf("%.20f\n", dp[0][n]);

}