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 <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

ll n;
vector<ll> m;
vector<vector<ll>> table;

#define DP table[left][right]
ll dfs(ll left=0, ll right=n + 1) {
  if (DP != -1) return DP;

  if (right - left <= 2) return DP = 0;
  ll minv = 1ll << 60;
  FOR(c, left + 1, right - 2) {
    minv = min(minv, dfs(left, c + 1) + dfs(c, right) + m[left] * m[c] * m[right - 1]);
  }
  return DP = minv;
}

int main() {
  cin >> n;
  m = vector<ll>(n + 1);
  REP(i, n) {
    cin >> m[i] >> m[i + 1];
  }

  table = vector<vector<ll>>(n + 2, vector<ll>(n + 2, -1));

  cout << dfs() << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    cout << fixed << setprecision(10);
    
    int n; cin >> n;
    vector<P> mt(n);
    rep(i, n) {
        cin >> mt[i].first >> mt[i].second;
    }
    // iからjまでの区間で最小の数
    vector<vector<int>> dp(n, vector<int>(n));

    // 区間を小さい方から始める
    repr(i, 2, n+1) {
        // iは区間の大きさ 2 <= i <= nまで
        int wj = i-1;
        rep(sj, n+1-i) {
            // jは区間の始める点, i = 2のとき、jは 0 <= j < n+1 - 2
            int lj = sj + wj;
            // [sj, wj]間を二つに分割して一番最小なコストを探索する
            // 分割点の候補は [sj, K), [K, lj]で、sj+1 < K < lj
            int min_cost = 100000001;
            repr(fk, sj+1, lj+1) {
                // fkは 分割するkの場所
                min_cost = min(min_cost, dp[sj][fk-1] + dp[fk][lj] + mt[sj].first * mt[fk].first * mt[lj].second);
            }
            dp[sj][lj] = min_cost;
        }
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

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

    int n;
    cin >> n;

    vector<int> r(n), c(n);
    rep(i, n) cin >> r[i] >> c[i];

    const int INF = 1001001001;
    vector<vector<int>> dp(n, vector<int>(n, INF));
    rep(i, n) dp[i][i] = 0;
    for(int w = 1; w < n; ++w) for(int i = 0; i < n-w; ++i){
        int j = i + w;
        for(int k = i; k < j; ++k){
            dp[i][i+w] = min(dp[i][i+w], dp[i][k] + dp[k+1][i+w] + r[i]*c[k]*c[j]);
        }
    }

    // rep(i, n){
    //     rep(j, n) cout << dp[i][j] << " ";
    //     cout << endl;
    // }

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

    return 0;
}

C++: /**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int r[45] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);


int f(int n, int k, int m)
{
	return f(n,k) + f(k+1,m) + (r[n] * r[k+1] * r[m+1]);
}

int f(int n, int m)
{
	if (n==m) return 0;
	int & ra = a[n][m];
	if (ra) return ra;
	ra = INT_MAX;
	for (int k=n;k<m;k++)
	{
		int ans = f(n,k,m);
		if (ans < ra) ra = ans;
	}
	return ra;
}

int main()
{
	int rr = 0;
	int cc = 0;
	int n = 0;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cin >> rr >> cc;
		r[i]=rr;
		r[i+1]=cc;
	}
	for (int i=0;i<n-1;i++)
	{
		a[i][i+1] = r[i] * r[i+1] * r[i+2];
	}
	int ans = f(0,n-1);
	cout << ans << endl;
}