Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

type matrixSize struct{ row, col int }

func calcNum(i, j matrixSize) int {
	return i.row * j.col * i.col
}

func parseProblem() (int, []matrixSize) {
	n := scanInt(sc)
	sizes := make([]matrixSize, n)
	for i := 0; i < n; i++ {
		sizes[i] = matrixSize{row: scanInt(sc), col: scanInt(sc)}
	}
	return n, sizes
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = math.MaxInt64
		}
	}
}

func resolve(n int, sizes []matrixSize) int {
	// TODO 復習
	initDP(n)
	_rec(sizes, 0, n)
	return dp[0][n]
}

func _rec(sizes []matrixSize, l, r int) int {
	if dp[l][r] != math.MaxInt64 {
		return dp[l][r]
	}
	if r-l <= 1 {
		dp[l][r] = 0
		return 0
	}
	num := math.MaxInt64
	for i := l + 1; i < r; i++ {
		v := _rec(sizes, l, i) + _rec(sizes, i, r) + calcNum(matrixSize{sizes[l].row, sizes[i].row}, matrixSize{sizes[i].row, sizes[r-1].col})
		if v < num {
			num = v
		}
	}
	dp[l][r] = num
	return dp[l][r]
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

const long N = 100;

int main(){
	long n, p[N + 1], m[N + 1][N + 1];
	cin >> n;
	for (long i = 1; i <= n; i++) cin >> p[i - 1] >> p[i];
	for (long i = 1; i <= n; i++) m[i][i] = 0;
	for (long l = 2; l <= n; l++){
		for (long i = 1; i <= n - l + 1; i++){
			long j = i + l - 1;
			m[i][j] = (1 << 21);
			for (long 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<iostream>
#include<cstdio>
#include<algorithm>
#define inf 1 << 21
#define MAX 100
using namespace std;

int m[MAX + 1][MAX + 1], p[MAX + 1],n;

void MCM(){
    
    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] = inf;
            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]);
            }
        }
    }
    printf("%d\n",m[1][n]);
}

int main(){
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) scanf("%d %d",&p[i-1],&p[i]);
    MCM();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)(n-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
    for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
    if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
    int l = vv.size();
    if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n  " ) << vv[i] << (i==l-1 ? " ]" : ""); }
    return o;
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

vector<pii> mat;
vvi dp;
int rec(int l, int r){
    if(dp[l][r] != INF) return dp[l][r];
    if(l == r) return dp[l][r] = 0;
    int res = INF;
    REP(i,l,r){
        int x = mat[l].fi * mat[i].se * mat[r].se;
        res = min(res, x + rec(l,i) + rec(i+1,r));
    }
    return dp[l][r] = res;
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    int n; cin >> n;
    mat = vector<pii>(n);
    rep(i,n){
        int r, c; cin >> r >> c;
        mat[i] = pii(r, c);
    }
    dp = vvi(n, vi(n, INF));
    if(n == 1) cout << 0 << endl;
    else cout << rec(0, n-1) << endl;
    return 0;
}

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

static const int N = 100;

struct Matrix{int row, col;};
Matrix M[N];
int cost[N][N] = {0};

int prod(int begin, int end){
  int mincost = 100000000, l = end - begin;
  if(cost[begin][end] > 0){
    return cost[begin][end];
  } else {
    if(l == 0){
      mincost = 0;
    } else if(l == 1){
      cost[begin][end] = M[begin].row * M[begin].col * M[end].col;
      mincost = cost[begin][end];
    } else {
      int temp;
      for(int i = 0; i < l; i++){
        temp = prod(begin, begin + i) + prod(begin + i + 1, end);
        temp += M[begin].row * M[begin + i].col * M[end].col;
        mincost = min(mincost, temp);
      }
      cost[begin][end] = mincost;
    }
    return mincost;
  }
}

int main(){
  int n;
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> M[i].row >> M[i].col;
  }

  cout << prod(0, n - 1) << endl;

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>

using namespace std;


int main()
{
    int n;
    int p[101];
    int m[101][101];

    cin >> n;

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

    for (int i = 0; i < n; i++)
        cin >> p[i] >> p[i + 1];

    for (int len = 1; (n - 1) - len >= 0; len++) {
        for (int i = 0; i <= (n - 1) - len; i++) {
            m[i][i + len] = INT_MAX / 3;
            for (int j = i; j < i + len; j++) {
                m[i][i + len] = min(m[i][i + len], m[i][j] + m[j + 1][i + len] + p[i] * p[j + 1] * p[i + len + 1]);
            }
        }
    }

    cout << m[0][n - 1] << endl;

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cassert>
#include <memory.h>
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <cassert>
typedef unsigned long long ull;
typedef unsigned long ul;
#define rep(i,n) for(unsigned long long i = 0;i<n;i++)
#define clear(c) memset(c,0,sizeof(c))
#define clearm(c) memset(c,-1,sizeof(c))
using namespace std;

int main(){
	int n;

	cin >> n;
	ull dp[100+1][100+1];
	clearm(dp);
	ull r[n],c[n];
	rep(i,n){
		int rr,cc;
		cin >> rr >> cc;
		r[i] = rr;
		c[i] = cc;
	}
	rep(i,n-1){
		dp[i][i+1] = r[i] * c[i+1] * r[i+1];
	}
	rep(i,n){
		dp[i][i] = 0;
	}
	for(int dif = 2;dif<n;dif++){
		for(int i = 0;i + dif<n;i++){
			for(int j = i + 1;j <= i +dif - 1;j++){
				dp[i][i+dif] = min(dp[i][i+dif],dp[i][j] + dp[j+1][i+dif] + r[i] * c[j] * c[i+dif]);
			}
		}
	}
	cout << dp[0][n-1] << endl;
	return 0;
}