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 <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define int long long
using namespace std;
int dp[111][111],n,L[111],R[111];

int dfs(int l,int r){
	if(dp[l][r]!=-1)return dp[l][r];
	if(l==r)return 0;
	int res=1e17;
	for(int i=l;i<r;i++){
		res=min(res,dfs(l,i)+dfs(i+1,r)+L[l]*R[i]*R[r]);
	}
	return dp[l][r]=res;
}

signed main(){
	memset(dp,-1,sizeof(dp));
	cin>>n;
	r(i,n)cin>>L[i]>>R[i];
	dfs(0,n-1);
	cout<<dp[0][n-1]<<endl;
}

C++: #include <iostream>
#include<climits>
using namespace std;
int m[100][100];

int MCM(int p[],int n) {
	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] = INT_MAX;
			for (int k = i; k <= j; k++) {
				int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
				if (m[i][j] > q) {
					m[i][j] = q;
				}

			}
		}

	}
	return m[1][n];
}

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


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

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

int n;
int matrix[110][2];
int _dp[110][110];

int dp(int begin,int end){
	if(_dp[begin][end]) return _dp[begin][end];
	if(begin + 1 == end){
		_dp[begin][end] == 0;
		return 0;
	}
	if(begin + 2 == end){
		int temp = matrix[begin][0] * matrix[begin][1] * matrix[end-1][1];
		_dp[begin][end] = temp;
		return temp;
	}
	int min = INF;
	for(int i = begin+1; i<end; i++){
		int temp = dp(begin, i) + dp(i, end)
			+ matrix[begin][0] * matrix[i][0] * matrix[end-1][1];
		if(temp < min) min = temp;
	}
	_dp[begin][end] = min;
	return min;
}

int main(void){
	int n;
	cin >> n;
	
	for(int i=0; i<n; i++) cin >> matrix[i][0] >> matrix[i][1];
	
	cout << dp(0,n) << endl;
	
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 100

int main(){
  int n,dammy;
  cin>>n;
  int p[MAX+1];
  cin>>p[0];
  for(int i=0;i<n-1;i++){
    cin>>p[i+1];
    cin>>dammy;
  }
  cin>>p[n];
  int C[MAX+1][MAX+1];
  for(int i=1;i<=n;i++) C[i][i]=0;
  for(int h=2;h<=n;h++){
    for(int s=1;s<=n-h+1;s++){
      int e=s+h-1;
      C[s][e]=C[s][s]+C[s+1][e]+p[s-1]*p[s]*p[e];
      for(int j=s+1;j<e;j++){
        C[s][e]=min(C[s][e],C[s][j]+C[j+1][e]+p[s-1]*p[j]*p[e]);
      }
    }
  }
  cout<<C[1][n]<<endl;
}

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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


int main() {

    int n;
    cin >> n;
    vector<int> R(n);
    vector<int> C(n);
    REP(i,n) cin >> R[i] >> C[i];
    vector<vector<int>> dp(n,vector<int>(n,INF));

    REP(i,n) dp[i][i] = 0;

    FOR(w,1,n){
        FOR(l,0,n-w){
            int r = l+w; 
            FOR(m,l,r){
                dp[l][r] = min(dp[l][r], dp[l][m] + dp[m+1][r] + R[l]*C[m]*C[r]);
            }
        }

    }

    cout << dp[0][n-1] << endl;
    


    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
using namespace std;
  int n;

int m[101][101];
  int p[101];
void chain(){
  int n1;
  int j;
  int q;
  n1=n;
  //ok
  // cout <<"n1 "<<n1<<endl;
  for(int i=1;i<=n1;i++){
    m[i][i]=0;
  }
  for(int l=2;l<=n1;l++){
    for(int i=1;i<=n1-l+1;i++){
      j=i+l-1;
      m[i][j] = INT_MAX;
      for(int k=i;k<=j-1;k++){
	q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
	//	cout<<"q "<<q<<endl;
	if(q<m[i][j]){
	  m[i][j]=q;
	  //	  cout<<s[i][j]<<endl;
	}
      }
    }
  }
}

int main(){

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

  //ok
  /*
 for(int i=0;i<=n;i++){
   cout<<"p "<<p[i]<<endl;
 }
  */
  chain();
  /*  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout<<m[i][j]<<endl;
    }
   }
*/
  cout<<m[1][n]<<endl;

  return 0;
}

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

using namespace std;
int N = 100;

int main(){
  int i,j,k,l;
  int n,p[N+1],m[N+1][N+1];
  cin >> n;
  for(i = 1; i <= n; i++){
    cin >> p[i-1];
    cin >> p[i];
  }
  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;
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
static const int N = 100;


int main() {
	int n;
	cin >> n;
	int p[N],m[N+1][N+1];
	for (int i = 1;i <= n;i++) {
		cin >> p[i - 1] >> p[i];
	}
	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]);
			}
		}
	}
	cout << m[1][n] << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
static const int N=100;
int main(){
  int j,i,k,n,l,p[N+1],m[N+1][N+1];
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d%d",&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-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[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
        }
    }
  }
  printf("%d\n",m[1][n]);
  return 0;
}