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>
using namespace std;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int d[n+1];
    for(int i=0;i<n;++i) cin>>d[i]>>d[i+1];
    int a[n][n];
    fill(a[0],a[n],1<<28);
    for(int i=0;i<n;++i) a[i][i]=0;
    for(int l=1;l<n;++l)
        for(int i=0;i<n-l;++i){
            int j=i+l;
            for(int k=i;k<j;++k)
                a[i][j]=min(a[i][j],a[i][k]+a[k+1][j]+d[i]*d[k+1]*d[j+1]);
        }
    cout<<a[0][n-1]<<endl;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define INF (1<<21)
using namespace std;

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

  vector<int> p(105);
  int m[105][105];

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

  for(int i = 2; i <= n; i++){
    for(int j = 1; j <= n-i+1; j++){
      int x = i + j - 1;
      m[j][x] = INF;

      for(int k = j; k <= x-1; k++){
	m[j][x] = min(m[j][x], m[j][k]+m[k+1][x] + p[j-1]*p[k]*p[x]);
      }
    }
  }

  cout << m[1][n] << endl;

  return 0;

}

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


int main() {
	int n, p[101], m[101][101];
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i - 1] >> p[i];
	
	int i, j, k, l;
	for (i = 1; i <= n; i++) m[i][i] = 0;
	for (l = 2; l <= n; l++) {
		for (i = 1; i <= n - l - 1; i++) {
			j = i + l - 1;
			m[i][j] = (1 << 21);
			for (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 <iostream>
#include <vector>
using namespace std;
const int INF = 1001001001;

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

    vector<vector<int>> memo(n, vector<int>(n, INF));
    for(int i = 0; i < n; i++) memo[i][i] = 0;

    for(int i = n-1; i >= 0; i--){
        for(int j = i; j < n; j++){
            for(int k = i; k+1 < n; k++){
                if(memo[i][j] > memo[i][k] + memo[k+1][j] + rnum[i]*rnum[k+1]*rnum[j+1]){
                    memo[i][j] = memo[i][k] + memo[k+1][j] + rnum[i]*rnum[k+1]*rnum[j+1];
                }
            }
        }
    }

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

C++: #include <bits/stdc++.h>
using namespace std;
long long dp[1005][1005];
long long a[1005];
long long b[1005];
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//	freopen("a.txt","r",stdin);
	int n;cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i]>>b[i];
	memset(dp,0xc,sizeof(dp));
	for(int i=1;i<=n;i++)dp[i][i]=0;
	for(int l=1;l<n;l++){
		for(int i=1;i<=n;i++){
			int j=i+l;
			if(j>n)continue;
			for(int k=i;k<j;k++)
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+a[i]*a[k+1]*b[j]);
		}
	}		
	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 <limits>
using namespace std;

int M[101][101] = {};
int p[101];

int matrixChain(int n)
{
    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] = std::numeric_limits<int>::max();

            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 M[1][n];
}

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

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

    cout << matrixChain(n) << endl;

    return 0;
}

C++: #include<iostream>
#include<cassert>
using namespace std;
struct mat
{
	int w;
	int h;
};
typedef struct mat mat_t;
int mulnum(mat_t,mat_t);
long long min(long long a,long long b);

int n;
mat_t a[101];
long long dp[101][101] ;

int main(){
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> a[i].h >> a[i].w;
	}
	for(int x = 0;x<n;x++){
		for(int y =0;y<n;y++){
			dp[x][y] = 2000000;
			if(x == y) dp[x][y] = 0;
		}
	}
	int i = 0;
	int j = 1;
	for(int l = 1;l<n;l++){
		i = 0;
		for(j=l;j<n;j++){
			for(int k = i;i <= k && k < j;k++){
				dp[i][j] = min(dp[i][j],dp[i][k] + dp[k+1][j] + a[i].h *a[k].w * a[j].w);
			}
			i++;
		}
	}
	/*
	for(int x = 0;x<n;x++){
		for(int y=0;y<n;y++){
			cerr << dp[x][y] << " ";
		}
		cerr << endl;
	}
	*/
	cout << dp[0][n-1] << endl;
}

int mulnum(mat_t a,mat_t b){
	assert(a.h == b.w);
	return a.h*a.w*b.h;
}

long long min(long long a,long long b) {
	return a<b? a:b;
}

C++: #include <string>
#include <utility>
#include <iostream>
 
#define MAX 100

#define INF 100000000

int left[MAX];
int right[MAX];

int DP[MAX][MAX];

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

  for(int i = 0; i < n; i++){
    std::cin >> left[i];
    std::cin >> right[i];
  }
  
  for(int i = 0; i < MAX; i++){
    for(int j = 0; j < MAX; j++){
      DP[i][j] = INF;
   }
  }

  for(int k = 0; k < n; k++){
    for(int l = 0; l + k < n; l++){
      int r = l + k;
      
      if(k == 0){
	DP[l][r] = 0;
      }else if(k == 1){
	DP[l][r] = left[l] * right[l] * right[r];
      }else{
	for(int t = l; t < r; t++){
	 
	  DP[l][r] = std::min(DP[l][r], DP[l][t] + DP[t + 1][r] + left[l] * right[t] * right[r]);
	}

	// printf("%d %d %d\n", l, r, DP[l][r]);
      }
    }
  }
  
  std::cout << DP[0][n - 1] << std::endl;
}