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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int N;

struct Matrix{ int R, W; };
Matrix M[MAX_N];

int mem[MAX_L][MAX_L];
Matrix memM[MAX_L][MAX_L];


void eval(Matrix M[], int s, int e){

	if (s == e){
		mem[s][e] = 0;
		memM[s][e] = M[s];
		return;
	}

	int minval = 1000000000;
	for (int mid = s; mid < e; mid++){
		Matrix m1 = memM[s][mid];
		Matrix m2 = memM[mid+1][e];
		int val = mem[s][mid] + mem[mid+1][e] + (m1.W * m1.R * m2.W);
		
		if (val < minval){
			minval = val;
			mem[s][e] = val;
			Matrix m; m.R = m1.R; m.W = m2.W;
			memM[s][e] = m;
		}
	}

}

int solve(Matrix M[], int N){

	for (int d = 0; d <= N; d++)
		for (int s = 0; s + d <= N; s++)
		{
			eval(M, s, s + d);
		}

	return mem[0][N];
}

int main() {

	cin >> N;
	rep(i, N){
		Matrix m;
		cin >> m.R;
		cin >> m.W;
		M[i] = m;
	}

	cout << solve(M, N) << endl;

#ifdef DEBUGRUN
	rep (i, N){
		rep(j, N){
			cerr << mem[i][j] << ' ';
		}
		cerr << endl;
	}
#endif

}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
 
 
int main()
{
    int n;
    cin >> n;
    vector<int> p(n + 1);
    for(int i = 0; i <= n; i++){
        int a,b;
        cin >> a >> b;
        if(!i) {
         p[i] = a;
         i++;
        }
        p[i] = b;
    }
    
    vector<vector<int>> m(n + 1, vector<int>(n + 1, 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 MOD 1000000007

int main() {
  
 int n;
 cin >> n;

 vector<long long> p(n+1);
 for(int i = 1;i <= n;i++) {
   cin >> p.at(i-1) >> p.at(i);
 }

 vector<vector<long long> > dp(n+1,vector<long long>(n+1,0));

 for(int l = 2;l <= n;l++) {
   for(int i = 1;i <= n-l+1;i++) {
     int j = i + l - 1;
     dp[i][j] = (1<<21);
     for(int k = i;k <= j-1;k++) {
       dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i-1] * p[k] * p[j]);
     }
   }
 }

 cout << dp[1][n] << endl;
 
 
  
}

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

void fnInput(vector<int>& rvnMtrx)
{
  int nMaxSiz;
  cin >> nMaxSiz;
  rvnMtrx.resize(nMaxSiz + 1);

  for (int i = 0; i < nMaxSiz; ++i)
    cin >> rvnMtrx[i] >> rvnMtrx[i + 1];
}

int fnMtrxChainMultpl(vector<vector<int>>& rvvnDetDP, const vector<int>& cnrvnMtrx)
{
  const int nMAXSIZ = cnrvnMtrx.size() - 1;
  for (int i = 1; i <= nMAXSIZ; ++i)  rvvnDetDP[i][i] = 0;

  for (int nDif = 1; nDif <= nMAXSIZ - 1; ++nDif)
    for (int nBgn = 1; nBgn <= nMAXSIZ - nDif; ++nBgn)
    {
      int nEnd = nBgn + nDif;
      for (int nx = nBgn; nx <= nEnd - 1; ++nx)
        rvvnDetDP[nBgn][nEnd] =
          min(rvvnDetDP[nBgn][nEnd],
              rvvnDetDP[nBgn][nx] + rvvnDetDP[nx + 1][nEnd] 
                                  + cnrvnMtrx[nBgn - 1] * cnrvnMtrx[nx] * cnrvnMtrx[nEnd]);
    }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnDetDP; 
  vector<int> vnMtrx;

  fnInput(vnMtrx);
  vvnDetDP.resize(vnMtrx.size(), vector<int>(vnMtrx.size(), INT_MAX));

  fnMtrxChainMultpl(vvnDetDP, vnMtrx);
  cout << vvnDetDP[1][vnMtrx.size() - 1] << endl;
  return 0;
}

C++: #include<iostream>

using namespace std;

void MATRIX_CHAIN_ORDER(int n, int p[]){
    int m[n+1][n+1];
    int s[n][n+1];

    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] = 2000000000;
            for(int k = i; k <= j-1; k++){
                int q = m[i][k] + m[k+1][j] + (p[i-1] * p[k] * p[j]);
                if(q < m[i][j]){
                    m[i][j] = q;
                    s[i][j] = k;
                }
            }
        }
    }
    cout << m[1][n] << endl;
}

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

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

ll dp[105][105]; // 区間[i,j)の連鎖行列積の最小回数

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  ll n; cin>>n;
  vll v(n+1);
  rep(i,n){
    cin>>v[i]>>v[i+1];
  }
  
  if(n==1){
    cout<<0<<endl;
    return 0;
  }
  
  for(ll l=2; l<=n; l++){
    for(ll i=0; i<n-l+1; i++){
      ll j=i+l;
      // [j,j+i)を更新する
      dp[i][j]=LINF;
      for(ll k=i+1; k<j; k++){
        chmin(dp[i][j],dp[i][k]+dp[k][j]+v[i]*v[k]*v[j]);
      }
    }
  }
  
  cout<<dp[0][n]<<endl;
}