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, 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<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 <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 1; 
    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<cstdio>
#include<algorithm>
#define Lens 105

using namespace std;
int p[Lens];
int C[Lens][Lens];
int N;

int main()
{
	cin >> N;
	for (int i = 1; i <= N; i++)
	{
		cin >> p[i - 1] >> p[i];
		C[i][i] = 0;
	}
	for (int k = 1; k < N; k++)
	{
		for (int i = 1; i <= N-k; i++)
		{
			int j = i + k;
			C[i][j] = C[i][i] + C[i + 1][j]+p[i-1]*p[i]*p[j];
			for (int t = i+1; t < j; t++)
			{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
				C[i][j] = min(C[i][j], C[i][t] + C[t+1][j] + p[i - 1] * p[t] * p[j]);
			}
		}
	}
	cout << C[1][N];
	return 0;

}

C++: #include<stdio.h>

int main(void) {
  int n, p[101], m[101][101], i, l, j, k, non, temp;
  
  scanf("%d", &n);
  for (i = 0; i < n; i++)
  {
    if (i == 0) scanf("%d %d", &p[0], &p[1]);
    else scanf("%d %d", &non, &p[i + 1]);
  }
  
  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] = 10000000000;
      for (k = i; k <= j - 1; k++)
      {
        if (m[i][j] > m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j])
        {
          m[i][j] = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        }
      }
    }
  }
  
  printf("%d\n", m[i][j]);
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <limits.h>
using namespace std;

int m[101][101]; // m[i][j]: from i to j (columns)
int p[101]; // 
int main(){
	int n; scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		int r, c; scanf("%d %d", &r, &c);
		p[i] = r; p[i + 1] = c;
	}
	// initialize m[][]
	for (int i = 0; i <= 100; ++i){
		for (int j = 0; j <= 100; ++j) m[i][j] = INT_MAX;
	}
	for (int i = 0; i <= 100; ++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;
			for (int k = i; k < j; ++k){
				if (m[i][k] == INT_MAX || m[k + 1][j] == INT_MAX) continue;
				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] << '\n';
}

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

int p[101];
int M[101][101];

int main() {
	int n;
	cin >> n;
	for (int i = 1; i < n + 1; i++) {
		cin >> p[i - 1] >> p[i];
	}
	for (int i = 1; i < n + 1; i++)M[i][i] = 0;
	for (int i = 2; i < n + 1; i++) {
		for (int j = 1; j + i - 1 < n + 1; j++) {
			int k = j + i - 1;
			M[j][k] = (1 << 21);
			for (int t = j; t < k; t++) {
				M[j][k] = min(
					M[j][t] + M[t + 1][k] + p[j - 1] * p[t] * p[k],
					M[j][k]
				);
			}
		}
	}
	cout << M[1][n] << endl;
}