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<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

ll dp[210][210];
// dp[i][j] は from i to jまで　の積の計算の最小回数

int main(){
  ll N; cin >> N;
  vector<P> a(N); rep(i,N) cin >> a[i].fi >> a[i].sc;

  // dp 初期化
  rep(i,N) rep(j,N) dp[i][j] = INF;
  rep(i,N) dp[i][i] = 0;

  repn(i,N){
    // iはいくつ離れてるか
    rep(j,N){
      // j is start
      for(ll k = j; k <= i + j; k++){
        // k が空いてた
        if(k == j){
          dp[j][j + i] = min(dp[j][j + i] , a[k].fi * a[k].sc * a[j + i].sc  + dp[j + 1][j + i]);
        }
        else if(k == i + j){
          dp[j][j + i] = min(dp[j][j + i] , dp[j][j + i - 1] + a[j].fi * a[k].fi * a[k].sc);
        }
        else{
          dp[j][j + i] = min(dp[j][j + i] ,
            dp[j][k] + a[j].fi * a[k].sc * a[j + i].sc + dp[k + 1][j + i]);
        }
      }
    }
  }

  // rep(i,N) rep(j,N) cout << i << " " << j << " " << dp[i][j] << endl;

  cout << dp[0][N - 1] << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <map>
#define MAX_NUM 100
#define INFTY 1000000000

using namespace std;

//think easy make easy

int n;//num of input


pair<int,int> MatrixChain[MAX_NUM+1];

int calc(int s,int e){//s~eの連鎖行列積の最少計算回数を出力
	int min_time  = INFTY;

	int min_tmp;
	if(s == e){
		return 0;
	}
	for(int i=s;i<e;i++){
		min_tmp = calc(s,i) + calc(i+1,e) 
			+MatrixChain[s].first*MatrixChain[i].second*MatrixChain[e].second;
		if(min_tmp < min_time)
			min_time = min_tmp;
	}
	return min_time;
}

int main(){
	scanf("%d",&n);
	int r,c;//row,column

	for(int i=0;i<n;i++){
		scanf("%d %d",&r,&c);
		MatrixChain[i] = pair<int,int>(r,c);//i:1~n-1
	}

	cout <<calc(0,n-1) <<endl;

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

int main() {
    int n;
    cin>>n;
    int m[n+1][n+1],p[n+1];
    rep(i,n){
        cin>>p[i]>>p[i+1];
        m[i][i]=0;
    }
    m[n][n]=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]=1000000;
            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;
}

C++: #include<iostream>

#define N 101

int n;
int a[N];
int g[N][N];

using namespace std;
int main() {

    cin>>n;
    for(int i = 0; i < n; i++) cin>>a[i]>>a[i+1];
    

    int i = 1, j = 1, k = 1, min, b;
    while(1) {
        if(i == 1 && j == n+1) break;
        if(i == j) g[i][j] = 0;
        
        if(i != j) {
            min = (1<<21);
            for(int x = i; x < j; x++) {
                b = g[i][x] + g[x+1][j] + a[i-1] * a[x] * a[j];
                if(min > b) min = b;
            }
        
            g[i][j] = min;
        }
        
        i++;
        j++;
        if(j > n) {
            i = 1;
            j = ++k;
        }
    }

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

    return 0;
}

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


int main(){
    int m[MAX][MAX];
    int p[MAX];
    int n;
    //FILE *fw=fopen("a.txt","r");
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d %d",&p[i],&p[i+1]);
    }
    //printf("start\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 = l+i-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]*p[k+1]*p[j+1]);
            }
        }
    }
    printf("%d\n",m[1][n]);
    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++: #define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main() { 
  int M[101][101];
  int P[100];
  rep(i,101){
    rep(j,101) M[i][j]=1e9;
  }
  
  int n;
  cin>>n;
  
  rep(i,n) cin>>P[i]>>P[i+1];
  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;
      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;
  
  return 0;
}

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

int main(){
  int n, p[101], m[101][101], i, j, k, l;
  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 - 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;
}