Go: package main

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

const N = 100

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

func main() {
	var n int
	fmt.Scan(&n)
	p := make([]int, N+1)
	m := make([][]int, N+1)
	for i := range m {
		m[i] = make([]int, N+1)
	}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		scanner.Scan()
		p[i-1], _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		p[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 1; i <= n; i++ {
		m[i][i] = 0
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i+l-1
			m[i][j] = 1 << 21
			for 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])
			}
		}
	}
	fmt.Println(m[1][n])
}

C++: /*
 * chain_matrix.cpp
 *
 *  Created on: 2017/07/11
 *      Author: LitMc
 */

//#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout<<#x<<": "<<x<<endl
#define dbp2(x,y) cout<<#x<<","<<#y<<": "<<x<<","<<y<<endl
#define dbp3(x,y,z) cout<<#x<<","<<#y<<","<<#z<<": "<<x<<","<<y<<","<<z<<endl
#define dbp4(w,x,y,z) cout<<#w<<","<<#x<<","<<#y<<","<<#z<<": "<<w<<","<<x<<","<<y<<","<<z<<endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x,y)
#define dbp3(x,y,z)
#define dbp4(w,x,y,z)
#define ifcin(x)
#endif
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for(int i=from; i<to; ++i)
#define REP(i, from, to) for(int i=from; i<=to; ++i)
#define EPS = 1e-14;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::max;
using std::min;
using std::swap;
using std::string;
using std::fill;
using std::pair;
using std::sort;
using std::reverse;
using std::pair;
using std::greater;
using std::priority_queue;
using std::ostream;

template<typename T>
ostream& operator<<(ostream& out, const vector<vector<T> >& v) {
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i] << endl;
  }
  return out;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last) {
      out << ",";
    }
  }
  out << "]";
  return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

typedef pair<int, int> P;
bool cmp(const P &p1, const P &p2) {
  if (p1.first == p2.first) {
    return p1.second > p2.second;
  } else {
    return p1.first < p2.first;
  }
}
typedef pair<int, int> P;
static const int MAX_N = 110;
static const int INF = 100000000;
int n, r, c;
P M[MAX_N];
int dp[MAX_N][MAX_N];

//dp[i][j]: M[i]??????M[j]?????§??????????????????????°??????°
//dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + prod)
//k = [i, j)
//prod = M[i].row * M[k].col * M[j].col

void solve() {
  cin >> n;
  rep(i,0,n)
  {
    cin >> M[i].first >> M[i].second;
    dp[i][i] = 0;
  }
  for (int i = 0; i + 1 < n; ++i) {
    dp[i][i + 1] = M[i].first * M[i].second * M[i + 1].second;
    dbp4(i, M[i], M[i + 1], dp[i][i + 1]);
  }
  for (int num_matrix = 3; num_matrix <= n; ++num_matrix) { //number of matrix A*B*C -> 3
    for (int i = 0, j = i + num_matrix - 1; j < n; ++i, ++j) {
      dbp4(i, M[i], M[j], dp[i][j]);
      int mini = INT_MAX;
      for (int k = i; k + 1 <= j; ++k) {
        int prod = M[i].first * M[k].second * M[j].second;
        mini = min(mini, dp[i][k] + dp[k + 1][j] + prod);
      }
      dp[i][j] = mini;
      dbp4(i, M[i], M[j], dp[i][j]);dbp("----------");
    }
  }
  cout << dp[0][n - 1] << endl;
}

int main() {
  solve();
}

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

using namespace std;

int main () {

    int i, j, k, l, n, p[101], m[101][101];

    cin >> n;

    for (i = 1; i <= n; i++) {
        cin >> p[i - 1] >> p[i];
    }

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

    for (l = 2; l <= n; l++) {
        for (i = 1; i <= n - l+ 1; i++) {
            j = i + l - 1;
            m[i][j] = (1 << 21);

            for (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<iostream>
#include<algorithm>
using namespace std;

static const int N = 100;

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[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

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

  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int p[105];
int m[110][110];
int n;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> p[i - 1];
		cin >> p[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] = (i << 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] << "\n";
	return 0;
}

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

int	main()
{
	int	cnt[100][100];
	int	n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j)
			cnt[i][j] = -1;
		cnt[i][i] = 0;
	}
	vector<int>	r_vec(n), c_vec(n);
	for (int i = 0; i < n; ++i)
		cin >> r_vec.at(i) >> c_vec.at(i);
	for (int i = 1; i < n; ++i) {
		int	k;
		for (int j = 0; (k = i + j) < n; ++j) {
			for (int l = j; l < k; ++l) {
				int	tmp_cnt = cnt[j][l] + cnt[l + 1][k];
				tmp_cnt += r_vec.at(j) * c_vec.at(l) * c_vec.at(k);
				if (cnt[j][k] == -1 || cnt[j][k] > tmp_cnt)
					cnt[j][k] = tmp_cnt;
			}
		}
	}
	cout << cnt[0][n - 1] << endl;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
static const int N=100;
int main(){
  int n,p[N+1],m[N+1][N+1];
  scanf("%d",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-1+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]);
      }
    }
  }
  printf("%d\n",m[1][n]);
  return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<string, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

int n;
vector<ll> r(100), c(100);
ll dp[100][100];

// dp[l][r]
ll calc(int lx, int rx)
{
    if (dp[lx][rx] != 1e15)
    {
        return dp[lx][rx];
    }
    for (int i = lx; i < rx; i++)
    {
        dp[lx][rx] = min(dp[lx][rx], calc(lx, i) + calc(i + 1, rx) + r[lx] * c[i] * c[rx]);
    }
    return dp[lx][rx];
}

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

    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> r[i] >> c[i];
    for (int i = 0; i < n; i++)
    {
        dp[i][i] = 0;
        for (int j = i + 1; j < n; j++)
        {
            dp[i][j] = 1e15;
        }
    }
    cout << calc(0, n - 1) << endl;
}