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 <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>

struct Matrix
{
	int rows;
	int columns;
};

int main()
{
	int n;
	std::cin >> n;
	//There's n matrixes.
	
	if(n == 1)
	{
		std::cout << 0 << std::endl;
		return 0;
	}
	
	Matrix matrix[1 + n];
	
	for(int i = 1;i <= n;i++)
	{
		//cin >> r, rows >> c, columns.
		std::cin >> matrix[i].rows >> matrix[i].columns;
	}
	
	long long int DPMem[1 + n][1 + n] = {0};
	
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			DPMem[i][j] = 9223372036854775807;
	
	//DP
	
	//init DPMem of M(i) * M(i) and M(i) * M(i + 1).
	for(int i = 1;i <= n - 1;i++)
	{
		DPMem[i][i] = 0;
		DPMem[i][i + 1] = matrix[i].rows * matrix[i].columns * matrix[i + 1].columns;
		
		//Debug.
		//printf("DPMem[%i][%i] = %lli \n",i,i + 1,DPMem[i][i + 1]);
	}
	DPMem[n][n] = 0;
	
	//Debug.
	//std::cout << std::endl;
	
	//DP main.
	for(int len = 3;len <= n;len++)		//len refers to length of multiplying matrix chain
	{
		//Debug.
		//printf("Length = %i \n",len);
		for(int head = 1;head + len - 1 <= n;head++)
		{
			int tail = head + len - 1;
			for(int mid = head;mid < tail;mid++)
			{
				//Debug.
				/*
				printf("DPMem[%i][%i] = %lli \n",head,tail,DPMem[head][tail]);
				printf("head = %i, mid = %i,mid + 1 = %i, tail = %i \n",head,mid,mid + 1,tail);
				*/
				
				DPMem[head][tail] = std::min(DPMem[head][tail],DPMem[head][mid] + DPMem[mid + 1][tail] + matrix[head].rows * matrix[mid].columns * matrix[tail].columns);
				
				//Debug.
				/*
				printf("DPMem[%i][%i] = %lli \n",head,mid,DPMem[head][mid]);
				printf("DPMem[%i][%i] = %lli \n",mid + 1,tail,DPMem[mid + 1][tail]);
				printf("matrix[%i].rows * matrix[%i].columns(matrix[%i].rows) * matrix[%i].columns = %i \n",head,mid,mid + 1,tail,matrix[head].rows * matrix[mid].columns * matrix[tail].columns);
				printf("DPMem[%i][%i] = %lli \n",head,tail,DPMem[head][tail]);
				std::cout << std::endl;
				*/
			}
		}
	}

	//Output.
	std::cout << DPMem[1][n] << std::endl;
	
	return 0;
}

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

typedef long long ll;

vector<ll> r(100);
vector<ll> c(100);
vector<vector<ll>> dp(101, vector<ll>(101,-1));

ll solve(ll a, ll b)
{
    if(dp.at(a).at(b) != -1){
        return dp.at(a).at(b);
    }
    if(b-a == 1){
        return dp.at(a).at(b) = 0;
    }else if(b-a == 2){
        return dp.at(a).at(b) = r.at(a)*c.at(a)*c.at(b-1);
    }else{
        vector<ll> tmp;
        ll i;
        for(i = a+1;i <= b-1;++i){
            ll tmp1 = solve(a, i);
            ll tmp2 = solve(i, b);
            tmp.push_back(tmp1 + tmp2 + r.at(a)*r.at(i)*c.at(b-1));
        }
        sort(tmp.begin(), tmp.end());
        return dp.at(a).at(b) = tmp.front();
    }
}

int main()
{
    ll i,j;
    ll n;
    cin >> n;
    for(i = 0;i < n;++i){
        cin >> r.at(i) >> c.at(i);
    }
    ll ans = solve(0,n);
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
const int INF=1<<21;
int p[105];
int m[105][105];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>p[i-1]>>p[i];
    for(int i=1;i<=n;i++)m[i][i]=0;

        /* l==1时，是每两个相乘，l=3个时，是三个与剩下的几个相乘，l=4，是四个矩阵与剩下的
        相乘的最小*/
    for(int l=2;l<=n;l++)       //对于每个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]);
            }
        }
    }
    cout<<m[1][n]<<endl;
    return 0;
}

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

const int MAX_SIZE = 101;

int dp[MAX_SIZE][MAX_SIZE];

int solve(int p[], int begin,int end) {
	if (dp[begin][end] != -1)return dp[begin][end];
	if (begin >= end - 1)return 0;
	int res = -1;
	for (int i = begin + 1; i < end; i++) {
		int temp = solve(p, begin, i) + solve(p, i, end) + (p[begin] * p[i] * p[end]);
		if (res != -1) {
			res = min(temp, res);
		}
		else {
			res = temp;
		}
	}
	return dp[begin][end] = res;
}

int main() {
	int N;
	int p[MAX_SIZE + 1];

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

	for (int i = 0; i < MAX_SIZE; i++) {
		for (int j = 0; j < MAX_SIZE; j++) {
			dp[i][j] = -1;
		}
	}

	cout << solve(p, 0, N) << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define INFTY (1<<21)

static const int N = 100;

int p[N + 1];
int m[N + 1][N + 1];

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> p[i - 1] >> p[i];
	}

	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] = INFTY;
			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]);
			}
		}
	}

	cout << m[1][n] << endl;
	return 0;
}

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

// (n, m) @ (m, k) = (n, k)のとき、スカラー乗算の回数はn*m*k回
// 最適な分割を探していく

struct matrix
{
    int r, c;
};

int min_chain_mul(vector<matrix> M) {
    int c[M.size() + 1][M.size() + 1];
    for (int i = 1; i <= M.size(); i++) {
        c[i][i] = 0;
    }

    for (int l = 2; l <= M.size(); l++) { // span
        for (int i = 1; i <= M.size() - l + 1; i++) {
            int j = i + l -1;
            c[i][j] = (1 << 21);
            for (int k = i; k <= j - 1; k++) {
                c[i][j] = min(c[i][j], c[i][k]+c[k+1][j] + M.at(i-1).r * M.at(k-1).c * M.at(j-1).c);
            }
        }
    }
    // for (int i = 1; i <= M.size(); i++)
    //     cout << c[i][i+1] << endl;
    return c[1][M.size()];
}

int main() {
    int n;
    cin >> n;

    vector<matrix> M;
    for (int i = 0; i < n; i++)
    {
        matrix m;
        cin >> m.r >> m.c;
        M.push_back(m);
    }

    cout<< min_chain_mul(M) << endl;
}