Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&a[i+1])
	}

	m := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		m[i] = make([]int, n+1)
	}

	for d := 2; d <= n; d++ {
		for s := 0; s+d <= n; s++ {
			min := 1 << 30
			for j := s+1; j < s+d; j++ {
				val := m[s][j] + m[j][s+d] + a[s]*a[j]*a[s+d]
				if val < min {
					min = val
				}
			}
			m[s][s+d] = min
		}
	}
	fmt.Println(m[0][n])
}

C++: #include<algorithm>
#include<cstdio>
#include<cstring>
#include<limits>
#define rep(i,a) for(int i=0;i<(a);++i)
#define clr(a,v) memset((a),(v),sizeof(a))

const int MAX_N = 100, INF = std::numeric_limits<int>::max()>>2;

int n;
int r[MAX_N], c[MAX_N];
int dp[MAX_N][MAX_N];

int rec( int b, int e )
{
    if( b == e )
        return dp[b][e] = 0;

    if( ~dp[b][e] )
        return dp[b][e];

    int ret = INF;

    rep( i, e-b )
        ret = std::min( ret, rec( b, b+i )+rec( b+i+1, e )+r[b]*c[b+i]*c[e] );

    return dp[b][e] = ret;
}

int main()
{
    scanf( "%d", &n );
    rep( i, n )
        scanf( "%d%d", r+i, c+i );

    clr( dp, -1 );

    printf( "%d\n", rec( 0, n-1 ) );

    return 0;
}

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

int r[105];
int c[105];
int cache[105][105];

int f(int a, int b) {
    if (a == b) return 0;
    if (cache[a][b] == -1) {
        cache[a][b] = 2000000000;
        for (int i = a; i < b; i++) {
            int res = f(a, i) + f(i + 1, b) + r[a] * c[i] * c[b];
            if (res < cache[a][b]) cache[a][b] = res;
        }
    }
    return cache[a][b];
}

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> r[i];
        cin >> c[i];
    }
    memset(cache, -1, sizeof(cache));
    cout << f(0, n-1);
}

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

class MatrixChainMultiplication {
private:
	int n, *r, *c;
	int memory[100][100];
public:
	MatrixChainMultiplication(int _n, int _r[], int _c[]) {
		n = _n;
		r = _r;
		c = _c;
		for (int i=0; i<n; i++) {
			for (int j=1; j<=n; j++) {
				memory[i][j] = INT_MAX;
			}
		}
	}
	int count(int start, int end) {
		if (memory[start][end] != INT_MAX)
			return memory[start][end];
		
		if (start + 1 == end) {
			memory[start][end] = 0;
		} else {
			int temp;
			for (int i=start+1; i<end; i++) {
				temp = count(start, i) + count(i, end) + (r[start] * r[i] * c[end - 1]);
				if (memory[start][end] > temp) memory[start][end] = temp;
			}
		}
		return memory[start][end];
	}
};

int main() {
	int n, r[100], c[100];
	
	cin >> n;
	for (int i=0; i<n; i++) {
		cin >> r[i] >> c[i];
	}
	
	auto mcm = MatrixChainMultiplication(n, r, c);
	
	cout << mcm.count(0, n) << '\n';
	
	return 0;
}

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

#define MAX_SIZE (110)
int P[MAX_SIZE];
int M[MAX_SIZE][MAX_SIZE];

int main()
{
  int N;
  std::cin >> N;
  for (int i = 1; i <= N; ++i)
  {
    std::cin >> P[i - 1] >> P[i];
  }

  for (int i = 0; i <= N; ++i) M[i][i] = 0;
  for (int n = 2; n <= N; ++n)
  {
    for (int i = 1; i <= N - n + 1; ++i)
    {
      int j = i + n - 1;
      M[i][j] = 1 << 30;
      for (int k = i; k < j; ++k)
      {
        M[i][j] = std::min(M[i][j], M[i][k] + M[k + 1][j] + P[i - 1] * P[k] * P[j]);
      }
    }
  }

  std::cout << M[1][N] << std::endl;

  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int n;
	int p[101], m[101][101];
	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 rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()

template <class T>
inline bool chmin(T &a, const T b){
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, const T b){
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using DoP = pair<double, double>;
using edge = struct { int to; double cost;}; // edge x = {to, cost}

const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1<<29;
const ll INF64 = 1ll << 45;
const int mod = 1000000007;

struct mint
{
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a)
  {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a)
  {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a)
  {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const
  {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const
  {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const
  {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const
  {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const
  {
    return pow(mod - 2);
  }
  mint &operator/=(const mint a)
  {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const
  {
    mint res(*this);
    return res /= a;
  }
};
// h(n, r) = c(n+r-1, r)
struct combination
{
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1)
  {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k)
  {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

int n;
vector<P> x;
vector<vector<ll>> dp;

ll rec(int l = 0, int r = n) {
  if(r-l <= 1) dp[l][r] = 0;
  if(r-l==2) dp[l][r] = x[l].first*x[l].second*x[l+1].second;
  if(dp[l][r] != INF64) return dp[l][r];
  for(int mid = l+1; mid < r; ++mid) chmin(dp[l][r], rec(l, mid)+rec(mid, r)+x[l].first*x[mid].first*x[r-1].second);
  return dp[l][r];
}

int main(){
  cin >> n;
  x.resize(n);
  dp.assign(n+1, vector<ll>(n+1, INF64));
  rep(i, n) cin >> x[i].first >> x[i].second;
  cout << rec() << endl;
}