Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	p := make([]int, N+1)
	dp := make([][]int, N+1)
	for i, _ := range dp {
		dp[i] = make([]int, N+1)
	}
	for i := 0; i < N; i++ {
		p[i] = sc.nextInt()
		p[i+1] = sc.nextInt()
	}
	for i := 1; i <= N; i++ {
		dp[i][i] = 0
	}
	for l := 2; l <= N; l++ {
		for i := 1; i <= N-l+1; i++ {
			j := i + l - 1
			dp[i][j] = 1 << 60
			for k := i; k <= j-1; k++ {
				dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j])
			}
		}
	}

	fmt.Fprintln(wtr, dp[1][N])
	wtr.Flush()
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;
typedef pair<int, int> P;
const int INF = 1<<22;

// dp[i][j] := [i , j]??????????????????????????則???????????????????属???即????????属
// |(4*5   (5*3)  3*2)|  2*1
int main()
{
	int n, dp[128][128], r[128];
	P T[128];

	cin >> n;

	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
		 	dp[i][j] = INF;
		}
	}
	/*
	for(int i = 0; i < n; i++)
	{
		dp[i][i] = 0;
	}
	*/
	for(int i = 0; i < n; i++)
	{
		cin >> T[i].first >> T[i].second;
		dp[i][i] = 0;
	}
	/*
	r[0] = T[0].first;
	for(int i = 0; i < n; i++)
	{
		r[i+1] = T[i].second;
	}
	*/
	/*
	for(int i = 0; i <= n; i++)
	{
		cout << r[i] << endl
	}
	*/
	/*
	for(int l = 1; l < n; l++)
	{
		for(int i = 0; i < n-l; i++)
		{ 
			int j = i + l;
			for(int k = 1; k <= l; k++)
			{
				dp[i][j] = std::min(dp[i][j], dp[i][i + k - 1] + dp[i + k][j] + T[i].first*T[i + k].first*T[j].second); //r[j]*r[k]*r[i+j+1]);
			}
		}
	}
*/
	for(int i = 1; i < n; i++)
	{
		for(int j = 0; j < n-i; j++)
		{ 
			for(int k = 1; k <= i; k++)
			{
				dp[i][j] = std::min(dp[i][j], dp[k-1][j] + dp[i-k][j+k] + T[j].first*T[j + k].first*T[j + i].second); //r[j]*r[k]*r[i+j+1]);
				printf("%3d : %3d : %3d => %3d\n", i, j, k, dp[i][j]);
				//puts("run");
			}
		}
	}

	/*
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			printf("%10d ",dp[i][j]);
		}
		puts("");
	}
	*/

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

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
const int N = 101;

int n;
long m[N][N], p[N+2];
int j;

int matrixChainOrder(){
  for(int i=0; i<n; i++){ m[i][i] = 0; }
  for(int l=2; l<=n; l++){
    for(int i=1; i+l-1<=n; i++){
      j = i+l-1;
      m[i][j] = 100000000;
      for(int k=i; k<j-1; k++){
        m[i][j] = std::min(m[i][j], m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]);
      }
    }
  }
  return m[1][n];
}

int main(){
  std::cin >> n; //number of matrix

  for(int i=1; i<=n; i++){
    std::cin >> p[i-1] >> p[i];
  }
  std::cout << matrixChainOrder() << std::endl;
  return 0;
}

C++: #include <cassert>
#include <climits>
#include <iostream>
#include <vector>

class Matrix {
  public:
    long long x;
    long long y;
    long long cost;
};

int main()
{
  int n;
  std::cin >> n;
  std::vector<Matrix> matrix(n);
  for (int i = 0; i < (int)matrix.size(); i++) {
    std::cin >> matrix.at(i).x >> matrix.at(i).y;
  }
  std::vector<std::vector<Matrix>> memo(n+1, std::vector<Matrix>(n));
  for (int i = 0; i < (int)matrix.size(); i++) {
    memo.at(1).at(i).x = matrix.at(i).x;
    memo.at(1).at(i).y = matrix.at(i).y;
  }
  for (int block = 2; block <= (int)matrix.size(); block++) {
    for (int i = 0; i + block <= (int)matrix.size(); i++) {
      long long result = LLONG_MAX;
      memo.at(block).at(i).x = matrix.at(i).x;
      memo.at(block).at(i).y = matrix.at(i+block-1).y;
      for (int part = i+1; part < i + block; part++) {
        long long tmp = 0;
        assert(memo.at(part-i).at(i).y == memo.at(block-part+i).at(part).x);
        tmp += memo.at(part-i).at(i).cost + memo.at(block-part+i).at(part).cost;
        tmp += memo.at(part-i).at(i).x * memo.at(part-i).at(i).y * memo.at(block-part+i).at(part).y;
        result = std::min(result, tmp);
      }
      memo.at(block).at(i).cost = result;
    }
  }
  if (matrix.size() == 1) {
    std::cout << "0" << std::endl;
  } else {
    std::cout << memo.at(matrix.size()).at(0).cost << std::endl;
  }
  return 0;
}

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

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int dp[100][100];
int size[100][2];

int main() {
    int n;
    cin >> n;
    REP(i, 100) REP(j, 100)
        dp[i][j] = INF;
    REP(i, n)
        cin >> size[i][0] >> size[i][1];
    REP(i, n) for(int j = 0; j < n - i; j++){
        if(i == 0){
            dp[i][j] = 0;
            continue;
        }
        if(i == 1){
            dp[i][j] = size[j][0] * size[j][1] * size[j+1][1];
            continue;
        }
        REP(k, i){
            dp[i][j] = min(dp[i][j], dp[k][j] + dp[i-1-k][j+k+1] + size[j][0] * size[j+k][1] * size[j+i][1]);
        }
    }
    cout << dp[n-1][0] << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdint>

using namespace std;

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

    vector<int32_t> r(n), c(n);
    for (int32_t i = 0; i < n; i++) {
        cin >> r[i] >> c[i];
    }

    vector<int32_t> table(n*n);
    for (int32_t i = 0; i < n; i++) {
        table[i*n+i] = 0;
    }
    for (int32_t i = n-1; i >= 0; i--) {
        for (int32_t j = i+1; j < n; j++) {
            table[i*n+j] = INT32_MAX;
            for (int32_t k = i; k < j; k++) {
                table[i*n+j] = min(table[i*n+j], table[i*n+k] + table[(k+1)*n+j] + r[i] * c[k] * c[j]);
            }
        }
    }

    cout << table[n-1] << endl;

    return 0;
}