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 <string>
#include <algorithm>
#include <vector>

using namespace std;
static const int N = 100;

#define INT_MIN     (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX       2147483647    /* maximum (signed) int value */

void solve(vector<int> &data)
{
	vector<vector<int> > dp(N + 1, vector<int>(N + 1, 0));

	int nums = data.size() - 1;
	for (int len = 2; len <= nums; ++len)
	{
		for (int l = 1; l <= nums - len + 1; ++l)
		{
			int r = l + len - 1;
			dp[l][r] = INT_MAX;
			for (int m = l; m <= r - 1; ++m)
			{
				dp[l][r] = min(dp[l][r], dp[l][m] + dp[m+1][r] + data[l - 1] * data[m] * data[r]);
			}
		}
	}

	cout << dp[1][nums] << endl;
}

int main()
{
	int num; cin >> num;
	vector<int> data(num + 1);
	for (int i = 1; i <= num; ++i)
		cin >> data[i-1] >> data[i];

	solve(data);
}

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];

	scanf("%d", &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]);
				}
			}
		}

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

		return 0;
	}

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

int main() {
  int n;
  cin >> n;
  vector<int> p(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> p[i - 1] >> p[i];

  vector< vector<int> > dp(n + 1, vector<int>(n + 1));
  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n - l + 1; ++i) {
      int j = i + l - 1;
      dp[i][j] = 1000000000;
      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 <iostream>
#include <string>
#define MAX 100001
#define SENTINEL 2000000000
using namespace std;

int m[101][101];
int p[101];

void mcm()
{
}

int main()
{
    int n;
    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] = MAX;
            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 <algorithm>
#include <climits>
#define INF INT_MAX

using namespace std;

int main(){
    int n;
    cin >> n;
    int m[n+1][n+1], p[n+1];
    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] = INF;
            for(int k = i; k < j; k++){
                int q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
                m[i][j] = min(m[i][j], q);
            }
        }
    }

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

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

const int INF = 1001001001;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    vi a(n+1);
    rep(i, n) {
        int r, c;
        cin >> r >> c;
        if (i == 0) {
            a[i] = r;
            a[i+1] = c;
        } else {
            a[i+1] = c;
        }
    }

    vvi dp(n+1, vi(n+1, INF));
    for (int i = 1; i <= n; ++i) dp[i][i] = 0;
    // kが間隔
    for (int k = 1; k <= n - 1; ++k) {
        // iが始点
        for (int i = 1; i + k <= n; ++i) {
            // jが間の点
            for (int j = i; j <= i + k - 1; ++j) {
                chmin(dp[i][i+k], dp[i][j] + dp[j+1][i+k] + a[i-1]*a[j]*a[i+k]);
            }
        }
    }

    // for (int i = 1; i <= n; ++i) {
    //     for (int j = i+1; j <= n; ++j) {
    //         for (int k = i; k <= j-1; ++k) {
    //             chmin(dp[i][j], dp[i][k] + dp[k+1][j] + a[i-1] * a[k] * a[j]);
    //         }
    //     }
    // }
    // だと更新順がダメ
    
    cout << dp[1][n] << endl;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<string>
//#define MAX 101
#define INFTY (1<<21)

using namespace std;

static const int N = 100;


/*int min(int a, int r[], int c[]) {



	for(int i=1; i<=a; i++){
		if(r[i] == c[i+1]){
			int s = r[i]*c[i]*r[i+1];
		}
}*/

int main() {
	int n;
	

	int M[N+1][N+1];
	int p[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 j = 2; j <= n; j++) {
		for (int i = 1; i <= n - j + 1; i++) {
			int k = i + j - 1;

			M[i][k] = INFTY;

			for (int a = i; a <= k - 1; a++) {
				int b = M[i][a] + M[a + 1][k] + p[i - 1] * p[a] * p[k];
				M[i][k] = min(M[i][k], b);

			}
		}
	}

	cout << M[1][n] << endl;
	/*int R[MAX];
	int C[MAX];

	int r, c;
	for(int i = 1; i<=n; i++){
		cin >> r >> c;
		R[i] = r;
		C[i] = c;

	}*/

	return 0;
}