Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	mm := make([]int, n+1)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		mm[i] = getNextInt(scanner)
		mm[i+1] = getNextInt(scanner)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			for k := i; k <= j-1; k++ {
				d := dp[i][k] + dp[k+1][j] + mm[i-1]*mm[k]*mm[j]
				if dp[i][j] == 0 || dp[i][j] > d {
					dp[i][j] = d
				}
			}
		}
	}

	fmt.Fprintln(writer, dp[1][n])

	writer.Flush()
}

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

using namespace std;

int main() {
    int n; 
    cin >> n; 
    int r, c;
    vector<pair<int, int>> v; 
    vector<int> dims; 
    for (int i = 0; i < n; ++i) { 
            cin >> r >> c; 
            if (i == 0) { 
                dims.push_back(r);
            } 
        dims.push_back(c);
    }
    // for (int i = 0; i < dims.size(); ++i)
    //     cout << dims[i] << " ";
    int dp[n][n];
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            dp[i][j] = 0;
    
    for (int len = 2; len <= n; ++len) { 
        for (int i = 0; i <= n - len; ++i) { 
            int j = i + len-1;
            // cout << i << " " << j <<" -> ";
            dp[i][j] = INT_MAX;
            for (int k = i; k < j; ++k) { 
                // cout << dp[i][k] << " " << dims[i] << " " << dims[k+1] << " " << dims[j+1] << endl;
               int q = dp[i][k] + dp[k+1][j] + dims[i]*dims[k+1]*dims[j+1]; 
               dp[i][j] = min(q, dp[i][j]);
            }
        }
    }
    // for (int i = 0; i < n; ++i) {
    //     for (int j = 0;j < n; ++j)
    //         cout << dp[i][j] << " ";
    //     cout << endl;
    // }
    cout << dp[0][n-1] << endl;
   
}

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

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

#define MAX 100005
#define INF INT_MAX

typedef long long ll;
typedef pair<int, int> P;

P a[MAX];
map<P, int> memo;

int minMultTime(int s, int e)
{
    if (s >= e) return 0;
    if (memo[P(s,e)]) return memo[P(s,e)];

    int ans = INF;
    rep(i,e-s) {
        int m1 = minMultTime(s,s+i);
        int m2 = minMultTime(s+i+1,e);
        int m3 = a[s].first * a[s+i].second * a[e].second;
        ans = min(ans, m1+m2+m3);
    }
    memo[P(s,e)] = ans;

    return ans;
}

int main(int, char**)
{
    int n;
    cin >> n;

    rep(i,n) {
        cin >> a[i].first;
        cin >> a[i].second;
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1000000000
typedef long long ll;
using namespace std;

int main()
{
	ll n;
	cin >> n;
	vector<ll> M(n + 1);
	cin >> M[0];
	for (int i = 1; i < n; i++) {
		cin >> M[i] >> M[i];
	}
	cin >> M[n];
	vector<vector<ll>> dp(n, vector<ll>(n, INF));
	for (int i = 0; i < n; i++) {
		dp[i][i] = 0;
	}
	for (ll k = 1; k < n; k++) {
		for (ll i = 0; i + k < n; i++) {
			for (ll j = 0; j <= i + k - 1; j++) {
				ll cost = dp[i][j] + dp[j + 1][i + k] + M[i] * M[j + 1] * M[i + k + 1];
				if (cost < dp[i][i + k]) {
					dp[i][i + k] = cost;
				}
			}
		}
	}
	cout << dp[0][n - 1] << endl;
	return 0;
}

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

using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
//typedef long long ll;
typedef long long ll;
//typedef pair<int, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
const ll INF = 10e15;
const ll MINF = -10e10;
//const int INF = INT_MAX / 100;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)


//ifstream myfile("~/Downloads/02.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;
const int mod = 1000000007;
const double PI = 3.14159265358979323846;

typedef pair<ll, ll> P;


string to_upper(string str) {
    int diff = 'a' - 'A';
    for (int i = 0; i < str.size(); i++) {
        char c = str[i];
        if ('A' <= c && c <= 'Z') {
            c += diff;
            str[i] = c;
        }
    }
    return str;
}


int main() {

    int n;
    cin >> n;


    vector<P> matrix_list(n);
    rep(i, n) cin >> matrix_list[i].first >> matrix_list[i].second;

    vector<ll> p(n + 1, 0);
    p[0] = matrix_list.front().first;
    rep(i, n) p[i + 1] = matrix_list[i].second;

    vector<vector<ll> > dp(n + 1, vector<ll>(n + 1, 0));


    for (int l = 2; l <= n; l++) {  // 行列の個数
        for (int i = 1; i <= n - l + 1; i++) {
            int j = i + l - 1;
            ll min_cost = INF;
            for (int k = i; k < j; k++) {
                if (k == i) min_cost = dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j];
                else min_cost = min(min_cost, dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j]);
            }
            dp[i][j] = min_cost;
        }
    }

    cout << dp[1][n] << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;}
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;



int main()
{
        ll n;
        cin >> n;
        vll a(n+1);

        rep(i, 1, n+1) cin >> a[i-1] >> a[i];

        //print1d(a);

        vllvll dp(110, vll(110, inf));
        rep(i, 0, 110) dp[i][i] = 0;
        
        rep(width, 2, n+1) {
            rep(left, 1, n-width+2) {
                ll right = left + width - 1;
                rep(div, left, right) {
                    chmin(dp[left][right], dp[left][div] + dp[div+1][right] + a[left-1] * a[div] * a[right]);
                }
            }
        }

        cout << dp[1][n] << endl;
}