Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

type matrixSize struct{ row, col int }

func calcNum(i, j matrixSize) int {
	return i.row * j.col * i.col
}

func parseProblem() (int, []matrixSize) {
	n := scanInt(sc)
	sizes := make([]matrixSize, n)
	for i := 0; i < n; i++ {
		sizes[i] = matrixSize{row: scanInt(sc), col: scanInt(sc)}
	}
	return n, sizes
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = math.MaxInt64
		}
	}
}

func resolve(n int, sizes []matrixSize) int {
	// TODO 復習
	initDP(n)
	_rec(sizes, 0, n)
	return dp[0][n]
}

func _rec(sizes []matrixSize, l, r int) int {
	if dp[l][r] != math.MaxInt64 {
		return dp[l][r]
	}
	if r-l <= 1 {
		dp[l][r] = 0
		return 0
	}
	num := math.MaxInt64
	for i := l + 1; i < r; i++ {
		v := _rec(sizes, l, i) + _rec(sizes, i, r) + calcNum(matrixSize{sizes[l].row, sizes[i].row}, matrixSize{sizes[i].row, sizes[r-1].col})
		if v < num {
			num = v
		}
	}
	dp[l][r] = num
	return dp[l][r]
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
void MatrixChain(int *p, int n, int **m, int **s)
{
	for (int i = 1;i <=n;i++)
		m[i][i] = 0;
	for (int r = 2;r <= n;r++)
	{
		for (int i = 1;i <= n - r + 1;i++)
		{
			int j = i + r - 1;
			m[i][j] = m[i + 1][j] + (p[i - 1] * p[i] * p[j]);
			s[i][j] = i;
			for (int k = i + 1;k < j;k++)
			{
				int t = m[i][k] + m[k + 1][j] + (p[i - 1] * p[k] * p[j]);
				if (t < m[i][j])
				{
					m[i][j] = t;
					s[i][j] = k;
				}
			}
		}
	}
}

void print(int i, int j, int **s)
{
	if (i == j)
		cout << "A" << i;
	else
	{
		cout << "(";
		print(i, s[i][j], s);
		print(s[i][j] + 1, j, s);
		cout << ")";
	}
}
int main()
{
	int n;
	cin >> n;
	int *tempP = new int[2 * n]; 
	int *p = new int[n + 1];   
	int **m = new int *[n + 1]; 
	int **s = new int *[n + 1]; 
	for (int i = 1; i <= n; i++)
	{
		m[i] = new int[n + 1];
		s[i] = new int[n + 1];
	}

	for (int i = 0; i < 2 * n; i++)
	{
		cin >> tempP[i];
	}

	for (int i = 0; i <= n; i++)
	{
		p[i] = (i == 0 ? tempP[0] : tempP[i * 2 - 1]);
	}
	MatrixChain(p, n, m, s);
	cout << m[1][n] << endl; 
	return 0;
}

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

int mcm(int p[], int n) {
	int m[n+1][n+1];
	memset(m, 0, sizeof(m));

	for (int s = 0; s < n; s++) {
		for (int i = 1, j = s + 2; j <= n; i++, j++) {
			m[i][j] = 1 << 30;
			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]);
			}
		}
	}
	return m[1][n];
}

int main() {
	int n, d;
	cin >> n;
	int p[n+1];
	for (int i = 0; i < n-1; i++) {
		cin >> p[i] >> d;
	}
	cin >> p[n-1] >> p[n];
	int ans = mcm(p, n);
	cout << ans << endl;
	return 0;
}

C++: #include <cstdio>
#include <climits>

int convert(int p, int size, int n){
    for(int i=1;i<size;i++)p+=n--;
    return p;
}

int multipulication(int *r, int *c, int *count, int p, int size, int n){
    if(size==1)return 0;
    int id = convert(p,size,n);
    if(count[id])return count[id];
    int mlt = INT_MAX;
    int i, tmp;
    for(i=1;i<size;i++){
        tmp = r[p]*r[p+i]*c[p+size-1] + multipulication(r,c,count,p,i,n) + multipulication(r,c,count,p+i,size-i,n);
        if(tmp<mlt)mlt=tmp;
    }
    count[id] = mlt;
    return mlt;
}

int main(){
    int i, n;
    scanf("%d",&n);
    int r[n];
    int c[n];
    int count[n*(n+1)/2];
    for(i=0;i<n*(n+1)/2;i++)count[i]=0;
    for(i=0;i<n;i++)scanf("%d %d",r+i,c+i);
    printf("%d\n",multipulication(r,c,count,0,n,n));
    return 0;
}

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

#define MAX_N 10000
#define INF 10000000LL

long long n, j, x[MAX_N][MAX_N], p[MAX_N + 2];

int matrix() {
	for (int i = 0; i < n; i++) { x[i][i] = 0; }
	for (int h = 2; h <= n; h++) {
		for (int i = 1; i <= n - h + 1; i++) {
			j = i + h - 1;
			x[i][j] = INF;
			for (int k = i; k < j; k++) {
				x[i][j] = min(x[i][j], x[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}
	return x[1][n];
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> p[i - 1] >> p[i];
	}
	cout << matrix() << endl;
	return 0;
}

C++: /**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int r[45] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);


int f(int n, int k, int m)
{
	return f(n,k) + f(k+1,m) + (r[n] * r[k+1] * r[m+1]);
}

int f(int n, int m)
{
	if (n==m) return 0;
	int & ra = a[n][m];
	if (ra) return ra;
	ra = INT_MAX;
	for (int k=n;k<m;k++)
	{
		int ans = f(n,k,m);
		if (ans < ra) ra = ans;
	}
	return ra;
}

int main()
{
	int rr = 0;
	int cc = 0;
	int n = 0;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cin >> rr >> cc;
		r[i]=rr;
		r[i+1]=cc;
	}
	for (int i=0;i<n-1;i++)
	{
		a[i][i+1] = r[i] * r[i+1] * r[i+2];
	}
	int ans = f(0,n-1);
	cout << ans << endl;
}

C++: #include <stdlib.h>
#include <stdio.h>

int MCM(int n){
int i,j,k,l,m[101][101],p[101],x;

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]=10000000;
        for(k=i;k<=j;k++){
            x=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
            if(m[i][j]>x)m[i][j]=x;
            }
        }
    }
return m[i-1][j];
}

int main(){

int i,n,m[101];

scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d %d",&m[i],&m[i+1]);
   }
printf("%d\n",MCM(n));
return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n,a[101][101];
	int p[101];
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>p[i-1]>>p[i];
	}
	for(int i=1;i<=n;i++)a[i][i]=0;
	for(int l=2;l<=n;l++){
		for(int i=1;i<=n-l+1;i++){
			int j=i+l-1;
			a[i][j]=(1<<21);
			for(int k=i;k<=j-1;k++){
				a[i][j]=min(a[i][j],a[i][k]+a[k+1][j]+p[i-1]*p[k]*p[j]);
			}
		}
	}
	cout<<a[1][n]<<endl;
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

int R[101], C[101];
int dp[101][101];

int f(int l, int r) {
	dump(l, r);
	if (dp[l][r] != INF)return dp[l][r];
	if (l >= r - 1)return 0;
	rep(m, l + 1, r) {
		chmin(dp[l][r], f(l, m) + f(m, r) + R[l] * R[m] * C[r - 1]);
	}
	return dp[l][r];
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	rep(i, 0, n)cin >> R[i] >> C[i];
	memset(dp, 0x3f, sizeof(dp));
	cout << f(0, n) << endl;
	return 0;
}