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>
#include <vector>
#include <climits>
using namespace std;

const int INF = INT_MAX/3;
typedef vector<int> vi;
typedef pair<int,int> pii;

int main(){
    int n;cin>>n;
    vector<pii> p(n+1);
    for(int i=0;i<n;i++) cin>>p[i].first>>p[i].second;

    vector<vi> mat(n+1,vi(n+1,INF));    //mat[i][j] : Mi*...*Mj???????°??????°
    for(int i=0;i<n;i++) mat[i][i] = 0;

    for(int sz=2;sz<=n;sz++){
        /*
         * mat[i][j] = min( mat[i][k]+mat[k+1][j]+Mi.first*Mk+1.first*Mk+1.second )
         */
        for(int i=0;i<=n-sz;i++){
            int j = i + sz - 1;

            for(int k=i;k<=j-1;k++){
                mat[i][j] = min(mat[i][j] , mat[i][k] + mat[k+1][j] + p[i].first * p[k+1].first * p[j].second);
            }
        }
    }
    cout << mat[0][n-1] << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
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 -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<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;

// O(n^3)
// dp[i][j] := [i,j]????????????????????????????°?????????????????????°
// i??????????????????(?????°????????°)??????(p[i - 1], p[i])
int matrixChainMultiplication(int n, int p[105]){
    int dp[105][105] = {{0}};
    range(seg, 2, n + 1){ //?¨????????????????????????°
        range(i, 1, n - seg + 2){
            int j = i + seg - 1;
            dp[i][j] = INT_MAX;
            range(k,i,j){
                int cost = dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j];
                dp[i][j] = min(dp[i][j], cost);
            }
        }
    }
    return dp[1][n];
}

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

    int p[105];
    rep(i,n){
        cin >> p[i] >> p[i + 1];
    }
    cout << matrixChainMultiplication(n, p) << endl;
}

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 nlist[110];
int dp[110][110];

int main(){
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>nlist[i]>>nlist[i+1];
  }
  for(int i=1;i<n;i++){
    for(int j=0;j<n-i;j++){
      dp[j][j+i]=1<<30;
      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]+nlist[j]*nlist[j+k+1]*nlist[j+i+1]);
      }
    }
  }
  cout<<dp[0][n-1]<<endl;
  return 0;
}

C++: #include <iostream>

using namespace std;

int m[124][124] = {{}};
int p[124] = {};
int n;

void chain(){
  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;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;

}


int main(){

  cin >> n;

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

  chain();

}

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

static const int N=100;

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 <stdio.h>
#include <algorithm>

const int N = 100;
int n, p[N + 1], dp[N + 1][N + 1];

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

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

	for (int k = 2; k <= n; k++)
	{
		for (int i = 1; i <= n - k + 1; i++)
		{
			int j = i + k - 1;
			dp[i][j] = 1 << 20;
			for (int s = i; s <= j - 1; s++)
				dp[i][j] = std::min(dp[i][j], dp[i][s] + dp[s + 1][j] + p[i - 1] * p[s] * p[j]);
		}
	}
	printf("%d\n", dp[1][n]);

	return 0;
}

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

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

int main() {
  int n; 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] = INF;
      for ( int k = i; k <= j-1; k++ ) {
	int q = M[i][k] + M[k+1][j] + p[i-1]*p[k]*p[j];
	M[i][j] = min(M[i][j],q);
      }
    }
  }
  cout << M[1][n] << endl;
  return 0;
}