Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(m[0][n-1])
}

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;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#define Lens 105

using namespace std;
int p[Lens];
int C[Lens][Lens];
int N;

int main()
{
	cin >> N;
	for (int i = 1; i <= N; i++)
	{
		cin >> p[i - 1] >> p[i];
		C[i][i] = 0;
	}
	for (int k = 1; k < N; k++)
	{
		for (int i = 1; i <= N-k; i++)
		{
			int j = i + k;
			C[i][j] = C[i][i] + C[i + 1][j]+p[i-1]*p[i]*p[j];
			for (int t = i+1; t < j; t++)
			{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
				C[i][j] = min(C[i][j], C[i][t] + C[t+1][j] + p[i - 1] * p[t] * p[j]);
			}
		}
	}
	cout << C[1][N]<<endl;

	return 0;

}

C++: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector<vector<int>>
typedef long long ll;
const ll INF = 1000000007;

vector< vector<ll> > M(110, vector<ll>(110, 0));
vector<ll> P(110);
int N;

ll multiplication(){
    for(int l = 2; l <= N; l++){
        for(int i = 1; i <= N - l + 1; i++){
            int j = i + l - 1;
            M[i][j] = INF * INF;
            for(int k = i; k < j; k++){
                M[i][j] = min(M[i][j], M[i][k] + M[k+1][j] + P[i-1] * P[k] * P[j]);
            }
        }
    }
    return M[1][N];
}

int main(){
    cin >> N;
    rep(i, N){
        cin >> P[i] >> P[i+1];
    }
    cout << multiplication() << endl;
}