Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func main() {
	sc.Split(bufio.ScanWords)
	var n int
	var M [101][101]int

	n = nextInt()

	P := make([]int, n*2)

	for i := 1; i <= n; i++ {
		P[i-1] = nextInt()
		P[i] = nextInt()
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			M[i][j] = 1000000
			for k := i; k <= j-1; k++ {
				M[i][j] = int(math.Min(float64(M[i][j]), float64(M[i][k]+M[k+1][j]+P[i-1]*P[k]*P[j])))
			}
		}
	}
	fmt.Println(M[1][n])
}

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

C++: #include <cstdio>
#include <algorithm>
using namespace std;
const int M = 128;
const int inf = (1<<25);

int d[M][M], p[M+2];
int path[M][M];

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d %d", p+i, p+i+1);
	
	for (int i = 1; i <= n; ++i) d[i][i] = 0;
	for (int ln = 2; ln <= n; ++ln)	// 枚举矩阵链的长度 
	{
		int m = n - ln + 1; 		// 剩余矩阵的长度 
		
		for (int i = 1; i <= m; ++i)// len-ln = j-i+1 j = ln+i-1
		{
			int j = ln + i - 1;
			d[i][j] = inf;			
			for (int k = i; k < j; ++k)
				d[i][j] = min(d[i][j], d[i][k] + d[k+1][j] + p[i-1] * p[k] * p[j]);
		}		
	}
	
	printf("%d\n", d[1][n]);
	return 0;
}