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>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define MAX_N 100
#define INF 1000000000


int N;
int r[MAX_N],c[MAX_N];
int memo[MAX_N][MAX_N];
int saiki(int i ,int j){
    if(memo[i][j] > -1)
        return memo[i][j];
    if(i == j)
        return 0;
    int mini = INF;
    FOR(k,i,j){
        mini = min(mini,saiki(i,k) + saiki(k + 1,j) + r[i] * c[k] * c[j]);
    }
    return memo[i][j] = mini;
}

int main(){
    scanf("%d",&N);
    REP(i,N){
        scanf("%d%d",&r[i],&c[i]);
    }
    REP(i,N)REP(j,N)memo[i][j] = -1;
    printf("%d\n",saiki(0,N-1));
    return 0;
}

C++: #include <iostream>

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

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

  int c[n + 1][n + 1];
  for (int i = 1; i <= n; ++i) c[i][i] = 0;

  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n - l + 1; ++i) {
      int j = i + l - 1;
      c[i][j] = 1 << 21;
      for (int k = i; k < j; ++k) {
        c[i][j] =
            std::min(c[i][j], c[i][k] + c[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  std::cout << c[1][n] << '\n';
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <iomanip>
  
using namespace std;

#define pi 3.141592653589793

int solve(int n, int p[])
{
	int result[101][101];
	for(int i = 0; i < n + 1; i++){
		for(int j = 0; j < n + 1; j++){
			if(i == j) result[i][j] = 0;
			else result[i][j] = 1000000;
		}
	}
	for(int i = 2; i < n + 1; i++){
		for(int j = 1; j < n - i + 2; j++){
			for(int k = j; k < j + i - 1; k++){
				result[j][j + i - 1] = min(result[j][j + i - 1], result[j][k] + result[k + 1][j + i - 1] + p[j - 1] * p[k] * p[j + i - 1]);
			}
		}
	}
	// i : 掛け合わせる行列の個数
	// j : 掛け合わせる行列のうち一番左のもの
	// k : 左から何番目で分けるか
	return result[1][n];
}

int main()
{
	int n;
	cin >> n;
	int p[101];
	for(int i = 1; i < n + 1; i++){
		cin >> p[i - 1] >> p[i];
	}
	cout << solve(n, p) << endl;
	return 0;
}

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

int row[110],column[110];

int dp[110][110];

const int inf = 1LL << 60;

int dfs(int left, int right){ 
  
  if( dp[left][right] != inf ){ return dp[left][right];} // メモ化再帰
  
  int res = inf;
  for(int i = left; i < right; i++){ 
    res = min(res, dfs(left,i)+dfs(i+1,right)+row[left]*column[i]*column[right]);
  }
  
  return dp[left][right] = res;
}

signed main(void){
  
  int n; cin >> n;
  for(int i = 1; i <= n; i++){ 
    cin >> row[i] >> column[i];
  }
  
  for(int i = 0; i < 110; i++){ 
    for(int j = 0; j < 110; j++){ 
      dp[i][j] = inf;
    }
  }

  for(int i = 1; i <= n; i++){
    dp[i][i] = 0;
  }
  
  for(int i = 1; i < n; i++){ 
    // Mi*M(i+1);
    dp[i][i+1] = row[i]*column[i]*column[i+1];
  }
  
  cout << dfs(1,n) << endl;
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;//行列のやつ
const long long  INF = 999999999;
vector<int> p;
vector<vector<int>> dp;
int matrix(){
    int n = p.size()-1;
    for(int i=1;i<=n;i++){
        dp[i][i] = 0;
    }
    for(int l=2;l<=n;l++){
        for(int i=1;i<=n-l+1;i++){
            int j = i+l-1;
            dp[i][j] = INF;
            for(int k=i;k<=j-1;k++){
                int q = dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j];
                dp[i][j] = min(q,dp[i][j]);
            }
        }
    }
    
    return dp[1][n];
}
int main(){
    int n;cin >> n;
    p.resize(n+1);
    dp.assign(n+1,vector<int>(n+1));
    for(int i=0;i<n;i++){
        cin >> p[i] >> p[i+1];
    }
    
    cout << matrix() << endl;
}
/*
 *
 * 連鎖行列積
n 個の行列の連鎖 M1,M2,M3,...,Mn が与えられたとき、スカラー乗算の回数が最小になるように積 M1M2M3...Mn の計算順序を決定する問題を連鎖行列積問題(Matrix-Chain Multiplication problem)と言います。

n 個の行列について、行列 Mi の次元が与えられたとき、積 M1M2...Mn の計算に必要なスカラー乗算の最小の回数を求めるプログラムを作成してください。

入力
入力の最初の行に、行列の数 n が与えられます。続く n 行で行列 Mi(i=1...n) の次元が空白区切りの２つの整数 r、c で与えられます。r は行列の行の数、c は行列の列の数を表します。

出力
最小の回数を１行に出力してください。

制約
*/
/*
1≤n≤100
1≤r,c≤100
入力例 1
6
30 35
35 15
15 5
5 10
10 20
20 25
出力例 1
15125
*/

C++: #include<iostream>
#include<algorithm>
using namespace std;
const int N = 100;
int main()
{
	int n, dimen[N + 1], cost[N + 1][N + 1];
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> dimen[i - 1] >> dimen[i];
	for (int i = 1; i <= n; i++)
		cost[i][i] = 0;
	for (int cnt = 2; cnt <= n; cnt++)
	{
		for (int i = 1; i <= n - cnt + 1; i++)
		{
			int j = i + cnt - 1;
			cost[i][j] = (1 << 21);
			for (int k = i; k <= j - 1; k++)
				cost[i][j] = min(cost[i][j], cost[i][k]+cost[k + 1][j] + dimen[i - 1] * dimen[k] * dimen[j]);
		}
	}
	cout << cost[1][n] << endl;
}

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

const int N = 110;
int x[N],y[N];
int dp[N][N];

int matrix(int l,int r){
  if(dp[l][r] > -1) return dp[l][r];
  int res = (1 << 30);
  for(int i = l; i < r; i++){
    res = min(res,matrix(l,i)+matrix(i+1,r)+y[l]*x[i]*x[r]);
  }
  return dp[l][r] = res;
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n; cin >> n;
  for(int i = 0; i < n; i++){
    cin >> y[i] >> x[i];
  }

  fill(dp[0],dp[N],-1);
  for(int i = 0; i < N; i++) dp[i][i] = 0;
  cout << matrix(0,n-1) << endl;

  return 0;
}