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

int main(){
	int n;
	cin >> n;
	int a[100][2];
	for ( int i = 0; i < n; i++ ) {
		cin >> a[i][0];
		cin >> a[i][1];
	}
	int c[100][100] = {0};
	for ( int i = 2; i <= n; i++ ) {
		for ( int j = 0; j <= n - i; j++ ) {
			int x = 0;
			for ( int k = 1; k < i; k++ ) {
				int y = 0;
				y = c[j][k] + c[j+k][i-k] + a[j][0] * a[j+k][0] * a[j+i-1][1];
				if ( x == 0 || x > y ) x = y;
			}
			c[j][i] = x;
		}
	}
	cout << c[0][n] << endl;
	return 0;
}

C++: /*
 * ALDS1_10_B.cpp
 *
 *  Created on: May 5, 2018
 *      Author: 13743
 */

#include<iostream>
#include<algorithm>

using namespace std;

struct Mat {
	int row;
	int col;
};

long mcm(Mat* M, int n) {
	long dp[101][101];
	for(int i=2; i<=n; i++) {
		for(int j=0; j<=n-i; j++) {
			dp[i][j] = 99999999999L;
			for(int k=1; k<i; k++) {
				dp[i][j] = min(dp[k][j]+dp[i-k][j+k]+M[j].row*M[j+k].row*M[j+i-1].col, dp[i][j]);
			}
		}
	}
	return dp[n][0];
}

long dpR[101][101];

long mcmRec(Mat* M, int l, int r) {
	if(r - l < 2) return 0;
	if(dpR[l][r] == 0) {
		dpR[l][r] = 9999999999L;
		for(int i=1; i<r-l; i++) {
			dpR[l][r] = min(mcmRec(M,l,l+i)+mcmRec(M,l+i,r)+M[l].row*M[l+i].row*M[r-1].col, dpR[l][r]);
		}
	}
	return dpR[l][r];
}

int main() {
	int n;
	Mat M[100];

	cin >> n;
	for(int i=0; i<n; i++) {
		cin >> M[i].row >> M[i].col;
	}

//	long ans = mcm(M, n);
	long ans = mcmRec(M, 0, n);
	cout << ans << endl;

}

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

#define min(a,b) ((a)<(b)?(a):(b))
#define N 100

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

  for(i=1;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]=INT_MAX;
      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]);
      }
    }
  }
  printf("%d\n",m[1][n]);

  return 0;
}

C++: #include <bits/stdc++.h>
#define N 101
using namespace std;
int A[100];

int dp[N][N];

int Min(int &a,int b){return a = min(a,b);}

int main(){
  int n;
  cin>>n;

  for(int i=0,cnt=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    if(i == 0) A[cnt++] = a;
    A[cnt++] = b;
  }

  for(int i=0;i<n+1;i++){
    for(int j=0;j<n+1;j++)dp[i][j] = 1e9;
    dp[i][i] = 0;
  }
  
  for(int len=0;len<n;len++)
    for(int l=1;l<n;l++){
      int r = l+len;
      for(int m = l;m<r;m++){
        Min(dp[l][r],dp[l][m] + dp[m+1][r] + A[l-1]*A[m]*A[r]);
      }
}
  
  cout<<dp[1][n]<<endl; 
  return 0;
}

C++: #include <cstdio>
#include <algorithm>

int main(void){
	int i,j,k,n,p[101], m[101][101];
	scanf("%d", &n);
	for(i=0;i<n;i++){
		scanf("%d %d", &p[i], &p[i+1]);
	}
	
	for(i=n-2;i>=0;i--){
		m[i+1][i+1] = 0;
		for(j=i+1;j<n;j++){
			m[i][j] = p[i]*p[i+1]*p[j+1] + m[i+1][j];
			for(k=i+1;k<j;k++){
				m[i][j] = std::min(m[i][j], m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1]);
			}
		}
	}

	printf("%d\n", m[0][n-1]);

	return 0;
}