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<cstdio>
#include<iostream>
#include<vector>
using namespace std;
const int MAX_V = 100;
vector<pair<int,int> > pairs;
int main(){
  int n; scanf("%d",&n);
  for(int i=0;i<n;i++){
    pair<int,int> p;
    scanf("%d %d",&p.first,&p.second);
    pairs.push_back(p);
  }
  long long int sum=0;
  while(pairs.size()>1){
    vector<pair<int,int> >::iterator min_ptr;
    int min=0x7fffffff;
    int mulMin=0x7fffffff;
    for(auto iter=pairs.begin()+1;iter<pairs.end();iter++){
      int localMin = (*(iter-1)).first;
      if(localMin > (*iter).second) localMin = (*iter).second;
      int mul=(*(iter-1)).first*(*iter).second;
      if(min>=localMin){
	if(mulMin>mul){
	  min=localMin;
	  mulMin=mul;
	  min_ptr=iter;
	}
      }
    }
    sum+=(*(min_ptr-1)).first*(*min_ptr).first*(*min_ptr).second;
    (*(min_ptr-1)).second=(*min_ptr).second;
    pairs.erase(min_ptr);
  }
  printf("%lld\n",sum);
}

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

const int N = 100;
const long int Max = pow(10, 10);

int setmatrixmulti(int p[], int n){
    int m[N+1][N+1];
    int minval;
    for(int l=1; l<=n; l++){
        for(int i=1; i<=n - l + 1; i++){
            int j = i + l - 1;
            m[i][j] = Max;
            if(i == j){
                m[i][j] = 0;
            } else {
                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]);
                }
            }
        }
    }
    minval= m[1][n];
    return minval;
}

int main(){
    int n, p1, p2;
    cin >> n;
    int p[n+1];
    for(int i=0; i<n; i++){
        cin >> p1 >> p2;
        if(i == 0){
            p[i] = p1;
            p[i+1] = p2;
        } else {
            p[i+1] = p2;
        }
    }

    // for(int i=0; i<n+1; i++){
    //     cout << p[i] << " " ;
    // }

    int ans = setmatrixmulti(p, n);
    cout << ans << endl;

    return 0;
}

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

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

int main() {
	int n, p[1000], m[1000][1000];
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i - 1] >> p[i];
	MatrixChainMultiplication(n, p, m);
	cout << m[1][n] << endl;

	
}

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

using namespace std;

typedef long long ll;

const ll INF = (1LL << 60);

pair<ll, ll> A[110];
ll dp[110][110];

ll matrix_chain(int i, int j){
	if(i == j) return 0;
	if(dp[i][j] != INF) return dp[i][j];
	for(int k = i;k < j; ++k) dp[i][j] = min(dp[i][j], matrix_chain(i, k) 
			+ matrix_chain(k+1, j) + A[i].first*A[k].second*A[j].second);
	return dp[i][j];
}

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;

	for(int i=0;i<n;++i) for(int j=0;j<n;++j) dp[i][j]=INF;

	for(int i=0;i<n;i++) cin >> A[i].first >> A[i].second;
	
	cout << matrix_chain(0, n-1);

	return 0;
} // br1

C++: #pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;
//
//ll matrix_chain_multiplications(vector<int>& p) {
//    int n = p.size() - 1; // num of matrix
//    vector<vll> dp(n + 1, vll(n + 1, INF));
//
//    // init
//    FOR(i, 1, n + 1) dp[i][i] = 0;
//
//    FOR(l, 2, n + 1) {
//        FOR(i, 1, n - l + 2) {
//            int j = i + l - 1;
//            FOR(k, i, j) {
//                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i - 1] * p[k] * p[j]);
//            }
//        }
//    }
//
//    return dp[1][n];
//}

// 0-indexed
ll matrix_chain_multiplications(vector<int>& p) {
    int n = p.size() - 1; // num of matrix
    vector<vll> dp(n, vll(n, INF)); // dp[l][r] = minimum cost of [l, r], 0 <= l, r < n

    // init
    FOR(i, 0, n) dp[i][i] = 0;

    FOR(l, 2, n + 1) { // l = 2 ~ n
        REP(i, n - l + 1) {
            int j = i + l - 1;
            FOR(k, i, j) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i] * p[k + 1] * p[j + 1]);
            }
        }
    }

    return dp[0][n - 1];
}


int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n; cin >> n;

    vi p(n + 1);
    REP(i, n) cin >> p[i] >> p[i + 1];

    print(matrix_chain_multiplications(p));
}