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;
int dp[101][101];
int r[100],c[100];
int solv(int a,int b){//a??????b-1?????§?????????????????????????????????????????°???????°???¨?????????????????????????????????
    if(dp[a][b]==-1){
        if(a == b-1){
            dp[a][b]=0;
        }else if(a==b-2){
            dp[a][b]=r[a]*c[a]*c[a+1];
        }else{
            int Pmin=999999999;
            for(int j=a+1;j<b;j++){
                int tmp=solv(a,j)+solv(j,b)+r[a]*r[j]*c[b-1];
                Pmin=min(Pmin,tmp);
            }
            dp[a][b]=Pmin;
        }
    }
    return dp[a][b];
}
int main(){
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++){
            dp[i][j]=-1;
        }
    }
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>r[i]>>c[i];
    }
    int ans=solv(0,N);
    cout<<ans<<endl;
    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=0;i<n;i++) cin >> p[i] >> p[i+1];
    for(int l=1;l<=n;l++) {
        for(int i=0;i<=n-l;i++) {
            int j = i + l;
            if(l==1) m[i][j] = 0;
            else if(l==2) m[i][j] = p[i]*p[i+1]*p[j];
            else {
                m[i][j] = 1000000000;
                for(int k=1;k<l;k++) {
                    m[i][j] = min(m[i][j], m[i][i+k] + m[i+k][j] + p[i]*p[i+k]*p[j]);
                }
            }
        }
    }
    //cout << m[4][6] << endl;
    cout << m[0][n] << endl;
    
    return 0;
}

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

#define N 100
#define INF 1000000
int main(){
	int n, tmp, p[N + 1], dp[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++){
		dp[i][i] = 0;
	}
	
	for(int l = 2 ; l <= n ; l++){    //l = ???????????????????????????????????????
		for(int i = 1 ; i <= n - l + 1 ; i++){
			int j = i + l - 1; 
			dp[i][j] = INF;
			for(int k = i ; k <= j - 1 ; k++){
				tmp = dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j];
			if(dp[i][j] > tmp) dp[i][j] = tmp;
			else dp[i][j] = dp[i][j];
				}
		}
	}
	
	cout << dp[1][n] << endl;
	
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
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++)
    scanf("%d%d",&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-1+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<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;

int n;

int m[101][101];

int p[101];


void matrix(){
  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] = 1000000;
      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]);
      }
    }
  }
}

int main(){

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

  matrix();



  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 - 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 <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string.h>
#include <set>

using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> void printvec(const vector<T>& v) {
  for (auto x : v) { cout << x << " "; }
  cout << endl;
}
template<class T> void printtree(const vector< vector<T> >& tree) {
  for (long long i = 0; i < tree.size(); ++i) {
    cout << i + 1 << ": "; printvec(tree[i]);
  }
}
template<class T, class U> void printmap(const map<T, U>& mp) {
  for (auto x : mp) { cout << x.first << "=>" << x.second << endl; }
}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

#define rep(i, n) for(ll i = 0; i < n; ++i)
#define all(s) s.begin(), s.end()
#define sz(x) (ll)(x).size()
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> triple;
typedef double D;
typedef vector<ll> vl;
typedef vector<P> vp;

const ll INF = 1e9;
const ll MOD = 1000000007;  // 1e9 + 7

int main(int argc, char** argv) {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  //cout << setprecision(10) << fixed;

  ll n;
  cin >> n;
  vector<ll> r(n);
  vector<ll> c(n);
  rep(i, n) {
    cin >> r[i] >> c[i];
  }

  // dp[i][j] .. min of dp[i][k] + dp[k+1][j] + r[i]*c[k]*c[j]. (dp[i][j] = 0 if i == j).
  vector<vector<ll>> dp(n, vector<ll>(n, INF));
  rep(i,n) { dp[i][i] = 0; }
  rep(d,n+1) {
    rep(i,n) {
      ll j = i+d-1;
      if (j >= n) { continue; }
      for (ll k = i; k < j; ++k) {
        chmin(dp[i][j], dp[i][k] + dp[k+1][j] + r[i]*c[k]*c[j]);
      }
    }
  }
  // printtree(dp);
  cout << dp[0][n-1] << endl;
}