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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

void print_vi(vector<int> vi)
{
	rep(i, vi.size())
	{
		printf("%d ", vi[i]);
	}
}

static const int N = 100;

signed main()
{
	int n, p[N + 1], m[N + 1][N + 1];
	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] = (1 << 21);
			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;
}

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

typedef pair<int, int> pii;

int main(int argc, char *argv[]){
  int n;
  cin >> n;
  pii mat[n];
  int dp[n][n];
  int p[n + 1];

  //????????????
  for (int i = 0; i < n; i++) {
    cin >> mat[i].first >> mat[i].second;
  }
  for (int i = 0; i < n; i++) {
    p[i] = mat[i].first;
  }
  p[n] = mat[n - 1].second;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if(i != j)dp[i][j] = 1e9;
      else dp[i][j] = 0;
    }
  }
  //???????????§?????????
  //????????????DP
  for (int l = 2; l <= n; l++) {
    for (int i = 0; i <= n - l; i++) {
      int j = i + l - 1;
      for (int k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] +
                       p[i]*p[k + 1]*p[j + 1]);
      }
    }
  }
  std::cout << dp[0][n - 1] << std::endl;
            
  return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
ll memo[111][111];
int n, a[111], b[111];
ll rec(int l, int r){
    if(memo[l][r] != 1e9) return memo[l][r];
    if(l == r) return memo[l][r] = 0;
    ll ret = 1e9;
    FOR(m, l, r){
      ret = min(ret, rec(l, m) + rec(m + 1, r) + a[l] * b[m] * b[r]);
    }
    return memo[l][r] = ret;
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  FOR(i,0,n) cin >> a[i] >> b[i];
  FOR(i,0,111)FOR(j,0,111)memo[i][j]=1e9;
  cout << rec(0,n-1) << endl;
}

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

#define N 100

int main() {
  int i, j, k, l, n, p[N+1], cost[N+1][N+1], min_cost;

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

  // 初期値
  for (i = 1; i <= n; i++) cost[i][i] = 0;

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

  cout << cost[1][n] << endl;

  return 0;
}

C++: #include<algorithm>
#include<cstdio>
#include<cstring>
#include<limits>
#define rep(i,a) for(int i=0;i<(a);++i)
#define clr(a,v) memset((a),(v),sizeof(a))

const int MAX_N = 100, INF = std::numeric_limits<int>::max()>>2;

int n;
int r[MAX_N], c[MAX_N];
int dp[MAX_N][MAX_N];

int rec( int b, int e )
{
    if( b == e )
        return dp[b][e] = 0;

    if( ~dp[b][e] )
        return dp[b][e];

    int ret = INF;

    rep( i, e-b )
        ret = std::min( ret, rec( b, b+i )+rec( b+i+1, e )+r[b]*c[b+i]*c[e] );

    return dp[b][e] = ret;
}

int main()
{
    scanf( "%d", &n );
    rep( i, n )
        scanf( "%d%d", r+i, c+i );

    clr( dp, -1 );

    printf( "%d\n", rec( 0, n-1 ) );

    return 0;
}

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

int r[105];
int c[105];
int cache[105][105];

int f(int a, int b) {
    if (a == b) return 0;
    if (cache[a][b] == -1) {
        cache[a][b] = 2000000000;
        for (int i = a; i < b; i++) {
            int res = f(a, i) + f(i + 1, b) + r[a] * c[i] * c[b];
            if (res < cache[a][b]) cache[a][b] = res;
        }
    }
    return cache[a][b];
}

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> r[i];
        cin >> c[i];
    }
    memset(cache, -1, sizeof(cache));
    cout << f(0, n-1);
}

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

class MatrixChainMultiplication {
private:
	int n, *r, *c;
	int memory[100][100];
public:
	MatrixChainMultiplication(int _n, int _r[], int _c[]) {
		n = _n;
		r = _r;
		c = _c;
		for (int i=0; i<n; i++) {
			for (int j=1; j<=n; j++) {
				memory[i][j] = INT_MAX;
			}
		}
	}
	int count(int start, int end) {
		if (memory[start][end] != INT_MAX)
			return memory[start][end];
		
		if (start + 1 == end) {
			memory[start][end] = 0;
		} else {
			int temp;
			for (int i=start+1; i<end; i++) {
				temp = count(start, i) + count(i, end) + (r[start] * r[i] * c[end - 1]);
				if (memory[start][end] > temp) memory[start][end] = temp;
			}
		}
		return memory[start][end];
	}
};

int main() {
	int n, r[100], c[100];
	
	cin >> n;
	for (int i=0; i<n; i++) {
		cin >> r[i] >> c[i];
	}
	
	auto mcm = MatrixChainMultiplication(n, r, c);
	
	cout << mcm.count(0, n) << '\n';
	
	return 0;
}