Go: package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)

	P := make([]int, n+1)
	for i := 1; i <= n; i++ {
		l := nextInt(sc)
		P[i-1] = l
		m := nextInt(sc)
		P[i] = m
	}

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

	for l := 2; l <= n; l++ {
		for i := 1; i <= n - l + 1; i++ {
			j := i + l - 1
			matrix[i][j] = (1 << 21)
			for k := i; k <= j-1; k++ {
				matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k+1][j] + P[i-1] * P[k] * P[j])
			}
		}
	}
	fmt.Println(matrix[1][n])
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <cmath>
#include <iomanip>
#include <set>
#include <functional>

#define MOD (1000000007)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> tup;
const int MAX = 3000000;

long long fac[MAX], finv[MAX], inv[MAX];


ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}


// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

map<ll,ll> prime;//素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap

//O(√n)
//整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n){
    if(n<=1) return;
    ll l=sqrt(n);
   for(ll i=2;i<=(ll)(l);i++) { //
        if(n%i==0){
            prime_factorize(i);prime_factorize(ll(n/i));return;
        }
    }
    //mapでは存在しないkeyの場合も自動で構築される
    prime[n]++;return;
} 


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

    vector<pair<ll, ll>> v;

    for(ll i = 0; i < n; i++) {
        ll r, c;
        cin >> r >> c;
        v.push_back(make_pair(r,c));
    }

    vector<vector<ll>> dp(n+1, vector<ll>(n+1, 999999999999999999));
    for(ll i = 0; i < n; i++) {
        dp[i][i+1] = 0;
    }
    for(ll i = 0; i < n-1; i++) {
        dp[i][i+2] = v[i].first * v[i].second * v[i+1].second;
    }

    for(ll diff = 3; diff <= n; diff++) {
        //cout << diff << endl;
        for(ll i = 0; i + diff <= n; i++) {
            //cout << "*** [" << i <<", " << i+diff << ")" << endl;
            for(ll after = i+1; after <= i+diff-1; after++) {
                // cout << "-> [" << i << ", " << after << ") + [" << after << ", " << i+diff << ")" << endl;
                // cout <<  dp[i][after] + dp[after][i+diff] + v[i].first * v[after-1].second * v[i+diff-1].second << endl;
                dp[i][i+diff] = min(dp[i][i+diff], dp[i][after] + dp[after][i+diff] + v[i].first * v[after-1].second * v[i+diff-1].second);
                //cout << "dp[" << i << "][" << i+diff << "] = " << dp[i][i+diff] << endl;
            }
        }
    }

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

    return 0;    
}

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

int MCM(int molCol[], int matNum) {
	int DP_memory[100+1][100+1];

	//?????????????????????
	for (int i = 1; i <= matNum; i++)
		DP_memory[i][i] = 0;

	for (int l = 2; l <= matNum; l++) { //???????????¨????????????(i = 2??????2??????????????§????????????????°???????????±???????)
		for (int i = 1; i <= matNum - l + 1; i++) { //?????????????????§????°???????
			int j = i + l - 1; //??????????????§?????§??????
			DP_memory[i][j] = (1 << 30); //??????????????????????????§?????????(????°???????????±?????????????)
			for (int k = i; k < j; k++) //i??¨j????????§????????????????????????????????´???
				DP_memory[i][j] = DP_memory[i][j] < (DP_memory[i][k] + DP_memory[k + 1][j] + molCol[i - 1] * molCol[k] * molCol[j])
					? DP_memory[i][j] : (DP_memory[i][k] + DP_memory[k + 1][j] + molCol[i - 1] * molCol[k] * molCol[j]);
		}
	}

	return DP_memory[1][matNum];
}

int main() {
	int matNum;
	int molCol[200+1];
	cin >> matNum;
	for (int i = 1; i <= matNum; i++) {
		cin >> molCol[i - 1] >> molCol[i];
	}

	int answer = MCM(molCol, matNum);

	cout << answer << endl;
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#define INT_MAX 100000000
using namespace std;
int index;
int mm[1000][1000];
int mine = INT_MAX;
 
void solve(int p[]){
  int n = index-1,j,q;
  for(int i=1;i<=n;i++)mm[i][i]=0;
  for(int l=2;l<=n;l++){
    for(int i=1;i<=n-l+1;i++){
      j = i+l-1;
      mm[i][j] = INT_MAX;
      for(int k=i;k<=j-1;k++){
    q = mm[i][k] + mm[k+1][j] + p[i-1]*p[k]*p[j];
    if(q<mm[i][j]){
      mm[i][j] = q;
        
    }
 
      }
    }
  }
 
 
 
 
}
 
int main(){
  int p[1000],a,b;
  scanf("%d",&index);
  //for(int i=0;i<=index;i++)for(int j=0;j<=n;j++)mm[i][j] = INT_MAX;
  for(int i=0;i<index;i++){
    scanf("%d %d",&a,&b);
    p[i] = a;
    if(i == index-1)p[i+1] = b;
  }
 
  index = index+1;
  solve(p);
  
  printf("%d\n",mm[1][index-1]);
  return 0;
}

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

int n;
int m[101][101];
int p[101];

int main() {
    cin >> n;
    int count = n - 1, temp, i, j, k;

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

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

    for ( i = 1; i <= n-1; i++ ) {
        for ( j = 1; j <= n - i; j++ ) {
            temp = p[j - 1] * p[j] * p[j + i] + m[j + 1][j + i] + m[j][j];
            for ( k = j + 2; k <= i + j; k++ ) {
                temp = min(temp, p[j - 1] * p[k - 1] * p [j + i] + m[k][j + i] + m[j][k - 1]);
            }
            m[j][j + i] = temp;
        }
    }

    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 - 1 + 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++: #include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 29);

struct Matrix
{
  int r, c;
};

vector<Matrix> M;

int rec(vector<vector<int>> &dp, int top, int bottom)
{
  if (dp.at(top).at(bottom) != INF)
  {
    return dp.at(top).at(bottom);
  }
  if (top == bottom)
  {
    dp.at(top).at(bottom) = 0;
    return 0;
  }
  for (int mid = top; mid < bottom; mid++)
  {
    dp.at(top).at(bottom) = min(dp.at(top).at(bottom), rec(dp, top, mid) + rec(dp, mid + 1, bottom) + M.at(top).r * M.at(mid).c * M.at(bottom).c);
  }
  return dp.at(top).at(bottom);
}

int main()
{
  int n;
  cin >> n;
  M.resize(n);
  for (int i = 0; i < n; i++)
  {
    int r, c;
    cin >> r >> c;
    M.at(i) = Matrix{r, c};
  }
  vector<vector<int>> dp(n + 1, vector<int>(n + 1, INF));
  cout << rec(dp, 0, n - 1) << endl;
}