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
	}

	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
		}
	}
	memo[r] = minCount
	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++: // https://ei1333.github.io/luzhiled/
// http://beet-aizu.hatenablog.com/entry/2017/01/04/113233
// http://www.prefield.com/algorithm/
#include <bits/stdc++.h>
constexpr char bn = '\n';
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
using Graph = vector<vector<ll>>;
const ll INF = 1LL << 60;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
const ll tl= 1000000007;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REREP(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define REFOR(i, n, a) for (ll i = n; i >= a; i--)
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#define in1(A) REP(i,A.size()) in(A[i]);
#define in2(A,B) REP(i,A.size()) in(A[i],B[i]);
#define in3(s,t,u) REP(i,sz(s)){in(s[i] , t[i] , u[i]);}
#define in4(s,t,u,v) REP(i,sz(s)){in(s[i] , t[i] , u[i] , v[i]);}
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
struct Point{ ld x,y; };
ld dist(Point a, Point b){return sqrt(abs(a.x-b.x)*abs(a.x-b.x)+abs(a.y-b.y)*abs(a.y-b.y));} // ２点間の距離 
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} 
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
   while (n > 0) {
      if (n & 1) res = res * a % mod;
      a = a * a % mod;
      n >>= 1;
  }
  return res;
}
ll fact(ll n){ if(n < 2) return 1; return (n * fact(n-1))%tl; } //階乗
inline ll updiv(ll a,ll b){ return (a + b - 1) / b; } //切り上げ
template<typename T,typename U>ll ceil(T a,U b){return (a + b - 1) / b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO"  )
#define Yes(n) ((n) ? "Yes" : "No"  )
#define yes(n) ((n) ? "yes" : "no"  )
//-------------------------------------------------------------------------------------------------
int main(){
    init();
    ll ans= 0;
    inl(n);
    vector<pair<ll,ll>> A;
    vvl dp(n,vl(n,INF)); // dp[i][j]: 区間[i,j)の中での乗算回数最小値
    
    REP(i,n){
        inl(x,y);
        A.pb({x,y});
    }
    //対角部は0（dpテーブル初期値）
    REP(i,n){
        dp[i][i]= 0;
    }
    // REP(i,n){
    //     REP(j,n){
    //         if(dp[i][j]==INF) cout << -1 << " ";
    //         else cout<<(dp[i][j])<<" ";
    //     }out();
    // }
    // dp[i][j]: A[i]~A[j]で最小の計算
    // dp[i][j]: dp[i][k] +dp[k][j] + A[i]*A[k]*A[j]  (i<k<j) i~k番目の組み合わせとk+1~j番目の組み合わせ
    // この小さい方を選ぶ
    // 斜めに遷移するfor文
    REP(j,n){
        if(j==0) continue;
        REP(i,n-j){
            ll jj= i+j;
            FOR(k,i,jj){
                ll division= dp[i][k] + dp[k+1][jj] + A[i].first *A[k].second* A[jj].second; //or A[i].first *A[k+1].first* A[jj].second;
                chmin(dp[i][jj], division);
            // out(dp[i][jj],":",fromLeft-dp[i][jj-1],fromUnder-dp[i+1][jj]);
            }
        }
    }
    // REP(i,n){
    //     REP(j,n){
    //         if(dp[i][j]==INF) cout << -1 << " ";
    //         else cout<<(dp[i][j])<<" ";
    //     }out();
    // }
    out(dp[0][n-1]);
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include<string>
#include <set>
#include <map>
#include <queue>
#include <limits.h>
#include <bitset>
#include <cmath>

using namespace std;

#define rip(i,n,s) for (int i=(s);i<(int)(n);i++)
#define all(a) (a).begin(),(a).end()
typedef long long ll;

struct mat{
    ll r;
    ll c;
};

typedef pair<ll,mat> P;

ll cal(P &x, P &y){
    return x.first+y.first+x.second.r*y.second.r*y.second.c;
}

int main(){
    int n;
    cin >> n;
    vector<mat> da(n);
    rip(i,n,0){
        cin >> da[i].r >> da[i].c;
    }
    vector<vector<P>> dp(n,vector<P>(n));
    rip(i,n,0){
        dp[i][i].first=0;
        dp[i][i].second=da[i];
    }
    rip(i,n,1){
        rip(j,n-i,0){
            dp[j][j+i].first=LLONG_MAX;
            rip(k,i,0){
                dp[j][j+i].first=min(dp[j][j+i].first,cal(dp[j][j+k],dp[j+k+1][j+i]));
            }
            dp[j][j+i].second.r=da[j].r;
            dp[j][j+i].second.c=da[j+i].c;
        }
    }
    cout << dp[0][n-1].first << endl;
}

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

int arr[100];
int dp[100][100];

int findNumOperation(int l, int r)
{
    if(dp[l][r] != -1) return dp[l][r];
    if(r-l==1) return 0;
    else if(r-l==2) return arr[l]*arr[l+1]*arr[r];

    int mn = 99999999;
    for(int i=l+1; i<r; i++){
        int a = findNumOperation(l,i);
        int b = findNumOperation(i,r);
        int c = a+b+(arr[l]*arr[i]*arr[r]);
        if(c<mn) mn=c;
    }
    return mn;
}

int main()
{
    int n;
    cin >> n;
    for(int i=0; i<=n; i++){
        for(int j=0; j<=n; j++){
            dp[i][j] = -1;
        }
    }
    cin >> arr[0] >> arr[1];
    for(int i=2; i<n+1; i++) {
        int a,b;
        cin >> a >> b;
        arr[i] = b;
    }
    int ans = findNumOperation(0, n);
    cout << ans << endl;
}