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 (r Range) length() int {
	return r.end - r.start + 1
}

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[r.start].r, matrices[r.end].c}, v
	}
	if r.length() == 0 {
		panic("must not be empty")
	} else if r.length() == 1 {
		return matrices[r.start], 0
	}

	var minCount int = math.MaxInt32

	for i := 0; i < r.length()-1; i++ {
		formerRange := Range{r.start, r.start + i}
		latterRange := Range{r.start + i + 1, r.end}
		matF, countF := calcMinMatrixChainMultiplication(matrices, formerRange, memo)
		matL, countL := calcMinMatrixChainMultiplication(matrices, latterRange, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	memo[r] = minCount
	return Matrix{matrices[r.start].r, matrices[r.end].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<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++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
typedef long long ll;
const int INF = 1<<21;

//m[i][j]はM_{i}M_{i+1}...M_{j}を計算するための最小の掛け算の回数とする。
int m[110][110];

int main(){
	int n;
	cin >> n;
	vector<int> p(n+1);
	rep(i,n){
		int a,b;
		cin >> a >> b;
		p[i] = a;
		if(i==n-1){
			p[i+1] = b;
		}
		p[i] = a;
	}
	
	rep(i,n){
		rep(j,n){
			if(i==j) continue;
			m[i][j] = INF;
		}
	}
	
	//lは対象となる行列の数
	for (int l = 2; l <= n ; ++l) {
		for (int i = 0; i <= n - l ; ++i) {
			int j = i + l - 1;
			for (int k = i; k <= j - 1; ++k) {
				m[i][j] = min(m[i][j], m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1]);
			}
		}
	}
	
	cout << m[0][n-1] << endl;
    return 0;
}

C++: #include<cstdio>
using namespace std;
const char N_MAX = 105;
int INF = 1<<30;
int dp[N_MAX][N_MAX];
char matrix[N_MAX];
int n;
int solve() {
    for(char offset = 1; offset < n; offset++) {
        for(char c = 1, r = c + offset; r <= n; c++, r++) {
            dp[c][r] = INF;
            for(char divid = 1; divid <= offset; divid ++) {
                int newValue = dp[c][c - 1 + divid] + dp[c + divid][r] + matrix[c - 1] * matrix[r] * matrix[c + divid - 1];
                if(newValue < dp[c][r])
                    dp[c][r] = newValue;
            }
        }
    }
    return dp[1][n];
}
int main(){
    scanf("%d", &n);
    for(char i=0;i<n;i++) scanf("%d %d", &matrix[i], &matrix[i+1]);
    printf("%d\n", solve());
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)


int main() {
  int n;
  cin >> n;
  vector<int> RC(n+1);
  for(int i = 0;i < n;i++){
      cin >> RC.at(i) >> RC.at(i+1);
  }

  vector<vector<int>> cost(n+1,vector<int>(n+1));
  int min_cost;
  for (int l = 2;l <= n;l++){
      for (int i = 1;i <= n-l+1;i++){
          int j = i+l-1;
          for (int k = i;k < j;k++){
              if (k == i){
                  min_cost = cost.at(i).at(k)+cost.at(k+1).at(j)+RC.at(i-1)*RC.at(k)*RC.at(j);
              }else{
                  min_cost = min(min_cost,cost.at(i).at(k)+cost.at(k+1).at(j)+RC.at(i-1)*RC.at(k)*RC.at(j));
              }
          }
          cost.at(i).at(j) = min_cost;
      }
  }

  cout << cost.at(1).at(n) << endl;
  
}

C++: #include<iostream>
#include<map>
#include<deque>
#include<algorithm>
#include<string>
#include<cctype>
using namespace std;
typedef long long ll;
#define REP(i,b,e) for(ll i=(ll)b;i<(ll)e;i++)
#define Rep(i,n) REP(i,0,n)
ll longinf=1ll<<60;

ll n,p[101],m[101][101];
int main(){
    cin>>n;
    Rep(i,n){
        cin>>p[i]>>p[i+1];
    }
    REP(i,1,n+1) m[i][i]=0;
    REP(l,2,n+1){
        REP(i,1,n-l+2){
            int j=i+l-1;
            m[i][j]=longinf;
            REP(k,i,j+1){
                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 <algorithm>
#include <cmath>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cstring>
#include <tuple>
#include <bitset>
#include <unordered_map>
#include <climits>
using namespace std;
typedef long long ll;

int dp[101][101];

void init() {
    for (int i=0; i<101; ++i) {
        for (int j=0; j<101; ++j) {
            if (i == j) {
                dp[i][j] = 0;
                continue;
            }
            dp[i][j] = INT_MAX;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<pair<int,int>> mat(n);
    for (int i=0; i<n; ++i) {
        cin >> mat[i].first >> mat[i].second;
    }

    init();

    for (int l=2; l<=n; ++l) {
        for (int i=0; i<n; ++i) {
            int j = i+l-1;
            if (j > n) continue;
            for (int k=i+1; k<=j; ++k) {
                dp[i][j] = min(dp[i][j], dp[i][k-1] + dp[k][j]
                               + mat[i].first * mat[j].second * mat[k].first);
            }
        }
    }
    cout << dp[0][n-1] << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
int dp[111][111];
signed main(){
  int n;
  cin>>n;
  int r[n],c[n];
  for(int i=0;i<n;i++) cin>>r[i]>>c[i];
  int INF=1LL<<55LL;
  for(int i=0;i<111;i++) fill_n(dp[i],111,INF);
  for(int i=0;i<n;i++) dp[i][i+1]=0;
  for(int i=2;i<=n;i++)
    for(int j=0;j<=n-i;j++)
      for(int k=1;k<i;k++)
	dp[j][j+i]=min(dp[j][j+i],dp[j][j+k]+dp[j+k][j+i]+(r[j]*r[j+k]*c[j+i-1]));
  cout<<dp[0][n]<<endl;
  return 0;
}