Go: package main

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

var cache [100][100]int

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	tables := []table{}
	for i := 0; i < num; i++ {
		sc.Scan()
		num1, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		num2, _ := strconv.Atoi(sc.Text())
		t := table{num1, num2}
		tables = append(tables, t)
	}

	fmt.Println(minimum(0, len(tables)-1, tables))
}

type table struct {
	v int
	h int
}

func minimum(i int, j int, t []table) int {
	if i == j {
		return 0
	}

	// fmt.Printf("i:%v j:%v\n", i, j)
	if cache[i][j] != 0 {
		return cache[i][j]
	}

	min := math.MaxInt64
	for k := i; k < j; k++ {
		p := t[i].v
		n := t[k].h
		q := t[j].h
		val := minimum(i, k, t) + minimum(k+1, j, t) + p*n*q
		if val < min {
			min = val
		}
	}

	cache[i][j] = min
	return min
}

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;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 100

struct Matrix {int r, c; };

int main(){
    int n; cin >> n;
    struct Matrix M[MAXN];
    for(int i=0; i<n; ++i){
        cin >> M[i].r >> M[i].c;
    }
    
    int dp[MAXN+1][MAXN];
    for(int i=0; i<n; ++i){
        dp[1][i] = 0;
    }
    for(int m=2; m<=n; ++m){
        for(int i=0; i<=n-m; ++i){
            priority_queue<int, vector<int>, greater<int> > pq;
            for(int j=1; j<m; ++j){
                //cout << "here with m= " << m << " i = " << i << " j = " << j << endl;
                int x = dp[j][i] + dp[m-j][i+j] + M[i].r*M[i+j].r*M[m+i-1].c;
                pq.push(x);
            }
            dp[m][i] = pq.top();
        }
    }
    cout << dp[n][0] << endl;
}

C++: // #include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <set>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i, n) for(int i=0;i<n;i++)
#define irep(i, n) for(int i=n-1;i>=0;i--)
#define lrep(i, n) for(long long i=0;i<n;i++)
typedef long long ll;
typedef vector<long long> llv;
typedef vector<vector<long long> > llvv;

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;
}

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

#define INFTY (1<<21)
#define MAX_N 100

int m[MAX_N + 1][MAX_N + 1], p[MAX_N + 1];
int n;

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

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

	matrixChainMultiplication();

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

	return 0;
}