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;

#define M 100

int main(){
  int n,p[M + 1],m[M + 1][M + 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 <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;
const int INF = 1e+9;

const int N = 100;
int n;
int s[N+1];       //M_i : s[i-1] x s[i] (i=1,...,n)
int dp[N+1][N+1]; //dp[i][j] = (M_i * ... * M_j)????????????????¨?????????°???????°????(1<=i<=j<=n)

int main() {
  int i, j, k, l;
  scanf("%d ", &n);
  for (i=0; i<n; i++) scanf("%d%d ", s+i, s+i+1);
  for (i=1; i<=n; i++) dp[i][i] = 0;  //1????????????????¨?????????°???0
  for (l=2; l<=n; l++)  //M_i ... M_i????????°???????????§????????????
    for (i=1; i<=n-l+1; i++) {
      j = i + l - 1; 
      dp[i][j] = INF;
      for (k=i; k<j; k++)
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + s[i-1]*s[k]*s[j]);
    }

  printf("%d\n", dp[1][n]);

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define N_MAX 105
#define INF (1 << 21)

long P[N_MAX];
long dp[N_MAX][N_MAX];

int main() {
    long n;
    cin >> n;
    for (long i = 0; i < n; i++) {
        cin >> P[i] >> P[i + 1];
    }

    for (long i = 1; i <= n; i++) {
        dp[i][i] = 0;
    }

    for (long l = 1; l <= n - 1; l++) {
        for (long i = 1; i + l <= n; i++) {
            long j = i + l;
            dp[i][j] = INF;
            for (long k = i; k <= j; k++) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + P[i - 1] * P[j] * P[k]);
            }
        }
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
struct RC {
    int r, c;
};

int dp[101][101];
int main()
{
    int N;
    cin >> N;
    vector<RC> rc(N);
    rep(i, N) {
        int r, c;
        cin >> r >> c;
        rc[i] = {r, c};
    }

    vector<vector<int>> dp(N, vector<int>(N, 1e9));
    rep(i, N) dp[i][i] = 0;

    repd(w, 1, N) {
        rep(i, N - w) {
            int j = i + w;
            repd(k, i, j) {
                chmin(dp[i][j], dp[i][k] + dp[k + 1][j] + rc[i].r * rc[k].c * rc[j].c);
            }
        }
    }

    cout << dp[0][N - 1] << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define N 101

vector<vector<int> > dp(N, vector<int>(N));
int p[N+1];

// dp[i][j] := [Mi, Mj]におけるminimum number of scalar multiplications 

int main(){
    
    int n;
    cin >> n;
    
    for (int i = 1; i < n+1; i++) cin >> p[i-1] >> p[i];
    
    
    // l はchain lengthを表す変数である.
    // l == 1のとき計算回数は0である.
    for (int l = 2; l <= n; l++){
        
        // 変数iは区間の左端を表す
        // 左端が動ける範囲の上限は, n - l + 1である
        // 具体例を考えてみよ
        for (int i = 1; i <= n-l+1; i++){
            
            // jは右端の上限値を表す
            int j = i + l - 1;
            
            dp[i][j] = (1<<20);
            
            // 今回調べる区間は, [i, j-1]
            for (int k = i; k < j; k++){
                
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j]);
            }
        }
    }
    
    cout << dp[1][n] << endl;
}

C++: #include <stdio.h>

int N;
int R[200];
int dp[200][200];

int min(int x, int y) { return x < y ? x : y; }

int main(void) {
	scanf("%d", &N);
	for (int i = 0; i < N; i ++) {
		scanf("%d %d", &R[i], &R[i+1]);
	}
	for (int i = 0; i < N; i ++) {
		for (int j = 0; j < N; j ++) {
			dp[i][j] = 1 << 20;
		}
	}
	for (int i = 0; i < N; i ++) {
		dp[i][0] = 0;
	}
	for (int j = 0; j < N; j ++) {
		for (int i = 0; i + j < N; i ++) {
			// M_j ... M_{j+i}のi+1個の積について考える
			for (int k = 0; k < j; k ++) {
				// (M_j ... M_{j+k}) (M_{j+k+1} ... M_{j+i})
				//   (k+1個の積)            (i-k個の積)
				dp[i][j] = min(dp[i][j], dp[i][k] + dp[i+k+1][j-k-1] + R[i] * R[i+k+1] * R[i+j+1]);
			}
		}
	}
	printf("%d\n", dp[0][N-1]);
	return 0;
}

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

using namespace std;

int dp[100][100];
int p[101];
int n;

void solve() {
	for (int m = n; m > 0; m--) {
		for (int t = 0, i = 0, j = n - m + 1; t < n && j < n; t++) {
			dp[i][j] = INT_MAX;
			for (int k = i; k < j; k++) {
				int temp = dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1];
				if (temp < dp[i][j]) {
					dp[i][j] = temp;
				}
			}
			i++;
			j++;
		}
	}
}

int main() {
	int a, b;
	cin >> n >> a;

	p[0] = a;
	for (int i = 1; i < n; i++) {
		cin >> a >> b;
		p[i] = a;
	}
	cin >> a;
	p[n] = a;

	solve();
	cout << dp[0][n - 1] << endl;

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
static const int MAX = 101;
int M[MAX][MAX], P[MAX];

int main()
{
    int n, r, c;
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> r >> c;
        P[i-1] = r;
        P[i] = c;
    }
    for (int i = 0; i <= MAX; ++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 << 20);
            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;
}