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 "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){T x; cin >> x; return (x);}
template<typename T=ll,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

template<typename T>
bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>
bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}

vector<pair<ll,ll>> P;
vvll memo;

ll dfs(int l,int r){
	if(memo[l][r]>=0) return memo[l][r];
	if(r-l<2){
		return 0;
	}
	
	ll V=1LL<<60;
	for(int i=l+1;i<r;i++){
		V = min(V,dfs(l,i)+dfs(i,r)+P[l].first*P[i].first*P[r-1].second);
	}
	memo[l][r] = V;
	return V;
}

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

	int N=in();
	P.resize(N);
	memo.resize(N+1,vll(N+1,-1));
	REP(i,N){
		P[i].first=in();
		P[i].second=in();
	}
	
	out(dfs(0,N));
	return 0;
}

C++: // input: alds1_10_b.in
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define NIL -1

struct Matrix {
  Matrix(int r, int c)
    : r(r), c(c){
  }
  int r;
  int c;
};

int costs[101][101];

void init_costs() {
  for(int i=0; i<101; i++) {
    for(int j=0; j<101; j++) {
      costs[i][j] = NIL;
    }
  }
}

int cost(vector<Matrix>& ms, int start, int end) {
  if(costs[start][end] != NIL) {
    return costs[start][end];
  }

  if(start == end) {
    costs[start][end] = 0;
    return 0;
  }

  if(end - start == 1) {
    Matrix s = ms.at(start);
    Matrix e = ms.at(end);
    int c = s.r * s.c * e.c;
    costs[start][end] = c;
    return c;
  }

  vector<int> cs;
  for(int i=start; i<end; i++) {
    int c1 = cost(ms, start, i);
    int c2 = cost(ms, i+1, end);
    int c3 = ms.at(start).r * ms.at(i).c * ms.at(end).c;
    cs.push_back(c1 + c2 + c3);
  }
  int c = *min_element(cs.begin(), cs.end());
  costs[start][end] = c;

  return c;
}

int solve(vector<Matrix>& ms) {
  return cost(ms, 0, ms.size()-1);
}

int main() {
  init_costs();

  int n;
  cin >> n;

  vector<Matrix> ms;

  for(int i=0; i<n; i++) {
    int r, c;
    cin >> r >> c;

    ms.push_back(Matrix(r, c));
  }

  cout << solve(ms) << endl;
}

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

#define _CRT_SECURE_NO_WARNINGS

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

using namespace std;

const int INF = 1000000000;
const double PI = 3.1415926535897932384626433832795;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

using LL = long long int;
using LD = long double;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<LL, LL>;
using vi = vector<int>;
using vd = vector<double>;
using vvi = vector<vi>;
using vll = vector<LL>;

//==============================================

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

	
	int n;
	cin >> n;

	vi p(150);
	REP3(i, 1, n) {
		cin >> p[i - 1] >> p[i];
	}

	vvi m(150, vi(150));
	REP3(i, 1, n) m[i][i] = 0;
	REP3(l, 2, n) {
		REP3(i, 1, n - l + 1) {
			int j = i + l - 1;
			m[i][j] = (1 << 21);
			REP3(k, i, j - 1) {
				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] << "\n";
	return 0;
}

C++: // http://algorithms.blog55.fc2.com/blog-entry-66.html

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>

#define M 100

int N,R[M+1],C[M][M],B[M][M];

void compute(){
	int i=0,j,k,cost;
	memset(C,99,sizeof(C));
	for(;i<N;i++)C[i][i]=0;
	for(j=1;j<N;j++)
		for(i=0;i<N - j; i++ )
			for (k = i; k < i + j; k++ ){
				cost = C[i][k] + C[k+1][i+j] + R[i]*R[k+1]*R[i+j+1];
				if(cost<C[i][i+j])C[i][i+j]=cost,B[i][i+j]=k; 
			}
}

void order( int i, int j ){
	if ( i == j ) cout << "M" << i;
	else{
		cout << "(";
		order( i, B[i][j]-1 ); order(B[i][j], j);
		cout << ")";
	}
}

void input(){
	cin >> N;
	int z,i=0;
	for (; i < N; i++ ) cin >> R[i]>>z;
	R[i]=z;
	//SIZE++;
}

void output(){
	cout <<  C[0][N-1] << endl;
	//order(0, N);
	//cout << endl;
}

main(){
	input();
	compute();
	output();
}