Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	mm := make([]int, n+1)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		mm[i] = getNextInt(scanner)
		mm[i+1] = getNextInt(scanner)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			for k := i; k <= j-1; k++ {
				d := dp[i][k] + dp[k+1][j] + mm[i-1]*mm[k]*mm[j]
				if dp[i][j] == 0 || dp[i][j] > d {
					dp[i][j] = d
				}
			}
		}
	}

	fmt.Fprintln(writer, dp[1][n])

	writer.Flush()
}

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG


struct Matrix {
  int numOfRow;
  int numOfColumn;
};


struct MinMatrix {
  int minMultiply;
  int numOfRow;
  int numOfColumn;
};


const int kMaxSize = 100;
const int kInvalid = -1;
MinMatrix minArrays[kMaxSize][kMaxSize];


bool isValid(MinMatrix mtx)
{
  return mtx.minMultiply != kInvalid ? true : false;
}

MinMatrix getMinScalarMultiply(const Matrix matrices[], int start, int end)
{
  DPRINTF("start %d  end %d\n", start, end);

  assert(start <= end);

  if (isValid(minArrays[start][end]))
    return minArrays[start][end];

  if (start + 1 == end)
  {
    MinMatrix mtx;

    mtx.minMultiply = 0;
    mtx.numOfRow = matrices[start].numOfRow;
    mtx.numOfColumn = matrices[start].numOfColumn;

    minArrays[start][end] = mtx;

    return minArrays[start][end];
  }


  MinMatrix minMtx;
  minMtx.minMultiply = INT_MAX;

  for (int i = start + 1; i < end; i++)
  {
    MinMatrix mtx0 = getMinScalarMultiply(matrices, start, i);
    MinMatrix mtx1 = getMinScalarMultiply(matrices, i, end);

    MinMatrix mtx;

    mtx.minMultiply = mtx0.minMultiply + mtx1.minMultiply + mtx0.numOfRow * mtx0.numOfColumn * mtx1.numOfColumn;
    mtx.numOfRow = mtx0.numOfRow;
    mtx.numOfColumn = mtx1.numOfColumn;

#if DEBUG
    DPRINTF("start %d  end %d  i %d : mtx.minMultiply %d  min.minMultiply %d\n",
        start, end, i, mtx.minMultiply, minMtx.minMultiply);
    DPRINTF("  mtx0.minMultiply %d  mtx1.minMultiply %d\n",
        mtx0.minMultiply, mtx1.minMultiply);
#endif

    if (minMtx.minMultiply > mtx.minMultiply)
      minMtx = mtx;
  }

  DPRINTF("start %d  end %d  multiply %d\n", start, end, minMtx.minMultiply);

  minArrays[start][end] = minMtx;

  return minArrays[start][end];
}


int calcMinScalarMultiply(const Matrix matrices[], int num)
{
  if (num <= 1)
    return 0;

  MinMatrix mtx = getMinScalarMultiply(matrices, 0, num);

  return mtx.minMultiply;
}


int main() {

  // init
  for (int i = 0; i < kMaxSize; i++)
    for (int j = 0; j < kMaxSize; j++)
      minArrays[i][j].minMultiply = kInvalid;

  int numOfMatrix;
  cin >> numOfMatrix; cin.ignore();

  Matrix  matrices[numOfMatrix];
  for (int i = 0; i < numOfMatrix; i++)
  {
    cin >> matrices[i].numOfRow; cin >> matrices[i].numOfColumn; cin.ignore();
  }

#if DEBUG
  for (int i = 0; i < numOfMatrix; i++)
  {
    DPRINTF("Matrix %d: %d %d\n", i, matrices[i].numOfRow, matrices[i].numOfColumn);
  }
#endif

  cout << calcMinScalarMultiply(matrices, numOfMatrix) << endl;

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>


#define inf 1000000007

using namespace std;

int main(){
	int n;
	cin >> n;
	vector<pair<int,int> > m(n);
	for(int i=0;i<n;++i)cin >> m[i].first >> m[i].second;
	vector<vector<int> > dp;
	dp.resize(n);
	for(int i=0;i<n;++i){
		dp[i].resize(n);
		for(int j=0;j<n;++j)dp[i][j] = 0;
	}
	vector<int> p;
	for(int i=0;i<n;++i)p.push_back(m[i].first);
	p.push_back(m[n-1].second);
	for(int i=1;i<n;++i){
		for(int j=0;j+i<n;++j){
			dp[j][i+j] = inf;
			int t = i+j;
			for(int k=j+1;k<=t;++k){
				dp[j][t] = min(dp[j][t],dp[j][k-1]+dp[k][t]+p[j]*p[k]*p[t+1]);
			}
		}
	}
	cout << dp[0][n-1] << endl;
}

C++: /*
　　　∫ ∫ ∫
　　　ノヽ
　　（＿　 ）
　（＿　　　 ）
（＿＿＿＿＿＿ ）
　ヽ(´･ω･)ﾉ　
　　 |　 /
　　 UU
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
using P = pair<ll, ll>;
const int MOD = (int)1e9 + 7;
const ll INF = 1LL << 60;
const int inf = 1<<30;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
template <typename T>
ostream& operator<<(ostream& os, vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, pair<T,T> const&P){
    os << P.first;
    os << " ";
    os << P.second;
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
     os<<endl;
    return os;
}
//fixed<<setprecision(10)<<ans<<endl;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> A(N+1);
    REP(i,N){
        int a,b;
        cin >> a >> b;
        A[i]=a;
        A[i+1]=b;
    }

    int ans=0;
    vector<vector<int>> dp(N,vector<int>(N,inf));
    REP(i,N) dp[i][i]=0;

    for(int i=1;i<N;i++){
        for(int left=0;left<N-i;left++){
            int right = left+i;
            for(int k=0;k<i;k++){
                int mid = left+k;
                chmin(dp[left][right], dp[left][mid] + A[left]*A[mid+1]*A[right+1] + dp[mid+1][right] );
            }
        }
    }
    ans = dp[0][N-1];
    cout << ans << endl;
}

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

template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }

int mul(const int r1, const int c1, const int r2, const int c2) {
	assert(c1 == r2);
	return r1 * c2 * r2;
}

int main() {
	int n;
	cin >> n;

	vector<int> R(n), C(n);
	for(int i = 0; i < n; ++i) {
		cin >> R[i] >> C[i];
	}

	constexpr int INF = (1 << 29);
	vector<vector<int>> dp(n, vector<int>(n + 1, INF));
	for(int i = 0; i < n; ++i) {
		dp[i][i + 1] = 0;
	}

	for(int len = 0; len <= n; ++len) {
		for(int l = 0; l + len <= n; ++l) {
			const int r = l + len;
			for(int i = l + 1; i < r; ++i) {
				chmin(dp[l][r], dp[l][i] + dp[i][r] + mul(R[l], C[i - 1], R[i], C[r - 1]));
			}
		}
	}

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

	return EXIT_SUCCESS;
}