Go: package main

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

type Matrix struct {
	r int // num of row
	c int // num of column
}

type Range struct {
	start int // index, inclusive
	end   int // index, inclusive
}

func calcMultiplication(x, y Matrix) (Matrix, int) {
	if x.c != y.r {
		panic("cannnot calculate")
	}
	return Matrix{x.r, y.c}, x.r * y.c * x.c
}

func calcMinMatrixChainMultiplication(matrices []Matrix, r Range, memo map[Range]int) (Matrix, int) {
	if v, ok := memo[r]; ok {
		return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, v
	}
	if len(matrices) == 0 {
		panic("must not be empty")
	} else if len(matrices) == 1 {
		return matrices[0], 0
	} else if len(matrices) == 2 {
		return calcMultiplication(matrices[0], matrices[1])
	}

	var minCount int = math.MaxInt32

	for i := 0; i < len(matrices)-1; i++ {
		formerMatrices := matrices[:i+1]
		latterMatrices := matrices[i+1:]
		matF, countF := calcMinMatrixChainMultiplication(formerMatrices, Range{0, i}, memo)
		matL, countL := calcMinMatrixChainMultiplication(latterMatrices, Range{i + 1, len(matrices) - 1}, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, minCount
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, _ := nextInt(sc)
	matrices := make([]Matrix, n)
	for i := 0; i < n; i++ {
		r, _ := nextInt(sc)
		c, _ := nextInt(sc)
		matrices[i] = Matrix{r, c}
	}
	memo := make(map[Range]int)
	_, resultCount := calcMinMatrixChainMultiplication(matrices, Range{0, len(matrices) - 1}, memo)
	fmt.Println(resultCount)
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

static const int N = 100;

void mcm() {
    int n = 4;
    int p[n+1], m[n+1][n+1];

    int sample[][4] = {{5, 4}, {4, 6}, {6, 2}, {2, 7}};

    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        p[i -1] = sample[cnt][0];
        p[i] = sample[cnt][1];
        cnt++;
    }
    cout << "Values of p: " << endl;
    for (int i = 0; i <=n; i++) cout << p[i] << " ";
    cout << endl;

    //diagonal part is always 0
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <=n; j++) {
            m[i][j] = 0;
        }
    }

    for (int l = 2; l <= n; l++) {
        for (int i = 1; i <= n - l + 1; i++) {
            int j = i + l - 1;
            m[i][j] = (1 << 21);
            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]);
            }
        }
    }

    cout << "Result table: " << endl;
    for (int i = 1; i <=n; i++) {
        for (int j = 1; j <=n; j++) {
            cout << m[i][j] << " ";
        }
        cout << endl;
    }
}


int main()
{
    int n, p[N+1], m[N+1][N+1];
    cin >> n;

    for (int i = 1; i <=n; i++) {
        cin >> p[i - 1] >> p[i];
    }
    //diagonal part is always 0
    for (int i = 1; i <= n; i++) m[i][i] = 0;

    for (int l = 2; l <= n; l++) {
        for (int i = 1; i <= n - l + 1; i++) {
            int j = i + l - 1;
            m[i][j] = (1 << 21);
            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]);
            }
        }
    }

    cout << m[1][n] << endl;
    //mcm();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
typedef long long LL;
const int INF=10000000;
int r[110], c[110];
int dp[110][110];

int CalcMin(int L, int R){
  if(L==R) return dp[L][R]=0;
  int res=INF;
  rep(M,R-L){
    int left, right;
    if(dp[L][L+M]!=-1) left=dp[L][L+M];
    else left=CalcMin(L,L+M);
    if(dp[L+M+1][R]!=-1) right=dp[L+M+1][R];
    else right=CalcMin(L+M+1,R);
    res=min(res,left+right+r[L]*c[L+M]*c[R]);
  }
  return dp[L][R]=res;
}

int main(){
  int n;
  cin >> n;
  rep(i,n) cin >> r[i] >> c[i];
  rep(i,n){
    rep(j,n) dp[i][j]=-1;
  }
  cout << CalcMin(0,n-1) << endl;

  return 0;
}

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

const int MAX = 105;
const int INF = 1<<29;
struct Matrix
{
    int r, c;
};
Matrix M[MAX];
int dp[MAX][MAX], n;

int DP(int i, int j)
{
    int &ans = dp[i][j];
    if(ans!=-1) return ans;
    ans = INF;
    for(int k=i; k<j; k++)
    {
        ans = min(ans, DP(i, k)+DP(k+1, j)+M[i].r*M[k].c*M[j].c);
    }
    return ans;
}
int main()
{
    scanf("%d", &n);
    memset(dp, -1, sizeof(dp));
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d", &M[i].r, &M[i].c);
        dp[i][i] = 0;
    }
    printf("%d\n", DP(1, n));
    return 0;
}

C++: #include<bits/stdc++.h>
#define dump(a) cout << #a << " " << a << endl
#define MAX_SIZE 101
using namespace std;

vector<pair<int,int>> sikis;
int dp[MAX_SIZE][MAX_SIZE];


int mul(int L, int M, int R) {
	int r1 = sikis[L].first;
	int c2 = sikis[R-1].second;
	int r2 = sikis[M].first;
	return (r1 * c2 * r2); //r1*c2*c1
}

int dfs(int L, int R) {
	if (L + 1 == R) { return 0; }
	if (dp[L][R] >= 0) { return dp[L][R]; } //dp?????¢
	
	vector<int> values;
	for(int m = L+1; m < R; m++) {
		int value = dfs(L,m)+dfs(m,R)+mul(L,m,R);
		values.push_back(value);
	}
	dp[L][R] = *min_element(values.begin(), values.end());

	return dp[L][R];
}

int main() {
	int N;
	int r,c;

	cin >> N;
	for(int y=0; y<N; y++) {
		for(int x=0; x<N+1; x++) {
			dp[y][x] = -1;
		}
	}

	for(int i=0; i<N; i++) {
		cin >> r >> c;
		sikis.push_back(make_pair(r,c));
	}
	cout << dfs(0,N) << endl;

	return EXIT_SUCCESS;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int N;

struct Matrix{ int R, W; };
Matrix M[MAX_N];

int mem[MAX_L][MAX_L];
Matrix memM[MAX_L][MAX_L];


void eval(Matrix M[], int s, int e){

	if (s == e){
		mem[s][e] = 0;
		memM[s][e] = M[s];
		return;
	}

	int minval = 1000000000;
	for (int mid = s; mid < e; mid++){
		Matrix m1 = memM[s][mid];
		Matrix m2 = memM[mid+1][e];
		int val = mem[s][mid] + mem[mid+1][e] + (m1.W * m1.R * m2.W);
		
		if (val < minval){
			minval = val;
			mem[s][e] = val;
			Matrix m; m.R = m1.R; m.W = m2.W;
			memM[s][e] = m;
		}
	}

}

int solve(Matrix M[], int N){

	for (int d = 0; d <= N; d++)
		for (int s = 0; s + d <= N; s++)
		{
			eval(M, s, s + d);
		}

	return mem[0][N];
}

int main() {

	cin >> N;
	rep(i, N){
		Matrix m;
		cin >> m.R;
		cin >> m.W;
		M[i] = m;
	}

	cout << solve(M, N) << endl;

#ifdef DEB
	rep (i, N){
		rep(j, N){
			cerr << mem[i][j] << ' ';
		}
		cerr << endl;
	}
#endif
}