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 <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
pii A[200];//firstは行secondを列とする
int dp[200][200];//dp[i][j]:=Ai...Ajの最小スカラー積とする
int n;
int f[200][200];//最適な括弧を挿入する位置
void solve(){
	for(int i =0;i < n;i++){
		for(int j =0;j < n;j++){
			if(i==j)dp[i][j]=0;
			else dp[i][j]=INF;
		}
	}
	
	for(int k =1;k <n;k++){//鎖の数
		for(int i =0;i <n-k;i++){//どこから始めるか
			for(int j =i;j <i+k;j++){//どこに括弧を挟むか
				int tmp =A[i].first*A[j].second*A[i+k].second;
				if(dp[i][i+k] >dp[i][j]+dp[j+1][i+k]+tmp){
					dp[i][i+k]=dp[i][j]+dp[j+1][i+k]+tmp;
					f[i][i+k] =j;
				}
			}
		}
	}
}

void print(int i,int j){//解を表示する
	if(i==j){
		cout <<"A"<<i;
	}else{
		cout <<"(";
		print(i,f[i][j]);
		print(f[i][j]+1,j);
		cout <<")";
	}
}


int main(){
	cin>>n;
	for(int i =0;i < n;i++){
		cin>>A[i].first>>A[i].second;
	}
	solve();
	cout <<dp[0][n-1]<<endl;
	//print(0,n-1);
	return 0;
}

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

static const int NMAX = 100;
static const int RMAX = 100;
static const int CMAX = 100;
static const int INF = (1 << 29);

int main() {
    int n; cin >> n;
    int M[NMAX+1][NMAX+1];
    int r[RMAX+1];
    int c[CMAX+1];
    int min_cost;

    for (int i = 1; i < n+1; ++i) cin >> r[i] >> c[i];

    for (int i = 0; i < n; ++i) {
        for (int j = 1; j < n+1-i; ++j) {
            for (int k = 1+i; k < n+1; ++k) {
                if (j==k) {
                    M[j][k] = 0;
                    continue;
                }

                min_cost = INF;
                for (int l = j; l < k; ++l) {
                    min_cost = min(min_cost, M[j][l] + M[l+1][k] + (r[j]*c[l]*c[k]));
                }
                M[j][k] = min_cost;
            }
        }
    } 

    // for (int i = 1; i < n+1; ++i) {
    //     for(int j = 1; j < n+1; ++j) {
    //         cout << M[i][j] << " ";
    //     }
    //     cout << endl;
    // }

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


    return 0;
}

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

typedef long long ll;
#define inf 1e9+7

int n;
ll dp[102][102];
ll p[102];

void init(){
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            dp[i][j]=inf;
        }
        dp[i][i]=0;
    }
}

void cal(int l, int r){
    if(l>=r)return;
    for(int k=l;k<r;k++){
        cal(l,k);
        cal(k+1,r);
        dp[l][r] = min(dp[l][r], dp[l][k]+dp[k+1][r]+p[l]*p[k+1]*p[r+1]);
    }
}

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

    cin >> n;
    init();
    cin >> p[0] >> p[1];
    ll a,b;
    for(int i=1;i<n;i++){
        cin >> a >> b;
        p[i+1]=b;
    }
    cal(0,n-1);
    cout << dp[0][n-1] << endl;
    return 0;
}

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

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

typedef pair<int, int> pii;

const int N_SIZE = 100;

int dp[N_SIZE + 1][N_SIZE + 1];

void solve(int n, pii rc[])
{
  REP(i, n)
    dp[i][i] = rc[i].first * rc[i].second;
  REP(i, n - 1)
    dp[i][i + 1] = dp[i][i] * rc[i + 1].second;
  for(int i = 2; i < n; i++){
    for(int j = 0, k = i + j, m = 100000000; k < n; j++, k++){
      for(int l = j, x; l < k; l++){
	x = ((j == l) ? dp[j][l] * rc[k].second : dp[j][l]) + ((l + 1 == k) ? dp[l + 1][k] * rc[j].first : dp[l + 1][k]);
	if(j != l && l + 1 != k)
	  x += rc[j].first * rc[l].second * rc[k].second;
	m = min(m, x);
      }
      dp[j][k] = m;
      m = 100000000;
    }
  }
}
 
int main()
{
  int n;
  cin >> n;
  pii rc[n];
  REP(i, n)
    cin >> rc[i].first >> rc[i].second;
  solve(n, rc);
  cout << dp[0][n - 1] << endl;
}