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<iostream>
#include<algorithm>
using namespace std;

static const int N = 100;

int main() {
	int n;
	int 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 = 0; 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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
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;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
int n;
vector<ll> r, c;
auto memo = make_vec<ll>(110, 110);
ll dp(int i, int j) {
    if (memo[i][j] != -1) return memo[i][j];
    if (i == j) return memo[i][j] = 0;
    ll ret = LINF;
    for (int k = i; k < j; k++) {
        chmin(ret, dp(i, k) + dp(k + 1, j) + r[i] * c[k] * c[j]);
    }
    return memo[i][j] = ret;
}
int main() {
    cin >> n;
    r.resize(n);
    c.resize(n);
    rep(i, n) cin >> r[i] >> c[i];
    rep(i, 110) rep(j, 110) memo[i][j] = -1;
    cout << dp(0, n - 1) << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define chmax(a,b) a=max(a,b)
#define chmin(a,b) a=min(a,b)
#define INF 100100100100100

int main(){

    long long n; cin >> n;
    vector<long long> r(n);
    vector<long long> c(n);
    for(long long i = 0; i < n; i++){
        cin >> r[i] >> c[i];
    }

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

    cout << dp[0][n] << endl;
    
    return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <climits>

/*
 * http://ttrsq.exblog.jp/24318687/
 */

const int MAX_N = 100;
int DP[MAX_N + 1][MAX_N + 1] = {0};
int *p;

int best(int r, int c) {
  int min = INT_MAX;
  int num = c - r;

  for (int i = 0; i < num; i++) {
    min = std::min(min, DP[r][r + i] + DP[r + i + 1][c] + p[r - 1] * p[r + i] * p[c]);
  }
  
  return min;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int n = 0;
  std::cin >> n;

  if (n == 1) {
    std::cout << "1" << std::endl;
  }
  
  p = (int *) std::malloc(sizeof(int) * n + 1);

  int bb = 0;

  std::cin >> p[0];
  std::cin >> p[1];
  
  for (int i = 2; i <= n; i++) {
    std::cin >> bb;
    std::cin >> p[i];
  }

  for (int i = 1; i < n; i++) {
    DP[i][i + 1] = p[i - 1] * p[i] * p[i + 1];
  }

  for (int j = 0; j <= n - 3; j++) {
    for (int i = j + 3; i <= n; i++) {
      DP[i - 2 - j][i] = best(i - 2 - j, i);
    }
  }

  std::cout << DP[1][n] << std::endl;

  return 0;
}

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

static const in N = 100;

int main(){
  int j,i,l,n,p[N+1],m[N+1][N+1];
  scanf("%d",&n);

  for(i = 0;i<=n;i++){
    scanf("%d %d",&p[i-1],&p[i]);
  }

  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);
      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 int long long
int n,dp[111][111],p[111];

int rec(int i,int j){
  int Min=2*1e9;
  for(int k=0;k<i-1;k++){
    Min=min(Min,dp[j+1][j+k+1]+dp[j+k+2][j+i]+p[j]*p[j+k+1]*p[j+i]);
  }
  return Min;
}

signed main(){
  cin>>n;
  cin>>p[0]>>p[1];
  for(int i=2;i<=n;i++){
    cin>>p[i]>>p[i];
  }
  for(int i=0;i<n;i++){
    dp[i][i]=0;
    if(i)dp[i][i+1]=p[i-1]*p[i]*p[i+1];
  }

  for(int i=3;i<=n;i++){
    for(int j=0;j+i<=n;j++){
      dp[j+1][j+i]=rec(i,j);
    }
  }
  cout<<dp[1][n]<<endl;
  return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;

#define N_MAX 100
#define C_MAX 100
#define R_MAX 100

int p[N_MAX+1];
int m[R_MAX+1][C_MAX+1];


int chain_multiplication(int n)
{
    for (int i = 0; i < n; i++) {
        m[i][i] = 0;
    }
    for (int l = 1; l < n; l++) {
        for (int i = 0; i < n - l; i++) {
            int j = i + l;
            m[i][j] = 100000000;
            for ( int k = i ; k < j; k++) {
                int a = m[i][k] + m[k+1][j] + p[i] * p[k+1] * p[j+1];
//                printf("i %d k %d j %d a %d min %d\n", i, k, j, a, m[i][j]);
                if (m[i][j] > a) {
                    m[i][j] = a;
                }
            }
//            printf("m[%d][%d] = %d\n", i, j, m[i][j]);
        }
    }
    return m[0][n-1];
}

int main() {
    int n;

    cin >> n;

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