Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	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
			//kまでで区切る
			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])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[1][n])
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;

int n,t;
ll a[105],dp[105][105];

ll solve(int i,int j) {
	if (i==j) return 0;
	if (dp[i][j]) return dp[i][j];
	dp[i][j]=INF;
	for (int k=i+1;k<=j;k++)
		dp[i][j]=min(dp[i][j],solve(i,k-1)+solve(k,j)+a[i]*a[k]*a[j+1]);
	return dp[i][j];
}

int main() {
	cin>>n;
	for (int i=1;i<=n;i++) if (i==n) cin>>a[i]>>a[i+1];
	else cin>>a[i]>>t;
	solve(1,n);
	cout<<dp[1][n]<<'\n';
}

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

signed main() {
	int n, 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; 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 <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[k + 1][j] + p[i - 1] * p[k] * p[j]);
               }
          }
     }

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

     return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
#define range(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repR(i, n) for(int i = (n) - 1; i > -1; i--)
#define rep1(i, n) for(int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for(int i = (n); i > 0; i--)
using namespace std;

int n;
int m[110][110];
int p[110];

int matrixChainMultiplication() {
  rep1(i, n) m[i][i] = 0;

  range(l, 2, n) {
    rep1(i, n - l + 1) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      range(k, i, j - 1) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  return m[1][n];
}

int main() {
  cin >> n;
  rep1(i, n) {
    cin >> p[i - 1] >> p[i];
  }
  cout << matrixChainMultiplication() << endl;
  return 0;
}

C++: //ALDS1_10B Matrix Chain Multiplication
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

static const int N=100;
vector<int> p(N+1);
vector< vector<int> > m(N+1,vector<int>(N+1));

int main(){
 int n;
 cin >> n;
 for(int i=1;i<=n;i++){
 cin>>p[i-1]>>p[i];
 }
 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<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int main()
{
	int dp[100][100];
	int P[101];

	int n;
	cin >> n;
	cin >> P[0] >> P[1];
	for (int i = 1; i < n; i++)
	{
		int t;
		cin >> t >> P[i + 1];
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; (j + i) < n; j++)
		{
			if (i == 0)
			{
				dp[j][j + i] = 0;
			}
			else if (i == 1)
			{
				dp[j][j + i] = P[j] * P[j + 1] * P[j + 2];
			}
			else
			{
				int mini = 10000000;
				for (int k = 0; k < i; k++)
				{
					mini = min(mini, dp[j][j + k] + dp[j + k + 1][j + i] + P[j] * P[j + k + 1] * P[j + i + 1]);
				}
				dp[j][j + i] = mini;
			}
		}
	}

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

	return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>

using namespace std;
static const int N = 100;

int main(){
  //int i,j,k,l;
  int n,p[N+1],m[N+1][N+1];
  cin >> n;
  for(int i = 1; i <= n; i++){
    cin >> p[i-1];
    cin >> 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] = INFINITY;
      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) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}

vl R, C;

vector<vl> dp;

ll calc(int l, int r) {
    if(dp[l][r] != INF) return dp[l][r];
    auto& ret = dp[l][r];
    if(r - l == 1) return ret = 0;
    if(r - l == 2) return ret = R[l] * C[l] * C[l+1];
    for(int m = l + 1; m < r; ++m) {
        chmin(ret, R[l] * C[m-1] * C[r-1] + calc(l, m) + calc(m, r));
    }
    return ret;
}

int main(){
    int n;
    cin >> n;
    R.resize(n);
    C.resize(n);
    rep(i, n) cin >> R[i] >> C[i];
    dp = vector<vl>(n+1, vl(n+1, INF));
    cout << calc(0, n) << endl;
    return 0;
}