Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	mm := make([]int, n+1)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		mm[i] = getNextInt(scanner)
		mm[i+1] = getNextInt(scanner)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			for k := i; k <= j-1; k++ {
				d := dp[i][k] + dp[k+1][j] + mm[i-1]*mm[k]*mm[j]
				if dp[i][j] == 0 || dp[i][j] > d {
					dp[i][j] = d
				}
			}
		}
	}

	fmt.Fprintln(writer, dp[1][n])

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

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;

}