Go: package main

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

type Matrix struct {
	r int // num of row
	c int // num of column
}

type Range struct {
	start int // index, inclusive
	end   int // index, inclusive
}

func calcMultiplication(x, y Matrix) (Matrix, int) {
	if x.c != y.r {
		panic("cannnot calculate")
	}
	return Matrix{x.r, y.c}, x.r * y.c * x.c
}

func calcMinMatrixChainMultiplication(matrices []Matrix, r Range, memo map[Range]int) (Matrix, int) {
	if v, ok := memo[r]; ok {
		return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, v
	}
	if len(matrices) == 0 {
		panic("must not be empty")
	} else if len(matrices) == 1 {
		return matrices[0], 0
	} else if len(matrices) == 2 {
		return calcMultiplication(matrices[0], matrices[1])
	}

	var minCount int = math.MaxInt32

	for i := 0; i < len(matrices)-1; i++ {
		formerMatrices := matrices[:i+1]
		latterMatrices := matrices[i+1:]
		matF, countF := calcMinMatrixChainMultiplication(formerMatrices, Range{0, i}, memo)
		matL, countL := calcMinMatrixChainMultiplication(latterMatrices, Range{i + 1, len(matrices) - 1}, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, minCount
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, _ := nextInt(sc)
	matrices := make([]Matrix, n)
	for i := 0; i < n; i++ {
		r, _ := nextInt(sc)
		c, _ := nextInt(sc)
		matrices[i] = Matrix{r, c}
	}
	memo := make(map[Range]int)
	_, resultCount := calcMinMatrixChainMultiplication(matrices, Range{0, len(matrices) - 1}, memo)
	fmt.Println(resultCount)
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <stdlib.h>
#include <stdio.h>

int MCM(int n){
int i,j,k,l,m[101][101],p[101],x;

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]=10000000;
        for(k=i;k<=j;k++){
            x=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
            if(m[i][j]>x)m[i][j]=x;
            }
        }
    }
return m[i][j];
}

int main(){

int i,n,m[100];

scanf("%d",&n);
for(i=0;i<n;i++){
    scanf("%d %d",&m[i],&m[i+1]);
   }
printf("%d\n",MCM(n));
return 0;
}

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


int n;
int p[102], m[102][102]={0};
int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p[i-1] >> p[i];
	}
	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;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define lper(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define at(x,i) get<i>(x);
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n;cin >> n;
    vl p(n+1);
    rep(i,n){
        ll hoge,hogehoge;
        if(i%2==0)cin >> p[i] >> p[i+1];
        else {
            cin >> hoge >>hogehoge;
            if(i==n-1)p[n]=hogehoge;
        }
    }
    vvl dp(n,vl(n,inf));
    rep(i,n)dp[i][i]=0;
    rep(j,n){
        lper(i,j+1,0){
            rep(k,j-i){
                chmin(dp[i][j],dp[i][i+k]+dp[i+k+1][j]+p[i]*p[i+k+1]*p[j+1]);
            }
        }
    }
    cout << dp[0][n-1] <<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[105][105];
int q[105];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>q[i-1]>>q[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(i==j)	dp[i][j]=0;
			else dp[i][j]=0x3f3f3f3f;
	for(int l=1;l<=n-1;l++){
		for(int i=1;i<=n-l ;i++){
			int j=i+l;
			for(int k=i;k<=j-1;k++){
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+q[i-1]*q[k]*q[j]);
			}
		}
	}
	cout<<dp[1][n]<<endl;
}

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

static const int N = 100;

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

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#define MAX 100
#define INFTY (1 << 21)
using namespace std;

int p[MAX + 1], mat[MAX + 1][MAX + 1];

void matrixChainMultiplication(int n) {
	for (int i = 1; i <= n; i++) {
		mat[i][i] = 0;
	}
	for (int l = 2; l <= n; l++) {
		for (int i = 1; i <= n - l + 1; i++) {
			int j = i + l - 1;
			mat[i][j] = INFTY;
			for (int k = i; k <= j - 1; k++) {
				mat[i][j] = min(mat[i][j], (mat[i][k] + mat[k + 1][j] + (p[i - 1] * p[k] * p[j])));
			}
		}
	}
}

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

C++: #ifdef DEBUG
#include "../utils.cpp"
#endif
#include <bits/stdc++.h>
/*
Problem


Category
DP
 
URL:


考察:



Date: 2020/06/01
*/
 

using namespace std;
using ll = int64_t;
#define int int64_t
#define INF 10e9

 
/* ==== declarations ====== */
// functions
void solve();
void getInput();
 
// global variables
int N;
vector<int> L, R;
vector<vector<int>> dp;

/* ======================== */


void getInput() {
    cin>>N;
    L = vector<int>(N, 0);
    R = vector<int>(N, 0);
    dp = vector<vector<int>>(N, vector<int>(N, 1e9));
    
    for(int i=0; i<N; ++i) {
        cin>>L[i]>>R[i];
    }
}
 

void solve() {
    
    for(int i=0; i<N; ++i) {
        dp[i][i] = 0;
    }
    for(int i=0; i+1<N; ++i) {
        dp[i][i+1] = L[i]*R[i]*R[i+1];
    }

    for(int i=2; i<N; ++i) {
        for(int j=0; i+j < N; ++j) {
            for(int k = 0; k<i; ++k) {
                int p = L[j];
                int q = R[j+k];
                int r = R[j+i];
                dp[j][j+i] = min(dp[j][j+i], p*q*r + dp[j][j+k] + dp[j+k+1][j+i]);

            }
        }
    }
    cout<<dp[0][N-1]<<endl;
}
 
 
signed main(){
    
    getInput();
    solve();
    
    return 0;
}

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

static const int N = 100;

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

  return 0;
}