Go: package main

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

var cache [100][100]int

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	tables := []table{}
	for i := 0; i < num; i++ {
		sc.Scan()
		num1, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		num2, _ := strconv.Atoi(sc.Text())
		t := table{num1, num2}
		tables = append(tables, t)
	}

	fmt.Println(minimum(0, len(tables)-1, tables))
}

type table struct {
	v int
	h int
}

func minimum(i int, j int, t []table) int {
	if i == j {
		return 0
	}

	// fmt.Printf("i:%v j:%v\n", i, j)
	if cache[i][j] != 0 {
		return cache[i][j]
	}

	min := math.MaxInt64
	for k := i; k < j; k++ {
		p := t[i].v
		n := t[k].h
		q := t[j].h
		val := minimum(i, k, t) + minimum(k+1, j, t) + p*n*q
		if val < min {
			min = val
		}
	}

	cache[i][j] = min
	return min
}

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>

long long int INF = 1e18;
using namespace std;

typedef pair<long long int, long long int> P;

int main(){
	
	int n;
	cin >> n;
	
	int A[200], B[200];
	
	for(int i = 0; i < n; i++){
		cin >> A[i] >> B[i];
	}
	
	long long int DP[101][101] = {};
	for(int i = 1; i < n; i++){
		for(int j = 0; j + i < n; j++){
			DP[j][j + i] = INF;
			for(int k = 0; k < i; k++){
				DP[j][j + i] = min(DP[j][j + i], DP[j][j + k] + DP[j + k + 1][j + i] + A[j] * B[j + k] * B[j + i]);
				//cout << DP[j][j + k] + DP[j + k + 1][j + i] + A[j + k] * B[j + k] * B[j + k + 1] << " ";
			}
			//cout << DP[j][j + i] << " ";
		}
		//cout << endl;
	}
	
	cout << DP[0][n - 1] << endl;
	
	return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<algorithm>

using namespace std;

void output( int, int, int, int);

#define MAX 100
#define INFTY (1 << 21)

int SIZE;
int R[MAX + 2];
int C[MAX+1][MAX+1]; // cost table
int B[MAX+1][MAX+1]; // best table

void compute(){
    for ( int i = 1; i <= SIZE; i++ )
    for ( int j = 1; j <= SIZE; j++ ) C[i][j] = INFTY;
    
    for ( int i = 1; i <= SIZE; i++ ) C[i][i] = 0;
    
    for ( int j = 1; j <= SIZE - 1; j++ ){
        for ( int i = 1; i <= SIZE - j; i++ ){
            for ( int k = i + 1; k <= i + j; k++ ){
                // C[i][i+j] = min( C[i][i+j], 
                //                  C[i][k-1] + C[k][i+j] + R[i]*R[k]*R[i+j+1]);
                int cost = C[i][k-1] + C[k][i+j] + R[i]*R[k]*R[i+j+1];
                if ( cost < C[i][i+j] ){
                    C[i][i+j] =  cost;
                    B[i][i+j] = k;
                }
                
            }
        }
    }
}

void order( int i, int j ){
    if ( i == j ) cout << "M" << i;
    else{
        cout << "(";
        order( i, B[i][j]-1 ); order(B[i][j], j);
        cout << ")";
    }
}

void input(){
    cin >> SIZE;
    for ( int i = 1; i <= SIZE + 1; i++ ) cin >> R[i];
}

void output(){
    cout << "minimum cost = " << C[1][SIZE] << endl;
    order(1, SIZE);
    cout << endl;
}

main(){
    input();
    compute();
    output();
}

C++: /**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int r[45] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);


int f(int n, int k, int m)
{
	return f(n,k) + f(k+1,m) + (r[n] * r[k+1] * r[m+1]);
}

int f(int n, int m)
{
	if (n==m) return 0;
	int & ra = a[n][m];
	if (ra) return ra;
	ra = INT_MAX;
	for (int k=n;k<m;k++)
	{
		int ans = f(n,k,m);
		if (ans < ra) ra = ans;
	}
	return ra;
}

int main()
{
	int rr = 0;
	int cc = 0;
	int n = 0;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cin >> rr >> cc;
		r[i]=rr;
		r[i+1]=cc;
	}
	for (int i=0;i<n-1;i++)
	{
		a[i][i+1] = r[i] * r[i+1] * r[i+2];
	}
	int ans = f(0,n-1);
	cout << ans << endl;
}

C++: #include<iostream>
#include<climits>
#include<algorithm>
using namespace std;
struct mat{
  int w,h;
};
vector<mat> v;
int memo[101][101];

int solve(int l,int r){
  if(memo[l][r] > 0)return memo[l][r];
  //cout << l << ' ' << r << endl;
  if(l == r)return 0;
  int ret = INT_MAX;
  for(int i = l; i < r; i++){
    int ans = solve(l,i) + solve(i+1,r) + v[l].h * v[i].w * v[r].w;
    ret = min(ret,ans);
  }
  return memo[l][r] = ret;
}
int main(){
  fill(*memo,*(memo+101),-1);
  int n;
  cin >> n;
  for(int i = 0;i < n;i++){
    int r,c;
    cin >> r >> c;
    v.push_back({c,r});
  }
  cout << solve(0,n-1) << endl;
}

C++: #include <bits/stdc++.h>
       
using namespace std;
typedef long long ll;
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 (b<a) { a=b; return 1; } return 0; }

struct Size {
  int r, c;
};

int main() {
  int n;
  cin >> n;
  vector<Size> M(n);
  for (int i = 0; i < (int)(n); ++i) cin >> M[i].r >> M[i].c;

  const int INF = 1e9;
  vector<vector<int> > dp(n, vector<int>(n, INF));
  for (int i = 0; i < (int)(n); ++i) dp[i][i] = 0;

  for (int l = 2; l <= n; ++l) {
    for (int i = 0; i < (int)(n); ++i) {
      int j = i + l - 1;
      if (j >= n) continue;
      for (int k = i; k + 1 <= j; ++k) {
        chmin(dp[i][j], dp[i][k] + dp[k + 1][j] + M[i].r * M[k].c * M[j].c);
      }
    }
  }

  cout << dp[0][n - 1] << endl;

  return 0;
}

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

void fnInput(vector<int>& rvnMtrx)
{
  int nMaxSiz;
  cin >> nMaxSiz;
  rvnMtrx.resize(nMaxSiz + 1);

  for (int i = 0; i < nMaxSiz; ++i)
    cin >> rvnMtrx[i] >> rvnMtrx[i + 1];
}

int fnMtrxChainMultpl(vector<vector<int>>& rvvnDetDP, const vector<int>& cnrvnMtrx)
{
  const int nMAXSIZ = cnrvnMtrx.size() - 1;
  for (int i = 1; i <= nMAXSIZ; ++i)  rvvnDetDP[i][i] = 0;

  for (int nDif = 1; nDif <= nMAXSIZ - 1; ++nDif)
    for (int nFrt = 1; nFrt <= nMAXSIZ - nDif; ++nFrt)
    {
      int nBak = nFrt + nDif;
      for (int nx = nFrt; nx <= nBak - 1; ++nx)
        rvvnDetDP[nFrt][nBak] =
          min(rvvnDetDP[nFrt][nBak],
              rvvnDetDP[nFrt][nx] + rvvnDetDP[nx + 1][nBak] 
                                  + cnrvnMtrx[nFrt - 1] * cnrvnMtrx[nx] * cnrvnMtrx[nBak]);
    }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnDetDP; 
  vector<int> vnMtrx;

  fnInput(vnMtrx);
  vvnDetDP.resize(vnMtrx.size(), vector<int>(vnMtrx.size(), INT_MAX));

  fnMtrxChainMultpl(vvnDetDP, vnMtrx);
  cout << vvnDetDP[1][vnMtrx.size() - 1] << endl;
  return 0;
}