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])
}

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]);

}