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 <climits>
using namespace std;

long int M[101][101];
long int a[100];
int n;
void solve(){
  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++){
	        long int tmp = M[i][k]+M[k+1][j]+a[i-1]*a[k]*a[j];
		if(M[i][j]>tmp){
		    M[i][j] = tmp;	
		}
	    }
	}
    }
}

int main(){ 
   cin>>n;
   for(int i=0;i<n;i+=2){
	cin>>a[i]>>a[i+1];
   }
   solve();
   for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
//	    cout<<"("<<i<<","<<j<<")"<<M[i][j]<<" ";
	}
//	cout<<endl;
   }
   cout<<M[1][n-1]<<endl;
   return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
    int n,a;
    cin>>n;
    int p[n+1];
    for(int i=0;i<=n;i++){
        cin>>p[i]>>p[i+1];
    }
    int m[n+1][n+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;
            m[i][j]=100000000;
            for(int k=i;k<=j-1;k++){
                int q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
                if(q<m[i][j]){
                    m[i][j]=q;
                }
            }
        }
    }
    cout<<m[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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(b);i>=(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
template<class T>
bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}

ll n;
ll r[105],c[105];
ll dp[105][105];
ll f(ll lx,ll rx){
    if(rx-lx==1)return 0;
    if(dp[lx][rx]!=-1)return dp[lx][rx];
    ll ret=LINF;
    fore(i,1,rx-lx-1){
        chmin(ret,f(lx,lx+i)+f(lx+i,rx)+r[lx]*r[lx+i]*c[rx-1]);
    }
    return dp[lx][rx]=ret;
}
void solve(){
    cin>>n;
    rep(i,n)cin>>r[i]>>c[i];

    rep(i,n+1)rep(j,n+1)dp[i][j]=-1;
    cout<<f(0,n)<<endl;
}

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

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;


ll dp[110][110];
bool flag[110][110];
int n;
vector<ll>p;

ll rec(ll left,ll right){
    if(flag[left][right])return dp[left][right];
    if(left == right){
        flag[left][right]=true;
        return dp[left][right]=0;
    }
    if(left>right){
        return 0;
    }
    if(right -left ==1){
        flag[left][right]=true;
        return dp[left][right] = p[left]*p[left+1]*p[left+2];
    }
    ll Min = INF;
    for(ll l=left;l<right;l++){
        Min =min(Min,rec(left,l)+rec(l+1,right)+p[left]*p[l+1]*p[right+1]);
    }
    flag[left][right]=true;
    return dp[left][right]=Min;
}

int main(void){
    cin>>n;
    vector<int>r(n),c(n);
    for(int i=0;i<n;i++)cin>>r[i]>>c[i];
    p.resize(n+2);
    p[1]=r[0];
    for(int i=2;i<=n+1;i++)p[i]=(c[i-2]);
    //dp[l][r]をl,l+1,...,r-1と定める
    cout<<rec(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<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int n, r[105], c[105];
int dp[105][105];


int main() {
    cin >> n;
    rep(i, n) cin >> r[i] >> c[i];

    rep(i, n) rep(j, n) dp[i][j] = i == j ? 0 : INF;

    for (int i = 1; i < n; i++) {
        for (int j = i-1; j >= 0; j--) {
            for (int k = 0; k < i; k++) {
                int cost = dp[i][k+1] + dp[k][j] + r[j]*c[k]*c[i];
                chmin(dp[i][j], cost);
            }
        }
    }
    cout << dp[n-1][0] << endl;
}