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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int N;

struct Matrix{ int R, W; };
Matrix M[MAX_N];

int mem[MAX_L][MAX_L];
Matrix memM[MAX_L][MAX_L];


void eval(Matrix M[], int s, int e){

	if (s == e){
		mem[s][e] = 0;
		memM[s][e] = M[s];
		return;
	}

	int minval = 1000000000;
	for (int mid = s; mid < e; mid++){
		Matrix m1 = memM[s][mid];
		Matrix m2 = memM[mid+1][e];
		int val = mem[s][mid] + mem[mid+1][e] + (m1.W * m1.R * m2.W);
		
		if (val < minval){
			minval = val;
			mem[s][e] = val;
			Matrix m; m.R = m1.R; m.W = m2.W;
			memM[s][e] = m;
		}
	}

}

int solve(Matrix M[], int N){

	for (int d = 0; d <= N; d++)
		for (int s = 0; s + d <= N; s++)
		{
			eval(M, s, s + d);
		}

	return mem[0][N];
}

int main() {

	cin >> N;
	rep(i, N){
		Matrix m;
		cin >> m.R;
		cin >> m.W;
		M[i] = m;
	}

	cout << solve(M, N) << endl;

#ifdef DEBUGRUN
	rep (i, N){
		rep(j, N){
			cerr << mem[i][j] << ' ';
		}
		cerr << endl;
	}
#endif

}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
 
 
int main()
{
    int n;
    cin >> n;
    vector<int> p(n + 1);
    for(int i = 0; i <= n; i++){
        int a,b;
        cin >> a >> b;
        if(!i) {
         p[i] = a;
         i++;
        }
        p[i] = b;
    }
    
    vector<vector<int>> m(n + 1, vector<int>(n + 1, 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 MOD 1000000007

int main() {
  
 int n;
 cin >> n;

 vector<long long> p(n+1);
 for(int i = 1;i <= n;i++) {
   cin >> p.at(i-1) >> p.at(i);
 }

 vector<vector<long long> > dp(n+1,vector<long long>(n+1,0));

 for(int l = 2;l <= n;l++) {
   for(int i = 1;i <= n-l+1;i++) {
     int j = i + l - 1;
     dp[i][j] = (1<<21);
     for(int k = i;k <= j-1;k++) {
       dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i-1] * p[k] * p[j]);
     }
   }
 }

 cout << dp[1][n] << endl;
 
 
  
}

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

void fnInput(vector<int>& rvnMtrx)
{
  int nMaxSiz;
  cin >> nMaxSiz;
  rvnMtrx.resize(nMaxSiz + 1);

  for (int i = 0; i < nMaxSiz; ++i)
    cin >> rvnMtrx[i] >> rvnMtrx[i + 1];
}

int fnMtrxChainMultpl(vector<vector<int>>& rvvnDetDP, const vector<int>& cnrvnMtrx)
{
  const int nMAXSIZ = cnrvnMtrx.size() - 1;
  for (int i = 1; i <= nMAXSIZ; ++i)  rvvnDetDP[i][i] = 0;

  for (int nDif = 1; nDif <= nMAXSIZ - 1; ++nDif)
    for (int nBgn = 1; nBgn <= nMAXSIZ - nDif; ++nBgn)
    {
      int nEnd = nBgn + nDif;
      for (int nx = nBgn; nx <= nEnd - 1; ++nx)
        rvvnDetDP[nBgn][nEnd] =
          min(rvvnDetDP[nBgn][nEnd],
              rvvnDetDP[nBgn][nx] + rvvnDetDP[nx + 1][nEnd] 
                                  + cnrvnMtrx[nBgn - 1] * cnrvnMtrx[nx] * cnrvnMtrx[nEnd]);
    }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnDetDP; 
  vector<int> vnMtrx;

  fnInput(vnMtrx);
  vvnDetDP.resize(vnMtrx.size(), vector<int>(vnMtrx.size(), INT_MAX));

  fnMtrxChainMultpl(vvnDetDP, vnMtrx);
  cout << vvnDetDP[1][vnMtrx.size() - 1] << endl;
  return 0;
}

C++: #include<iostream>

using namespace std;

void MATRIX_CHAIN_ORDER(int n, int p[]){
    int m[n+1][n+1];
    int s[n][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] = 2000000000;
            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;
                    s[i][j] = k;
                }
            }
        }
    }
    cout << m[1][n] << endl;
}

int main(){
    int n;
    cin >> n;
    int p[n+1];
    for(int i = 1; i <= n; i++){
        cin >> p[i-1];
        cin >> p[i];
    }
    MATRIX_CHAIN_ORDER(n, p);
}