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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;

int n = 105;
vector<pp> rc(n);
vector<vector<int>> dp(n+1,vector<int>(n+1,-1));

int f(int l,int r){
        if(dp.at(l).at(r) >= 0) return dp.at(l).at(r);
        if( l+1 == r) return dp.at(l).at(r) = 0;
        int res = INF;
        for(int i = l+1;i<r;i++){
                res = min(res,f(l,i)+f(i,r)+rc.at(l).first*rc.at(i).first*rc.at(r-1).second);
        }
        return dp.at(l).at(r) = res;
}

int main() {
        cin >> n;
        rep(i,n){
                 int r,c;
                cin >> r >> c;
                rc.at(i) = make_pair(r,c);
        }
        
        cout << f(0,n) << endl;
        
        return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
int N;
int r[105],c[105];
ll dp[105][105]={0};

void dbg()
{
	for(int i=0;i<N;i++,puts(""))
		for(int j=0;j<N;j++)
			printf("%7d ",dp[i][j]);
}

int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)
		scanf("%d%d",&r[i],&c[i]);
	for(int i=0;i<N;i++)dp[i][i]=0;
	for(int l=1;l<N;l++)
		for(int i=0;i<N-l;i++)
		{
			int j=i+l;
			dp[i][j]=10000000;
			for(int k=0;k<j;k++)
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+r[i]*c[k]*c[j]);
		}
	printf("%lld\n",dp[0][N-1]);
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<vector>

#define INF 1000000000

struct matrix {
	int row;
	int column;

};

using namespace std;

//leftはindexの添え字
int partprob_matchain(vector<vector<int>>& table, matrix* mat, int left, int right) {
	if (left == right) return 0;
	if (table.at(left).at(right) != 0) return table.at(left).at(right);
	int temp, min = INF;
	for (int i = left, j = left + 1; j <= right; i++, j++) {
		temp = partprob_matchain(table, mat, left, i);
		temp += partprob_matchain(table, mat, j, right);
		temp += mat[left].row * mat[i].column * mat[right].column;
		if (temp < min) min = temp;
	}
	table.at(left).at(right) = min;
	return min;
}


int matrix_chain(vector<vector<int>>& table, matrix* mat) {
	return partprob_matchain(table, mat, 0, table.at(0).size() - 1);
}


int main(void) {
	int size;
	cin >> size;
	matrix* mat = new matrix[size];
	vector<vector<int>> table(size, vector<int>(size));

	for (int i = 0; i < size; i++) cin >> mat[i].row >> mat[i].column;

	cout << matrix_chain(table, mat) << endl;
	delete[] mat;
	return 0;

}

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

inline int MIN(int a,int b){
	return ((a)<(b)?(a):(b));
}

int minimumProduct(int left, int right, vector<pair<int,int> >& mat, vector<vector<int> >& product) {
	if(left==right) return 0;
	if(product[left][right]!=-1) {
		return product[left][right];
	}
	int i,min=INT_MAX;
	for(i=left; i<right; ++i) {
		min=MIN(min,minimumProduct(left,i,mat,product)
				+minimumProduct(i+1,right,mat,product)
				+mat[left].first*mat[right].second*((i==right-1)?mat[right].first:mat[i].second));
	}
	return product[left][right]=min;
}

int main() {
	int n;
	int i,j;
	vector<pair<int,int> > mat;
	vector<vector<int> > product;

	scanf("%d",&n);
	mat.resize(n);
	product.resize(n);
	for(i=0; i<n; ++i) {
		product[i].resize(n);
		scanf("%d%d",&mat[i].first,&mat[i].second);
		for(j=0; j<n; ++j) {
			product[i][j]=-1;
		}
	}
	printf("%d\n",minimumProduct(0,n-1,mat,product));
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAXN = 100+7;

int p[MAXN];
int dp[MAXN][MAXN];
int n;
void generate_dp(){
    for(int i = 1; i <= n; i++){
        dp[i][i] = 0;
    }
    for(int b = 1; b < n; b++){
        for(int i = 1; i <= n-b; i++){
            int j = i+b;
            int k; k = i;
            int min_x = dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j];
            for(k = i+1; k < j; k++){
                if(dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j] < min_x){
                    min_x = dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j];
                }
            }
            dp[i][j] = min_x;
        }
    }
}
int main(){

    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d %d", &p[i], &p[i+1]);
    }
    generate_dp();
    printf("%d\n", dp[1][n]);

    return 0;
}

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

using namespace std;

typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0);

	int n;
	cin >> n;
	ll a, b;
	vector<pair<ll, ll>> v;
	for (int i = 0; i < n; ++i)
	{
		cin >> a >> b;
		v.push_back({a, b});
	}

	vector<vector<ll>> dp(n, vector<ll>(n, -1));
	function<ll(int, int)> f = [&](int i, int j)
	{
		if (i == j)
			return 0LL;

		if (dp[i][j] != -1)
			return dp[i][j];

		ll r = 1LL<<62;
		for (int k = i; k < j; ++k)
			r = min(r, f(i, k)+f(k+1, j)+v[i].first*v[j].second*v[k].second);

		return dp[i][j]=r;
	};

	cout << f(0, n-1) << "\n";

	return 0;
}

C++: #include <iostream>
using namespace std;
#define INF 200000000

int n,m[101],cm[101][101];

void calc(){
  int i,j,l,p,k;
  for (i=0;i<n;i++)cm[i][i]=0;
  for (l=1;l<n;l++){
    for(i=0;i<n-l;i++){
      j=i+l;
      cm[i][j]=INF;
      //cout << i << " "<< j << endl;
      for (k=i;k<j;k++){
	//cout << i << " "<< j <<" "<< k<<" "<<cm[i][k]<<" "<<cm[k+1][j] <<  endl;
	p=cm[i][k]+cm[k+1][j]+m[i]*m[k+1]*m[j+1];
	cm[i][j]=min(cm[i][j],p);
      }
    }
  }
}

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

  calc();

  cout << cm[0][n-1]<<endl;
  
  return 0;
}