Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	for i := 1; i <= n; i++ {
		m[i][i] = 0
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			m[i][j] = 1 << 21
			//kまでで区切る
			for 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])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[1][n])
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int m[128], costs[128][128];
int calc(int s, int e){
	if(s==e) return 0;
	if(costs[s][e] == INT_MAX){
		if(e-s==1) return costs[s][e] = m[s-1] * m[s] * m[e];
		for(int i = s; i < e; i++){
			costs[s][e] = min(costs[s][e], calc(s, i) + calc(i+1, e) + m[s-1] * m[i] * m[e]);
		}
	}
	return costs[s][e];
}

int main() {
	int n;
	scanf("%d", &n);
	
	for(int i = 0; i < n; i++){
		scanf("%d %d", &m[i], &m[i+1]);
	}
	
	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= n; j++){
			costs[i][j] = INT_MAX;
		}
	}
	
	printf("%d\n", calc(1, n));
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int dp[111][111],n,L[111],R[111];
int dfs(int l,int r){
	if(dp[l][r]>-1)return dp[l][r];
	int res=1e9;
	for(int i=l;i<r;i++){
		res=min(res,dfs(l,i)+dfs(i+1,r)+L[l]*R[i]*R[r]);
	}
	return dp[l][r]=res;
}
main(){
	memset(dp,-1,sizeof(dp));
	r(i,111)dp[i][i]=0;
	cin>>n;
	r(i,n)cin>>L[i]>>R[i];
	cout<<dfs(0,n-1)<<endl;
}

C++: #include <cstdio>

struct Matrix
{
  int row;
  int column;
  int multiple_time;

  Matrix():
    row(0), column(0), multiple_time(0)
  {}

  Matrix(int r, int c, int m):
    row(r), column(c), multiple_time(m)
  {}
};

Matrix multiple(Matrix mat1, Matrix mat2)
{
  return Matrix(mat1.row,
                mat2.column,
                mat1.multiple_time + mat2.multiple_time + mat1.row * mat1.column * mat2.column);
}

Matrix matrices[100];
Matrix multiplication[100][100];

Matrix minimum_multiple_time(int i, int j)
{
  if (i == j)
    return matrices[i];

  Matrix min = multiple(multiplication[i][i], multiplication[i + 1][j]);

  for (int k = 1; k < j - i; ++k)
  {
    Matrix cur_matrix = multiple(multiplication[i][i + k], multiplication[i + k + 1][j]);
    if (min.multiple_time > cur_matrix.multiple_time)
      min = cur_matrix;
  }

  return min;
}

int main()
{
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; ++i)
    scanf("%d%d", &matrices[i].row, &matrices[i].column);

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; i + j < n; ++j)
    {
      multiplication[j][i + j] = minimum_multiple_time(j, i + j);
    }
  }

  printf("%d\n", multiplication[0][n - 1].multiple_time);

  return 0;
}

C++: #include<cstdio>
#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;
    scanf("%d",&n);
    for (int i = 0; i < n; i++) scanf("%d %d",&r[i], &c[i]);
    memset(cache, -1, sizeof(cache));
    printf("%d\n",f(0,n-1));
}

C++: #include<iostream>
using namespace std;
int dp[101][101];
class point {
public: int x, y;
};
point p[101];
int n;
int solve(int left, int right) {
	if (left == right) return dp[left][right] = 0;
	if (dp[left][right]) return dp[left][right];
	else
	{
		int min = 1<<29;
		for (int i = left; i < right; i++) {
			int k = dp[left][i] + dp[i + 1][right] + p[left].x * p[i + 1].x * p[right].y;
			if (k < min) min = k;
		}
		 dp[left][right] = min;
		 return min;
	}
}
int main(void) {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> p[i].x>>p[i].y;
	}
	for(int l=1;l<n; l++)
	for (int i = 0; i < n; i++) {
		if (i + l < n)
		{
			solve(i, i + l);
		}
	}
	cout << dp[0][n - 1]<<endl;
}

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

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

#define YES puts("YES")
#define Yes puts("Yes")
#define NO  puts("NO")
#define No  puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

//*
#define mod 1000000007 /*/
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL<<60)



void solve(){
    int N;
    cin >> N;
    vector<int> p(N+1,0);
    for(int i=0; i<N; i++)
        cin >> p[i] >> p[i+1];
    
    vector<vector<int>> dp(N+1, vector<int>(N+1,0));
    
    for(int w=2; w<=N; w++){
        for(int i=1; i<=N-w+1; i++){
            int j=i+w-1;
            dp[i][j] = INF;
            for(int 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]);
            }
        }
    }
    cout << dp[1][N] << endl;
}


signed main(){
    cout << fixed << setprecision(18);
    cerr << fixed << setprecision(18);
    solve();
}

C++: #include <cstdio>
#include <cstdlib>
#include <cassert>
#include <memory.h>
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <cassert>
typedef unsigned long long ull;
typedef unsigned long ul;
#define rep(i,n) for(unsigned long long i = 0;i<n;i++)
#define clear(c) memset(c,0,sizeof(c))
#define clearm(c) memset(c,-1,sizeof(c))
using namespace std;

int main(){
	int n;

	cin >> n;
	ull dp[10+1][10+1];
	clearm(dp);
	ull r[n],c[n];
	rep(i,n){
		int rr,cc;
		cin >> rr >> cc;
		r[i] = rr;
		c[i] = cc;
	}
	rep(i,n-1){
		dp[i][i+1] = r[i] * c[i+1] * r[i+1];
	}
	rep(i,n){
		dp[i][i] = 0;
	}
	for(int dif = 2;dif<n;dif++){
		for(int i = 0;i + dif<n;i++){
			for(int j = i + 1;j <= i +dif - 1;j++){
				dp[i][i+dif] = min(dp[i][i+dif],dp[i][j] + dp[j+1][i+dif] + r[i] * c[j] * c[i+dif]);
			}
		}
	}
	cout << dp[0][n-1] << endl;
	return 0;
}

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

#define N 100

int M[101][101], p[101], n;

int solve()
{
	int l, i, j, k;
	for (l = 2; l <= n; l++) {
		for (i = 1; i <= n - l + 1; i++) {
			j = i + l - 1;
			M[i][j] = 1 << 30;
			for (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()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	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;

	cout << solve() << endl;
	return 0;
}