Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(m[0][n-1])
}

C++: #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

ll dp[200][200];
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main() { 
  int n;
  cin >> n;
  Fill(dp,INT_MAX);
  vl r(n+1),c(n);
  rep(i,n) cin >> r[i] >> r[i+1];
  rep(i,n) dp[i][i] = 0;
  

  for(int w = 1;w<n;w++){
    for(int i =0;i+w<n;i++){
      int j = i+w;
      for(int k=i;k<j;k++){
        ll prod = r[i]*r[k+1]*r[j+1];
        dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+prod);
      }
    }
  }
  cout<<dp[0][n-1]<<endl;
  return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>


using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf(" %d", list[i]);
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct _Matrix {
    int row;
    int col;
} Matrix;

std::map<string, int> memo;
int GetCost(const vector<Matrix> list, int begin, int end)
{
    // printf("%d - %d\n", begin, end);
    if (begin == end) return 0;
    string hash = std::to_string(begin) + "," + std::to_string(end);
    if (memo[hash] != 0) return memo[hash];
    int min = std::numeric_limits<int>::max();
    for(int p = begin; p < end; p++){
        int r  = list[begin].row;
        int c1 = list[p].col;
        int c2 = list[end].col;
        int left = GetCost(list, begin, p);
        int right = GetCost(list, p + 1, end);
        min = std::min(min, left + right + r * c1 * c2);
        string lhash = std::to_string(begin) + "," + std::to_string(p);
        string rhash = std::to_string(p + 1) + "," + std::to_string(end);
        memo[lhash] = left;
        memo[rhash] = right;
        // min = std::min(min, GetCost(list, begin, p) + GetCost(list, p + 1, end) + r * c1 * c2);
    }
    return min;
}

void solve() {
    int n;
    scanf("%d", &n);
    vector<Matrix> matrixList(n);
    for(int i = 0; i < n; i++){
        int r, c;
        scanf("%d %d", &r, &c);
        matrixList[i] = {r,c};
    }
    int cost = GetCost(matrixList, 0, n - 1);
    printf("%d\n", cost);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    return 0;
}

C++: /*
* @Author: Samson
* @Date:   2018-06-11 12:09:01
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-11 15:37:51
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_B
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

int p[205],f[105][105];

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	int n;
	cin>>n;
	for(int i = 1; i <= n; ++i)
		cin>>p[i-1]>>p[i];
	memset(f,INF,sizeof f);
	for(int i = 1; i <= n; ++i)	f[i][i] = 0;
	for(int i = 2; i <= n; ++i)
	{
		for(int l = 1; l <= n-i+1; ++l)
		{
			int r = i+l-1;
			f[l][r] = INF;
			for(int k = l; k <= r-1; ++k)
				f[l][r] = min(f[l][r], f[l][k]+f[k+1][r]+p[l-1]*p[k]*p[r]);
		}
	}
	cout<<f[1][n]<<'\n';
	return 0;
}

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

template<typename T, typename U> void dmin(T &x, U y) { if(x > y) x = y; }

const int MAX_N = 100;
const int INF = 1001001001;

int main() {
  int n, r, c;
  int rc[MAX_N][2];
  cin >> n;
  for (int i=0; i<n; i++) {
    cin >> r >> c;
    rc[i][0] = r; rc[i][1] = c;
  }

  int dp[MAX_N][MAX_N];
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      dp[i][j] = INF;
    }
    dp[i][i] = 0;
  }

  for (int l=2; l<=n; l++) {
    for (int i=0; i+l-1<n; i++) {
      for (int j=1; j<l; j++) {
        dmin(dp[i][i+l-1], dp[i][i+j-1] + rc[i][0]*rc[i+j-1][1]*rc[i+l-1][1] + dp[i+j][i+l-1]);
      }
    }
  }

  cout << dp[0][n-1] << endl;
  return 0;
}