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
	}

	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
		}
	}
	memo[r] = minCount
	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];  //  M[i]がp[i-1]*p[i]の行列となるような1次元配列
    }

    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 <iostream>
using namespace std;

#define N 100
#define MAX 100000

int main(void) {
    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; //対角成分は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;
}

C++: #line 1 "../lib/template.hpp"


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>

using namespace std;

#line 2 "tmp.cpp"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vi a;
  REP(i, n) {
    int r, c;
    cin >> r >> c;
    if (i == 0)
      a.push_back(r);
    a.push_back(c);
  }

  vector<vi> dp(n + 1, vi(n + 1, 1e9));
  REP(k, n + 1) {
    REP(i, n + 1 - k) {
      if (k < 2) {
        dp[i][i + k] = 0;
      } else {
        REP(j, k) { dp[i][i + k] = min(dp[i][i + k], dp[i][i + j] + dp[i + j][i + k] + a[i] * a[i + j] * a[i + k]); }
      }
    }
  }

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

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <list>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int main() {

	int n, p[101], m[101][101];

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

	return 0;
}

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 ; k++){
	if(i == j) m[i][j] = 0;
	else m[i][j] = min(m[i][j], m[i][k] + m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

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

  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    int N;
    cin >> N;
    vector<P> dim(N);
    REP(i, N) cin >> dim[i].first >> dim[i].second;

    vector< vector<int> > dp(N, vector<int>(N, 1e9));
    vector< vector<P> > mat_size(N, vector<P>(N));
    REP(i, N)
        FOR(j, i, N-1) mat_size[i][j] = make_pair(dim[i].first, dim[j].second);
    REP(i, N) dp[i][i] = 0;
    FOR(l, 1, N - 1) {
        REP(i, N - l) {
            FOR(j, 0, l-1)
                dp[i][i + l] = min(dp[i][i + l],  mat_size[i][i+j].second * mat_size[i][i+j].first * mat_size[i+j+1][i+l].second  + dp[i][i+j] + dp[i+j+1][i+l]);
        }
    }

    // REP(i, N) {
    //     REP(j, N) cerr << dp[i][j] << " ";
    //     cerr << endl;
    // }

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

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

const int MAX = 105;

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

  for (int i = 0; i <= n; i++) {
    m[i][i] = 0;
  }

  for (int i = 2; i <=n; i++) {
    for (int j = 1; j <= n - i + 1; j++) {
      int k = j + i - 1;
      m[j][k] = (1 << 21);
      for (int l = j; l < k; l++) {
        long long tmp = m[j][l] + m[l+1][k] + p[j-1] * p[l] * p[k];
        m[j][k] = m[j][k] > tmp ? tmp : m[j][k];
      }
    }
  }
  cout << m[1][n] << endl;
  return 0;
}