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
	}

	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
		}
	}
	memo[r] = minCount
	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 <vector>
#include <string>
#include <stack>
#include <queue>

using namespace std;

static const int N = 1000;

int n, p[N + 1], m[N + 1][N + 1];

int main(){
	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> p[i - 1] >> p[i];

	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 - 1; ++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;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int p[105];
int m[110][110];
int n;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> p[i-1];
        cin >> p[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] = (i << 21);
            for (int 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]);
            }
        }
    }
    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 <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

#define N 100

int M[101][101], p[101], n;

int solve()
{
	int l, i, j, k;
	for (l = 2; l <= n; l++) {
		for (i = 1; i <= n - l + 1; i++) {
			j = i + l - 1;
			M[i][j] = INT32_MAX;
			for (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()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> p[i - 1] >> p[i];

	for (int i = 1; i <= n; i++) 
		M[i][i] = 0;

	cout << solve() << endl;
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
//#include<queue>
#include<algorithm>
using namespace std;
static const int N = 100;

int main() {
	int i, n, m[N + 1][N + 1], p[N + 1];
	//priority_queue<int> PQ;

	scanf("%d", &n);

	for (i = 1; i <= n; i++)scanf("%d %d", &p[i - 1], &p[i]);

	for (i = 1; i <= n; i++) {
		m[i][i] = 0;
	}

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

	printf("%d\n", m[1][n]);

	return 0;
}

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

vector<vector<int> > cost,best;
vector<int> A;

void compute(int s,int e){
	int minc = 0;
	int mink = -1;
	for(int k=s+1;k<=e;++k){
		if (cost[s][k-1] < 0){
			compute(s,k-1);
		}
		if (cost[k][e] < 0) {
			compute(k,e);
		}
		int c = cost[s][k-1] + A[s]*A[k]*A[e+1] + cost[k][e];
		if (mink < 0 || c < minc) {
			minc = c;
			mink = k;
		}
	}
	cost[s][e] = minc;
	best[s][e] = mink;
}

int main(){
	int n;
	cin >> n;
	A.resize(n+1);
	for(int i=0;i<n;++i) {
		int r,c;
		cin >> r >> c;
		if (i==0){
			A[0] = r;
		}
		if (A[i] != r){ cout << "error";exit(0);}
		A[i+1] = c;
	}
	cost.resize(n);
	best.resize(n);
	for(int i=0;i<n;++i){
		cost[i].resize(n);
		best[i].resize(n);
		for(unsigned j=0;j<cost[i].size();++j){
			cost[i][j] = -1;
			best[i][j] = -1;
		}
		cost[i][i]=0;
		best[i][i]=i;
	}
	compute(0,n-1);
	cout << cost[0][n-1] << std::endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for(int i=0; i<(int)(a); i++)
typedef long long ll;
const ll inf = 1LL<<60;

int main()
{
  int n; cin >> n;
  vector<int> p(n+1, 0);
  rep(i, n) cin >> p[i] >> p[i+1];
  vector<vector<ll>> dp(n+1, vector<ll>(n+1, inf));
  rep(i, n+1) dp[i][i] = 0;
  rep(i, n) dp[i][i+1] = 0;
  for (int len=2; len<=n; len++) {
    for (int left=0; left+len<=n; left++) {
      int right = left+len;
      dp[left][right] = inf;
      for (int i=left+1; i<right; i++) {
        dp[left][right] = min(dp[left][right], dp[left][i] + dp[i][right] + p[left]*p[i]*p[right]);
      }
    }
  }
  cout << dp[0][n] << endl;

  return 0;
}

C++: #include<iostream>
using namespace std;
#define M 101
#define I 100000000
int main(){
    int n,p[M],m[M][M],i,j,k,t;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>p[i-1]>>p[i];
    }
    for(i=1;i<=n;i++)m[i][i]=0;
    for(i=2;i<=n;i++){
        for(j=1;j<=n-i+1;j++){
            t=i+j-1;
            m[j][t]=I;
            for(k=j;k<=t-1;k++){
                m[j][t]=min(m[j][t],m[j][k]+m[k+1][t]+p[j-1]*p[k]*p[t]);
            }
        }
    }
    cout<<m[1][n]<<endl;
    return 0;
}