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 <bits/stdc++.h>
using namespace std;
#define INF 1e8;

int matrixChainMultiplication(int *p, int n) {
  int m[n+1][n+1];
  for (int i = 0; i <= n; i++) m[i][0] = 0;
  for (int j = 0; j <= n; j++) m[0][j] = 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] = INF;
      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]);
      }
    }
  }
  //上のループで、DPテーブル対角線上のゼロに近接するマスが順に埋まっていく
  //最終的に右端のm[1][n]がM1M2...Mnを計算する際の最小計算回数になる

  return m[1][n];
}

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

C++: #include <bits/stdc++.h>
using namespace std;

static const int N=100;

int main(){
  int i,l,n,p[N+1],m[N+1][N+1];
  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(l=2;l<=n;l++){
    for(i=1;i<=n-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 <vector>
#include <array>
#include <string>

const int size = 105;
const int NIL = -1;
int dp[size][size];
int ps[size];

int min_cost(int i, int j)
{
  if (dp[i][j] != NIL)
    return dp[i][j];

  int k=i;
  int min = min_cost(i,k) + min_cost(k+1,j) + ps[i-1]*ps[k]*ps[j];
  for (k=i+1; k<=j-1; ++k)
    {
      int tmp = min_cost(i,k) + min_cost(k+1,j) + ps[i-1]*ps[k]*ps[j];
      if (tmp < min) min = tmp;
    }
  dp[i][j] = min;
  return min;
}


int main()
{
  int n;
  std::cin >> n;

  for (int i=0; i<n; ++i)
    {
      std::cin >> ps[i];
      std::cin >> ps[i+1];
    }

  for (int i=0; i<size; ++i)
    for (int j=0; j<size; ++j)
      dp[i][j] = NIL;

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

  std::cout << min_cost(1,n) << std::endl;
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
using namespace std;

static const int N = 100;

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

  REP1(i, n + 1) m[i][i] = 0;
  FOR(l, 2, n + 1) {
    REP1(i, n - l + 2) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      FOR(k, i, j) {
        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;

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

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

using namespace std;

typedef long long ll;
#define endl '\n'

int n;
ll r[111],c[111],d[111];
ll dp[111][111];
const ll oo = 1000000000000000LL;

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

	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> r[i] >> c[i];
	d[0] = r[1];
	for(int i = 1; i <= n; i++)
		d[i] = c[i];
	for(int size = 1; size <= n;size++){
		for(int i = 1; i <= n - size; i++){
			int j = i + size;
			dp[i][j] = oo;
			for(int k = i; k <= j - 1;k++){
				ll cost = dp[i][k] + dp[k + 1][j] + d[i - 1]*d[k]*d[j];
				dp[i][j] = min(dp[i][j],cost);
			}
		}
	}
	
	cout << dp[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;

const long N = 100;

int main(){
	long n, p[N + 1], m[N + 1][N + 1];
	cin >> n;
	for (long i = 1; i <= n; i++){
		cin >> p[i - 1] >> p[i];
	}

	for (long i = 1; i <= n; i++) m[i][i] = 0;
	for (long l = 2; l <= n; l++){
		for (long i = 1; i <= n - l + 1; i++){
			long j = i + l - 1;
			m[i][j] = (1 << 21);
			for (long k = 0; 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;
    int DP[101][101];
    int *p;
int rec(int r,int c){
    int Min =100000001;
    
    for(int i=0;i<c-r;i++){
        Min =min(Min,DP[r][r+i] + DP[r+i+1][c]+p[r-1]*p[r+i]*p[c]);
    }
    return Min;
}
int main(void){
    int n,bb;
    cin>>n;
    p =new int[n+1];
    cin>> p[0];
    cin>>p[1];
    for(int i=1;i<=n ;i++) DP[i][i]=0;;
    for(int i=2;i<=n;i++){
        cin>>bb;
        cin>>p[i];
    }
    for(int i=1;i<n;i++){
        DP[i][i+1]=p[i-1]*p[i]*p[i+1];
    }
    for(int j=0;j<=n-3;j++){
        for(int i=3+j;i<=n;i++){
            DP[i-2-j][i] =rec(i-2-j,i);
        }
    }
    cout <<DP[1][n]<<endl;
}