Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	p := make([]int, N+1)
	dp := make([][]int, N+1)
	for i, _ := range dp {
		dp[i] = make([]int, N+1)
	}
	for i := 0; i < N; i++ {
		p[i] = sc.nextInt()
		p[i+1] = sc.nextInt()
	}
	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 << 60
			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])
			}
		}
	}

	fmt.Fprintln(wtr, dp[1][N])
	wtr.Flush()
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<stack>
#include<queue>
#include<sstream>
#include<string>
#include<set>
#include<array>
#include<cassert>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i);

using namespace std;

const int MAX=101;
const int INF=1<<29;

int dp[MAX][MAX];
int n;
int c[MAX];
int r[MAX];

int main(){

	fill_n((int *)dp,sizeof(dp)/sizeof(int),INF);
	fill_n((int *)r,sizeof(r)/sizeof(int),0);
	fill_n((int *)c,sizeof(c)/sizeof(int),0);

	cin >> n;

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

	rep(i,n){
		dp[i][i+1] = 0;
	}

	for(int R=2;R<=n;R++){
		for(int L=0;L+R-1<n;L++){
			for(int M=L+1;M<=L+R-1;M++){
				dp[L][L+R] = min(dp[L][L+R],dp[L][M] + dp[M][L+R] + (r[L] * c[M] * c[L+R-1]));
			}
		}
	}

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

}

C++: #include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())

static const int N = 1000;

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 <bits/stdc++.h>
using namespace std;

#define endl '\n'

typedef long long ll;

const ll oo = 0x3f3f3f3f3f3f3f3f;

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

	int n;
	cin >> n;

	vector<int> v(n + 1);
	for(int i = 0; i < n; ++i)
		cin >> v[i] >> v[n];

	vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, oo));

	for(int l = 0; l < 2; ++l)
		for(int i = 0; i + l <= n; ++i)
			dp[i][i + l] = 0;

	for(int l = 2; l <= n; ++l)
		for(int i = 0; i + l <= n; ++i)
			for(int k = i + 1; k < i + l; ++k)
				dp[i][i + l] = min(dp[i][i + l],
								   dp[i][k] + dp[k][i + l] + (ll) v[k] * v[i] * v[i + l]);

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

    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

static const int N = 100;

void mcm() {
    int n = 4;
    int p[n+1], m[n+1][n+1];

    int sample[][4] = {{5, 4}, {4, 6}, {6, 2}, {2, 7}};

    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        p[i -1] = sample[cnt][0];
        p[i] = sample[cnt][1];
        cnt++;
    }
    cout << "Values of p: " << endl;
    for (int i = 0; i <=n; i++) cout << p[i] << " ";
    cout << endl;

    //diagonal part is always 0
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <=n; j++) {
            m[i][j] = 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; k++) {
                m[i][j] = min(m[i][j], m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]);
            }
        }
    }

    cout << "Result table: " << endl;
    for (int i = 1; i <=n; i++) {
        for (int j = 1; j <=n; j++) {
            cout << m[i][j] << " ";
        }
        cout << endl;
    }
}


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];
    }
    //diagonal part is always 0
    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; 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;
    //mcm();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
typedef long long LL;
const int INF=10000000;
int r[110], c[110];
int dp[110][110];

int CalcMin(int L, int R){
  if(L==R) return dp[L][R]=0;
  int res=INF;
  rep(M,R-L){
    int left, right;
    if(dp[L][L+M]!=-1) left=dp[L][L+M];
    else left=CalcMin(L,L+M);
    if(dp[L+M+1][R]!=-1) right=dp[L+M+1][R];
    else right=CalcMin(L+M+1,R);
    res=min(res,left+right+r[L]*c[L+M]*c[R]);
  }
  return dp[L][R]=res;
}

int main(){
  int n;
  cin >> n;
  rep(i,n) cin >> r[i] >> c[i];
  rep(i,n){
    rep(j,n) dp[i][j]=-1;
  }
  cout << CalcMin(0,n-1) << endl;

  return 0;
}

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

const int MAX = 105;
const int INF = 1<<29;
struct Matrix
{
    int r, c;
};
Matrix M[MAX];
int dp[MAX][MAX], n;

int DP(int i, int j)
{
    int &ans = dp[i][j];
    if(ans!=-1) return ans;
    ans = INF;
    for(int k=i; k<j; k++)
    {
        ans = min(ans, DP(i, k)+DP(k+1, j)+M[i].r*M[k].c*M[j].c);
    }
    return ans;
}
int main()
{
    scanf("%d", &n);
    memset(dp, -1, sizeof(dp));
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d", &M[i].r, &M[i].c);
        dp[i][i] = 0;
    }
    printf("%d\n", DP(1, n));
    return 0;
}