Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

type matrixSize struct{ row, col int }

func calcNum(i, j matrixSize) int {
	return i.row * j.col * i.col
}

func parseProblem() (int, []matrixSize) {
	n := scanInt(sc)
	sizes := make([]matrixSize, n)
	for i := 0; i < n; i++ {
		sizes[i] = matrixSize{row: scanInt(sc), col: scanInt(sc)}
	}
	return n, sizes
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = math.MaxInt64
		}
	}
}

func resolve(n int, sizes []matrixSize) int {
	// TODO 復習
	initDP(n)
	_rec(sizes, 0, n)
	return dp[0][n]
}

func _rec(sizes []matrixSize, l, r int) int {
	if dp[l][r] != math.MaxInt64 {
		return dp[l][r]
	}
	if r-l <= 1 {
		dp[l][r] = 0
		return 0
	}
	num := math.MaxInt64
	for i := l + 1; i < r; i++ {
		v := _rec(sizes, l, i) + _rec(sizes, i, r) + calcNum(matrixSize{sizes[l].row, sizes[i].row}, matrixSize{sizes[i].row, sizes[r-1].col})
		if v < num {
			num = v
		}
	}
	dp[l][r] = num
	return dp[l][r]
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using qi = queue<int>;
using Graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)

ll dp(int* r, int* c, int n) {
    ll table[n][n];
    REP(i, n) table[i][i] = 0;
    FOR(i, 1, n) {
        REP(j, n-i) {
            table[j][j+i] = table[j][j+i-1] + r[j]*c[j+i-1]*c[j+i];
            FOR(k, j, j+i) {
                table[j][j+i] = min(table[j][k] + r[j]*c[k]*c[j+i] + table[k+1][j+i], table[j][j+i]);
            }
        }
    }
    return table[0][n-1];
}

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

    int n; cin >> n;
    int r[n], c[n];
    REP(i, n) {
        cin >> r[i] >> c[i];
    }

    // a に含まれる n 個の値で k を作ったときの最大値は何か
    int table = dp(r, c, n);

    cout << table << "\n";
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_B
// Dynamic Programming - Matrix Chain Multiplication
// TODO: review

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

const ui64 MAX_N = 100;
// dp[i][j] [i, j]までの行列をかけたときの最小コスト
int dp[MAX_N + 1][MAX_N + 1];
int p[MAX_N + 1];

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

    // initialization
    for (int i = 1; i <= n; ++i) dp[i][i] = 0;

    for (int l = 2; l <= n; ++l) {
        for (int i = 1; i <= n - l + 1; ++i) {
            auto j = i + l - 1;
            dp[i][j] = 1 << 21;
            for (int k = i; k <= j - 1; ++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;
    return 0;
}

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

long long dp[105][105];
pair<int,int> p[105];

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) dp[i][j]=2e9;
    for(int i=0;i<n;i++) {cin>>p[i].first>>p[i].second;dp[i][i]=0;}
    for(int i=0;i < n;i++){
        for(int j=i;j < n;j++){
            for(int k=i;k<j;k++){
                dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i].first*p[k].second*p[j].second);
            }
        }
    }
    for(int i=0;i < n;i++){
        for(int j=i;j < n;j++){
            for(int k=i;k<j;k++){
                dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i].first*p[k].second*p[j].second);
            }
        }
    }
    cout<<dp[0][n-1]<<endl;
    return 0;
}

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

int nlist[110];
int dp[110][110];

int main(){
  int n;
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> nlist[i] >> nlist[i+1];
  }
  for(int i = 1; i < n; i++){
    for(int j = 0; j < n - i; j++){
      dp[j][j+i] = 1 << 30;
      for(int k = 0; k < i; k++){
	    dp[j][j+i] = min(dp[j][j+i], dp[j][j+k] + dp[j+k+1][j+i] + nlist[j] * nlist[j+k+1] * nlist[j+i+1]);
      }
    }
  }
  cout << dp[0][n-1] << endl;
  return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int minana(int a,int b){
  if(a<b){
    return a;
  }
  else if(a>b){
    return b;
  }
  else return a;
}


int main(){
  int n;
  cin>>n;
  int r[100];
  int m[101][101];
  int j;
  int q=0;
  //int c[n];
  for(int i=1;i<=n;i++){
    cin>>r[i-1]>>r[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++){
      j=i+l-1;
      m[i][j]=(1<<21);
      for(int k=i;k<j;k++){
	q=m[i][k]+m[k+1][j]+r[i-1]*r[k]*r[j];
	m[i][j]=minana(m[i][j],q);
      }
    }
  }
  
  cout<<m[1][n]<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int solve(vector<vll> &c, vector<pii> &m, int s, int e1) {
  int e = e1 - 1;
  if (c[s][e1] != 0) return c[s][e1];
  
  if (e1 - s == 2) {
    c[s][e1] = m[s].first * m[s].second * m[e].second;
    return c[s][e1];
  }

  ll n_calc = LLONG_MAX;
  for (int i = s + 1; i < e1; i++) {
    ll total = 0;
    
    if (i - s > 1) {
      c[s][i] = solve(c, m, s, i);
      total += c[s][i];
    }
    
    if (e1 - i > 1) {
      c[i][e1] = solve(c, m, i, e1);
      total += c[i][e1];
    }

    total += m[s].first * m[i].first * m[e].second;
    
    n_calc = min(n_calc, total);
  }

  c[s][e1] = n_calc;
  return c[s][e1];
}

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

  vector<pii> m(n);
  rep(i, n) cin >> m[i].first >> m[i].second;

  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }

  vector<vll> c(n + 1, vll(n + 1, 0));
  cout << solve(c, m, 0, n) << endl;
}

C++: #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[101][101],p[101];

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