Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	mm := make([]int, n+1)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		mm[i] = getNextInt(scanner)
		mm[i+1] = getNextInt(scanner)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			for k := i; k <= j-1; k++ {
				d := dp[i][k] + dp[k+1][j] + mm[i-1]*mm[k]*mm[j]
				if dp[i][j] == 0 || dp[i][j] > d {
					dp[i][j] = d
				}
			}
		}
	}

	fmt.Fprintln(writer, dp[1][n])

	writer.Flush()
}

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

const int N = 100;
const long int Max = pow(10, 10);

int setmatrixmulti(int p[], int n){
    int m[N+1][N+1];
    int minval;
    for(int l=1; l<=n; l++){
        for(int i=1; i<=n - l + 1; i++){
            int j = i + l - 1;
            m[i][j] = Max;
            if(i == j){
                m[i][j] = 0;
            } else {
                for(int k=i; k<j; k++){
                    m[i][j] = min(m[i][j], m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]);
                }
            }
        }
    }
    minval= m[1][n];
    return minval;
}

int main(){
    int n, p1, p2;
    cin >> n;
    int p[n+1];
    for(int i=0; i<n; i++){
        cin >> p1 >> p2;
        if(i == 0){
            p[i] = p1;
            p[i+1] = p2;
        } else {
            p[i+1] = p2;
        }
    }

    // for(int i=0; i<n+1; i++){
    //     cout << p[i] << " " ;
    // }

    int ans = setmatrixmulti(p, n);
    cout << ans << endl;

    return 0;
}

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

void MatrixChainMultiplication(int n,int p[],int m[1000][1000]) {
	int i, j, k,l;
	for (i = 1; i <= n; i++) m[i][i] = 0;
	for (l = 2; l <= n; l++) {
		for (i = 1; i <= n - l - 1; i++) {
			j = i + l - 1;
			m[i][j] = (1<<21);
			for (k = i; k <= j - 1; k++) {
				m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}
}

int main() {
	int n, p[1000], m[1000][1000];
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i - 1] >> p[i];
	MatrixChainMultiplication(n, p, m);
	cout << m[1][n] << endl;

	
}

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

using namespace std;

typedef long long ll;

const ll INF = (1LL << 60);

pair<ll, ll> A[110];
ll dp[110][110];

ll matrix_chain(int i, int j){
	if(i == j) return 0;
	if(dp[i][j] != INF) return dp[i][j];
	for(int k = i;k < j; ++k) dp[i][j] = min(dp[i][j], matrix_chain(i, k) 
			+ matrix_chain(k+1, j) + A[i].first*A[k].second*A[j].second);
	return dp[i][j];
}

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;

	for(int i=0;i<n;++i) for(int j=0;j<n;++j) dp[i][j]=INF;

	for(int i=0;i<n;i++) cin >> A[i].first >> A[i].second;
	
	cout << matrix_chain(0, n-1);

	return 0;
} // br1

C++: #pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;
//
//ll matrix_chain_multiplications(vector<int>& p) {
//    int n = p.size() - 1; // num of matrix
//    vector<vll> dp(n + 1, vll(n + 1, INF));
//
//    // init
//    FOR(i, 1, n + 1) dp[i][i] = 0;
//
//    FOR(l, 2, n + 1) {
//        FOR(i, 1, n - l + 2) {
//            int j = i + l - 1;
//            FOR(k, i, j) {
//                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i - 1] * p[k] * p[j]);
//            }
//        }
//    }
//
//    return dp[1][n];
//}

// 0-indexed
ll matrix_chain_multiplications(vector<int>& p) {
    int n = p.size() - 1; // num of matrix
    vector<vll> dp(n, vll(n, INF)); // dp[l][r] = minimum cost of [l, r], 0 <= l, r < n

    // init
    FOR(i, 0, n) dp[i][i] = 0;

    FOR(l, 2, n + 1) { // l = 2 ~ n
        REP(i, n - l + 1) {
            int j = i + l - 1;
            FOR(k, i, j) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i] * p[k + 1] * p[j + 1]);
            }
        }
    }

    return dp[0][n - 1];
}


int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n; cin >> n;

    vi p(n + 1);
    REP(i, n) cin >> p[i] >> p[i + 1];

    print(matrix_chain_multiplications(p));
}

C++: #include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

struct mat {
    int row = -1;
    int col = -1;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    vector<mat> M(N);
    rep(i, N) {
        int a, b;
        cin >> a >> b;
        M[i].row = a, M[i].col = b;
    }

    vector<vector<ll>> dp(N + 5, vector<ll>(N + 5, INF));
    rep(i, N + 5) { dp[i][i] = 0; }

    for (int w = 1; w < N; w++) {
        for (int i = 0; i < N; i++) {
            if (i + w >= N)
                continue;
            int j = i + w;
            for (int k = i; k < j; k++) {
                ll num_pro = M[i].row * M[k].col * M[j].col;
                dp[i][i + w] =
                    min(dp[i][i + w], dp[i][k] + dp[k + 1][i + w] + num_pro);
            }
        }
    }

    cout << dp[0][N - 1] << endl;
}

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

int calcDpTable(int i, int j, int ** dpTable, const int *p) {
	int minValue = INT_MAX; 
	for (int k = i; k < j; ++k) {
		minValue = min(dpTable[i][k - i] + dpTable[k + 1][j - k - 1] + p[i] * p[k + 1] * p[j + 1], minValue);
	}
	//dpTable[0][0] = 1;
	return minValue;
}

int minNumOfMultiplications(const int *p, int n) {
	int **dpTable = new int*[n];
	for (int i = 0; i < n; ++i) {
		dpTable[i] = new int[n - i];
		dpTable[i][0] = 0;
	}

	int d = 1;
	for (int to = n - 2; to >= 0; --to) {
		for (int i = 0; i <= to; ++i) {
			int j = i + d;
			dpTable[i][j - i] = calcDpTable(i, j, dpTable, p);
		}
		++d;
	}
	int ret = dpTable[0][n - 1];
	
	for (int i = 0; i < n; ++i) {
		delete[] dpTable[i];
	}
	delete[] dpTable;

	return ret;
}

int main() {
	int n;
	cin >> n;
	int *p = new int[n + 1];
	for (int i = 0; i < n; ++i) {
		cin >> p[i] >> p[i + 1];
	}
	cout << minNumOfMultiplications(p, n) << endl;
}