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 <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const int INF = 1e9;
const int MOD = 1e9+7;
using Graph = vector<vector<ll>>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}

// 0 false, 1 true 
// string to int : -48
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll n;
vector<vector<ll>> dp(n, vector<ll>(n, INF));  //dp[i][j] i to j min cost
vector<pair<ll,ll>> vec(n);

ll calc_mult(pair<ll,ll> a, pair<ll,ll> b){
    return a.first * a.second * b.second;
}

ll calc_sec(ll a, ll b){
    if(dp[a][b] != -1){
        return dp[a][b];
    }
    if(b == a){
        return 0;
    }
    if(b - a == 1){
        dp[a][b] = vec[a].first * vec[a].second * vec[b].second;
        return vec[a].first * vec[a].second * vec[b].second;
        //return 0;
    }


    ll res = INF;
    for(ll i = a; i < b; i++){
        res = min(res, calc_sec(a, i) + calc_sec(i+1, b) + vec[a].first * vec[i].second * vec[b].second);
    }

    dp[a][b] = res;
    return res;
}


int main() {
    std::cout << std::fixed << std::setprecision(15);
    cin >> n;

    vec.resize(n);
    rep(i, n) cin >> vec[i].first >> vec[i].second;

    if(n == 1){
        cout << 0 << endl;
        return 0;
    }

    
    dp.resize(n, vector<ll>(n));
    dp.assign(n, vector<ll>(n, -1));
    rep(i,n){
        dp[i][i] = 0;
    }

    /*
    cout << calc_sec(0, 1) << endl;
    cout << calc_sec(0, 2) << endl;
    cout << calc_sec(0, 5) << endl;
    */
    /*
    rep(i,n){
        rep(j,n){
            cout << dp[i][j] << " ";
        }
        br;
    }
    */

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







}

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

//using Graph = vector<vector<edge>>;
using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define erep(i,container) for (auto i : container)
#define irep(i, n)        for(int i = n-1; i >= (int)0; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define all(x) (x).begin(),(x).end()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query int qq;std::cin >> qq;rep(qqq,qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout <<"Case #" <<qqq+1<<": "
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
#define ssum(a) accumulate(a,0ll) 

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
//typedef long long T;
const int INF = 2000000000;
const ll INF64 = 922337203685477580ll;
const ll MOD = 1000000007ll;
const dou pi=3.141592653589793;
int main(){
    int n;
    std::cin >> n;
    std::vector<int> a(n),b(n);
    rep(i,n)std::cin >> a[i]>>b[i];
    vvector(dp,int,n+1,n+1,INF);
    rep(i,n+1)dp[i][i]=0;
    rep(i,n)dp[i][i+1]=0;
    rep(i,n-1){
        dp[i][i+2]=a[i]*b[i]*b[i+1];
     //   std::cout << dp[i][i+2] << std::endl;
    }
    
    rrep(i,3,n+1){
        rep(j,n-i+1){
            //dp[j][j+i]=min(dp[j][j+i-1]+a[j]*a[j+i-1]*b[j+i-1],dp[j+1][j+i]+b[j+i-1]*a[j]*b[j]);
            rrep(ii,j+1,i+j)chmin(dp[j][j+i],dp[j][ii]+dp[ii][j+i]+a[j]*b[j+i-1]*a[ii]);
        }
    }
    std::cout << dp[0][n] << std::endl;
    
}

C++: #include <iostream>
#include <vector>

using namespace std;

int main(){
  int N; cin >> N;
  int dp[101][101];
  fill((int*)dp, (int*)dp+101*101, 1234567890);

  vector<int> r(N), c(N);
  for(int i=0;i<N;++i){
	cin >> r[i] >> c[i];
	dp[i][i+1] = 0;
  }
  
  for(int l=2;l<=N;++l){
	for(int i=0;i+l<=N;++i){
	  for(int j=i+1;j<i+l;++j){
		dp[i][i+l] = min(dp[i][j] + dp[j][i+l] + r[i]*c[i+l-1]*r[j], dp[i][i+l]);
	  }
	}
  }
  cout << dp[0][N] << endl;
  
  return 0;
}

C++: #include <iostream>
using namespace std;
#define MAX 200
#define INFTY (1<<21)

int m[MAX][MAX], n, p[MAX];

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

C++: #include<stdio.h>

static const int N = 100;

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

int main(){
  int N1[N+1],N2[N+1][N+1];
  int i,j,k,l,n;
 
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d %d",&N1[i-1],&N1[i]);
  }

  for(i=1;i<=n;i++)N2[i][i] = 0;
  for(l=2;l<=n;l++){
    for(i=0;i<=n-l+1;i++){
      j = i+l-1;
      N2[i][j] = (1<<21);
      for(k = i;k <= j-1;k++){
	N2[i][j] = min(N2[i][j],N2[i][k] + N2[k+1][j] + N1[i-1]*N1[k]*N1[j]);
      }
    }
  }

  printf("%d\n",N2[1][n]);
  
  return 0;
}