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 <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

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

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

ll dp[110][110];
ll matrix[110][2];

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

  REP(i, N) {
    cin >> matrix[i][0] >> matrix[i][1];
  }

  for (int i = 0; i < N - 1; i++) {
    dp[i][i + 1] = matrix[i][0] * matrix[i][1] * matrix[i + 1][1];
  }
  for (int i = 0; i < N; i++) {
    dp[i][i] = 0;
  }

  // cout << dp[0][1] << endl;

  for (int l = 2; l <= N; l++) {
    for (int i = 0; i < N - l + 1; i++) {
      int j = i + l - 1;

      dp[i][j] = INF;

      for (int k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + matrix[k][0] * matrix[k][1] * matrix[k + 1][1]);
      }
    }
  }




  cout << dp[0][N - 1] << 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=1; i<=n; i++) {
        cin >> p[i-1] >> p[i];
    }
    
    for (int i=0; i<=n; i++) m[i][i] = 0;
    for (int l=2; l<=n; l++) {
        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 <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstring>
#include <queue>
#include <string>

using namespace std;

const double PI = 3.14159265358979;


int matrix_chain_multi(int p[], int len) {	// 1 2 3 num

	int m[101][101]{};
	const int num = len - 1;
	for (int L = 2; L <= num; L++) {
		
		for (int i = 1; i <= num - L + 1; i++) {
			int j = i + L - 1;
			int minx = INT_MAX;
			for (int k = i; k < j; k++)	{
				minx = min(minx, m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
			m[i][j] = minx;
		}
	}

	return m[1][len - 1];
}

int main() {

	int n; cin >> n;
	n = n + 1;

	int* p = new int[n];		// p[i-1] hang		p[i] lie
	int hang, lie, temp;
	cin >> p[0];
	for (int i = 1; i < n; i++) {
		cin >> p[i];
		if (i != n - 1)
			cin >> temp;
	}
	cout << matrix_chain_multi(p, n) << endl;


	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <tuple>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <random>
#include <cstdlib>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;

ll memo[105][105];
int r[105], c[105];

ll func(int a, int b) {
	if (b - a == 0) { 
		memo[a][b] = 0;
	}
	else {
		ll m = INT_MAX;
		for (int k = a;k < b;k++) {
			m = min(m, func(a, k) + func(k + 1, b) + r[a] * c[k] * c[b]);
		}
		memo[a][b] = m;
	}
	return memo[a][b];
}

int main() {
	int n;
	cin >> n;
	Loop(i, n) {
		cin >> r[i] >> c[i];
	}
	cout << func(0, n - 1) << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namescape 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;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>
using namespace std;
#define INF 10000000

int M[101][101],p[101];

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] = INF;
      for ( int k = i; k <= j-1; 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 <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>

using namespace std;

int r[100], c[100];
int dp[100][100];

int optMat(int left, int right)
{
	if (dp[left][right] >= 0){
		return (dp[left][right]);
	}
	if (left == right){
		return (0);
	}
	
	int res = INT_MAX;
	for (int i = left; i < right; i++){
		res = min(res, optMat(left, i) + optMat(i + 1, right) + r[left] * c[i] * c[right]);
	}
	
	return (dp[left][right] = res);
}

int main()
{
	int N;
	
	scanf("%d", &N);
	
	for (int i = 0; i < N; i++){
		scanf("%d %d", &r[i], &c[i]);
	}
	
	memset(dp, -1, sizeof(dp));
	
	printf("%d\n", optMat(0, N - 1));
	
	return (0);
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
using namespace std;

struct Matrix{int r,c;};

int main(){
	int n;
	int i,k,l,f;
	cin >> n;
	vector<Matrix> M(n);
	
	for (i=0;i<n;i++){
		cin >> M[i].r;
		cin >> M[i].c;
	}
	
	int DPtable[n][n];
	for (i=0;i<n;i++){
		DPtable[i][i]=0;
	}
	
	for (k=1;k<n;k++){
		for (i=0;i<n-k;i++){
			int m=INT_MAX;
			for (l=0;l<=k-1;l++){
				f=M[i].r*M[i+l].c*M[i+k].c;
				if (DPtable[i][i+l]+DPtable[i+l+1][i+k]+f<m){
					m=DPtable[i][i+l]+DPtable[i+l+1][i+k]+f;
				}
			}
			DPtable[i][i+k]=m;
			
		}
	}
	
	cout << DPtable[0][n-1] <<endl;
	
	return 0;
}