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 (r Range) length() int {
	return r.end - r.start + 1
}

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[r.start].r, matrices[r.end].c}, v
	}
	if r.length() == 0 {
		panic("must not be empty")
	} else if r.length() == 1 {
		return matrices[r.start], 0
	}

	var minCount int = math.MaxInt32

	for i := 0; i < r.length()-1; i++ {
		formerRange := Range{r.start, r.start + i}
		latterRange := Range{r.start + i + 1, r.end}
		matF, countF := calcMinMatrixChainMultiplication(matrices, formerRange, memo)
		matL, countL := calcMinMatrixChainMultiplication(matrices, latterRange, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	memo[r] = minCount
	return Matrix{matrices[r.start].r, matrices[r.end].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>
using namespace std;

template<typename T, typename U> void dmin(T &x, U y) { if(x > y) x = y; }

const int MAX_N = 100;
const int INF = 1001001001;

int main() {
  int n, r, c;
  int rc[MAX_N][2];
  cin >> n;
  for (int i=0; i<n; i++) {
    cin >> r >> c;
    rc[i][0] = r; rc[i][1] = c;
  }

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

  for (int l=2; l<=n; l++) {
    for (int i=0; i+l-1<n; i++) {
      for (int j=1; j<l; j++) {
        dmin(dp[i][i+l-1], dp[i][i+j-1] + rc[i][0]*rc[i+j-1][1]*rc[i+l-1][1] + dp[i+j][i+l-1]);
      }
    }
  }

  cout << dp[0][n-1] << endl;
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
const int M = 128;
const int inf = (1<<25);

int d[M][M], p[M+2];
int path[M][M];

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d %d", p+i, p+i+1);
	
	for (int i = 1; i <= n; ++i) d[i][i] = 0;
	for (int ln = 2; ln <= n; ++ln)	// 枚举矩阵链的长度 
	{
		int m = n - ln + 1; 		// 剩余矩阵的长度 
		
		for (int i = 1; i <= m; ++i)// len-ln = j-i+1 j = ln+i-1
		{
			int j = ln + i - 1;
			d[i][j] = inf;			
			for (int k = i; k < j; ++k)
				d[i][j] = min(d[i][j], d[i][k] + d[k+1][j] + p[i-1] * p[k] * p[j]);
		}		
	}
	
	printf("%d\n", d[1][n]);
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
int N;
int r[105],c[105];
ll dp[105][105]={0};

void dbg()
{
	for(int i=0;i<N;i++,puts(""))
		for(int j=0;j<N;j++)
			printf("%7d ",dp[i][j]);
}

int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)
		scanf("%d%d",&r[i],&c[i]);
	for(int i=0;i<N;i++)dp[i][i]=0;
	for(int l=1;l<N;l++)
		for(int i=0;i<N-l;i++)
		{
			int j=i+l;
			dp[i][j]=1e15;
			for(int k=i;k<j;k++)
			{
				//printf("%d %d %d %d\n",i,j,i+l,k);
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+r[i]*c[k]*c[j]);
			}
		}
	//dbg();
	printf("%lld\n",dp[0][N-1]);
	return 0;
}

C++: #include<iostream>
using namespace std;
struct mat
{
	int w;
	int h;
};
typedef struct mat mat_t;
long long min(long long a,long long b);

int n;
mat_t a[101];
long long dp[101][101] ;

int main(){
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> a[i].h >> a[i].w;
	}
	for(int x = 0;x<n;x++){
		for(int y =0;y<n;y++){
			dp[x][y] = 2000000;
			if(x == y) dp[x][y] = 0;
		}
	}
	int i = 0;
	int j = 1;
	for(int l = 1;l<n;l++){
		i = 0;
		for(j=l;j<n;j++){
			for(int k = i;i <= k && k < j;k++){
				dp[i][j] = min(dp[i][j],dp[i][k] + dp[k+1][j] + a[i].h *a[k].w * a[j].w);
			}
			i++;
		}
	}
	cout << dp[0][n-1] << endl;
}

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

C++: /*
 * chain_matrix.cpp
 *
 *  Created on: 2017/07/11
 *      Author: LitMc
 */

//#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout<<#x<<": "<<x<<endl
#define dbp2(x,y) cout<<#x<<","<<#y<<": "<<x<<","<<y<<endl
#define dbp3(x,y,z) cout<<#x<<","<<#y<<","<<#z<<": "<<x<<","<<y<<","<<z<<endl
#define dbp4(w,x,y,z) cout<<#w<<","<<#x<<","<<#y<<","<<#z<<": "<<w<<","<<x<<","<<y<<","<<z<<endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x,y)
#define dbp3(x,y,z)
#define dbp4(w,x,y,z)
#define ifcin(x)
#endif
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for(int i=from; i<to; ++i)
#define REP(i, from, to) for(int i=from; i<=to; ++i)
#define EPS = 1e-14;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::max;
using std::min;
using std::swap;
using std::string;
using std::fill;
using std::pair;
using std::sort;
using std::reverse;
using std::pair;
using std::greater;
using std::priority_queue;
using std::ostream;

template<typename T>
ostream& operator<<(ostream& out, const vector<vector<T> >& v) {
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i] << endl;
  }
  return out;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last) {
      out << ",";
    }
  }
  out << "]";
  return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

typedef pair<int, int> P;
bool cmp(const P &p1, const P &p2) {
  if (p1.first == p2.first) {
    return p1.second > p2.second;
  } else {
    return p1.first < p2.first;
  }
}
typedef pair<int, int> P;
static const int MAX_N = 110;
static const int INF = 100000000;
int n, r, c;
P M[MAX_N];
int dp[MAX_N][MAX_N];

//dp[i][j]: M[i]??????M[j]?????§??????????????????????°??????°
//dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + prod)
//k = [i, j)
//prod = M[i].row * M[k].col * M[j].col

void solve() {
  cin >> n;
  rep(i,0,n)
  {
    cin >> M[i].first >> M[i].second;
    dp[i][i] = 0;
  }
  for (int i = 0; i + 1 < n; ++i) {
    dp[i][i + 1] = M[i].first * M[i].second * M[i + 1].second;
    dbp4(i, M[i], M[i + 1], dp[i][i + 1]);
  }
  for (int num_matrix = 3; num_matrix <= n; ++num_matrix) { //number of matrix A*B*C -> 3
    for (int i = 0, j = i + num_matrix - 1; j < n; ++i, ++j) {
      dbp4(i, M[i], M[j], dp[i][j]);
      int mini = INT_MAX;
      for (int k = i; k + 1 <= j; ++k) {
        int prod = M[i].first * M[k].second * M[j].second;
        mini = min(mini, dp[i][k] + dp[k + 1][j] + prod);
      }
      dp[i][j] = mini;
      dbp4(i, M[i], M[j], dp[i][j]);dbp("----------");
    }
  }
  cout << dp[0][n - 1] << endl;
}

int main() {
  solve();
}