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 <cstdio>
#include <algorithm>
using namespace std;

int MatrixChainMaltiplication(int *p, int n) {
    int m[n+1][n+1];
    for (int i=0; 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]);
            }
        }
    }
    return m[1][n];
}

int
main()
{
    int n;
    scanf("%d", &n);
    int p[n+1];
    for (int i=0; i<n; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        if (i == 0) p[i] = a;
        p[i+1] = b;
    }
    printf("%d\n", MatrixChainMaltiplication(p, n));
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long llong;
#define MAX 100
#define INFTY 9999999

// 行列群を保持しておく
vector<int> vecMtr(MAX);
vector<vector<int>> dp(MAX,vector<int>(MAX));

int
MultipleMatrixChain(int size)
{
  for (int i = 1; i < size; i++)
  {
    dp[i][i] = 0;
  }
  
  for (int l = 2; l <= size; l++)
  {
    for (int i = 1; i <= size-l+1; i++)
    {
      int j = i + l - 1;
      dp[i][j] = INFTY;
      for (int k = i; k <= j-1; k++)
      {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]+ vecMtr[i-1]*vecMtr[k]*vecMtr[j]);
      }
    }
  }
  return dp[1][size];
}

int main() {
  int n;
  cin >> n;
  
  int r = 0;
  int c = 0;

  for (int i = 1; i <= n; i++)
  {
      cin >> r >> c;
      vecMtr[i-1] = r;
      vecMtr[i] = c;
  }
  
  cout << MultipleMatrixChain(n) << "\n";
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, r[100], c[100], dp[100][101];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d%d", &r[i], &c[i]), dp[i][i] = 0;
	for(int i = 1; i < n; i++) {
		for(int j = 0; i + j <= n; j++) {
			dp[j][i + j] = min(dp[j][i + j - 1], dp[j + 1][i + j]) + r[j] * c[i + j - 1];
		}
	}
	printf("%d\n", dp[0][n]);
	return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second
#define mp make_pair
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N;
ll r[105], c[105], DP[105][105];

ll Calc(int L, int R){
  if (DP[L][R] != 0) return DP[L][R];
  if (L == R) return DP[L][R] = 0;

  ll ans = INF;
  for (int k = L; k < R; k++){
    ll now = r[L] * c[k] * c[R];
    now += Calc(L, k) + Calc(k + 1, R);
    ans = min(now, ans);
  }
  
  return DP[L][R] = ans;
}

int main(){
  cin >> N;
  for (int i = 0; i < N; i++) cin >> r[i] >> c[i]; 
  cout << Calc(0, N - 1) << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000;
long long matrix_chain_multiplication(vector<long long> &A){
    int n = A.size() - 1;
    vector<vector<long long>> dp(n, vector<long long>(n, INF));
    for (int i = 0; i < n; i++){
        dp[i][i] = 0;
    }
    for (int i = 1; i < n; i++){
        for (int j = 0; j < n - i; j++){
            for (int k = j; k < j + i; k++){
                dp[j][j + i] = min(dp[j][j + i], dp[j][k] + dp[k + 1][j + i] + A[j] * A[k + 1] * A[j + i + 1]);
            }
        }
    }
    return dp[0][n - 1];
}
int main(){
  int n;
  cin >> n;
  vector<long long> H(n + 1);
  for (int i = 0; i < n; i++){
    cin >> H[i] >> H[i + 1];
  }
  cout << matrix_chain_multiplication(H) << endl;
}

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("%d\n",dp[0][N-1]);
	return 0;
}

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

static const int N = 100;

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];
}

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[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;

static const int N=100;

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];
  }

  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 n, r[109], c[109];
long long dp[109][109];

const long long MAX = __LONG_LONG_MAX__;

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++)dp[i][j] = MAX;
  }
  for(int i=0;i<n;i++)cin >> r[i] >> c[i];
  for(int i=0;i<n;i++)dp[i][i] = 0;
  for(int i=1;i<n;i++){
    for(int j=0;j+i<n;j++){
      for(int k=0;k<i;k++){
        dp[j][j+i] = min(dp[j][j+i], dp[j][j+k]+dp[j+k+1][j+i] + r[j]*c[j+k]*c[j+i]);
      }
    }
  }
  cout << dp[0][n-1] << endl;
}