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 <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int N = 100;

signed main() {
	ios::sync_with_stdio(false);
	int n, p[N + 1], m[N + 1][N + 1];
	cin >> n;
	int i;
	FOR(i, 1, n + 1) {
		cin >> p[i - 1] >> p[i];
	}
	FOR(i, 1, n + 1) 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] << '\n';
	return 0;
}

C++: #include <stdio.h>

int main() {	
	int rankN;
	int colRowL[101];
	int min[100][100];
	int left, right;
	int temp;
	int i, j, k;

	scanf("%d", &rankN);
	for (i = 0; i < rankN; i++) {
		scanf("%d %d", &colRowL[i], &colRowL[i + 1]);
	}

	for (i = 0; i < rankN; i++) {
		min[i][i] = 0;
	}
	for (i = 1; i < rankN; i++) {
		for (j = 0; j < rankN - i; j++) {
			left = j;
			right = j + i;
			min[left][right] = 1000000000;

			for (k = left; k < right ; k++) {
				temp = min[left][k] + min[k + 1][right] 
		                    +colRowL[left] * colRowL[k + 1] * colRowL[right + 1];
				if (temp < min[left][right]) {
					min[left][right] = temp;
				}
			}
		}
	}

	printf("%d\n", min[0][rankN - 1]);

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#define INT_MAX 100000000
using namespace std;
int index;
int mm[1000][1000];
int mine = INT_MAX;

void solve(int p[]){
  int n = index-1,j,q;
  for(int i=1;i<=n;i++)mm[i][i]=0;
  for(int l=2;l<=n;l++){
    for(int i=1;i<=n-l+1;i++){
      j = i+l-1;
      mm[i][j] = INT_MAX;
      for(int k=i;k<=j-1;k++){
	q = mm[i][k] + mm[k+1][j] + p[i-1]*p[k]*p[j];
	if(q<mm[i][j]){
	  mm[i][j] = q;
       
	}

      }
    }
  }




}

int main(){
  int p[1000],a,b;
  scanf("%d",&index);
  //for(int i=0;i<=index;i++)for(int j=0;j<=n;j++)mm[i][j] = INT_MAX;
  for(int i=0;i<index;i++){
    scanf("%d %d",&a,&b);
    p[i] = a;
    if(i == index-1)p[i+1] = b;
  }

  index = index+1;
  solve(p);
 
  printf("%d\n",mm[1][index]);
  return 0;
}

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

int main() {
    int max = 100;
    int n;
    cin >> n; // take the input
    int p[max + 1], m[max + 1][max + 1];
    for (int i = 1; i <= n; i++) {
        cin >> p[i - 1] >> p[i]; // scan in the matrix
    }

    for (int i = 1; i <= n; i++) {
        m[i][i] = 0;
        for (int t = 2; t <= n; t++) {
            for (int i = 1; i <= n - t + 1; i++) {
                int j = i + t - 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 <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;
typedef vector<pll> vec;

lli n;
vec rc;
mat dp;

lli solve(lli x,lli y){
    if(x == y) return dp[x][y] = 0;
    if(dp[x][y]) return dp[x][y];
    lli ret = 1ll << 61;
    for(lli i = x;i < y;i++){
        ret = min(ret,solve(x,i) + solve(i+1,y) + rc[i].second*rc[x].first*rc[y].second);
    }
    return dp[x][y] = ret;
}


int main(){
    cin >> n;
    rc = vec(n);
    dp = mat(n,vll(n));
    for(lli i = 0;i < n;i++) cin >> rc[i].first >> rc[i].second;
    cout << solve(0,n-1) << endl;
    return 0;

}

C++: #include <bits/stdc++.h>
#define N 110
using namespace std;
int A[N];
int dp[N][N];

int Min(int &a,int b){return a = min(a,b);}
int main(){
  int n;
  cin>>n;

  for(int i=0,cnt=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    if(i == 0) A[cnt++] = a;
    A[cnt++] = b;
  }

  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++)dp[i][j] = 1e9;
    dp[i][i] = 0;
  }
  
  for(int len=0;len<n;len++)
    for(int l=1;l<n;l++){
      int r = l+len;
      if(l+len>=N)continue;
      for(int m = l;m<r;m++){
        Min(dp[l][r],dp[l][m] + dp[m+1][r] + A[l-1]*A[m]*A[r]);
      }
}
  
  cout<<dp[1][n]<<endl; 
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namescape 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;
}