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<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++: //matrix chain multiplication
#include<iostream>
#include<cstdio>
#include<cstdlib> //malloc
using namespace std;
#define INF 1000000001
int DP[100][100];
int *p;
int rec(int r, int c) { //what to record on DP, r番目の行列からc番目の行列まで掛け算
    int Min = INF;
    for (int i = 0; i < c - r; i++) { //どこで区切るか
        Min = min(Min, DP[r][r+i] + DP[r+i+1][c] + p[r-1]*p[r+i]*p[c]);
        //3項目は2つのDPをつなぐ計算量
    }
    return Min;
}
int main() {
    int N;
    cin >> N;
    for (int i = 1; i <= N; i++) { //matrix starts index 1, p[] starts index 0
        for (int j = 1; j <= N; j++) {
            DP[i][j] = 0;
        }
    }
    p = (int *)malloc(sizeof(int) *(N+1) ); //main()外でp使うから面倒だけど
    //N+1 -> 0 start, 最後の行列の列 p[N]
    for (int i = 0; i < N; i++) {
        cin >> p[i]; cin >> p[i+1]; //start p[2]
    }
    for (int i = 1; i < N; i++) DP[i][i+1] = p[i-1]*p[i]*p[i+1]; //2つの積
    
    int y;
    for (int k = 0; k <= N - 2; k++) {
        for (int x = 1; x <= N - 2; x++) {
            y = x+(2+k);
            if (x <= N && y <= N) DP[x][y] = rec(x, y);
        }
    }
    
    cout << DP[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++: //Matrix Chain Multiplication
#include <iostream>
#include <climits>

using namespace std;

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

int main() {
    int n, r, c;
    cin >> n;
    int p[n + 1];
    for(int i = 0; i < n; i++) {
        cin >> r >>  c;
        if(i == 0) p[0] = r;
        p[i + 1] = c;
    }

    cout << matrixChainMultiplication(p, n) << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
    int n,p[110],dp[101][101];
    cin >> n;
    for (int i = 1;i <= n;i++) {
        cin >> p[i - 1] >> p[i];
        dp[i][i] = 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<stdio.h>
#include<math.h>

static int N=100;

int main(){

  int n,p[N+1],m[N+1][N+1];
  int i,j,l,k;
  
  scanf("%d",&n);

  for(i=1;i<=n;i++){
    scanf("%d",&p[i-1]);
    scanf("%d",&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] = fmin(m[i][j],m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
      
	  }
    }
  }

  printf("%d",m[1][n]);
  printf("\n");

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1501001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

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

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

    int N;
    cin >> N;
    vector<int> R(N), C(N);
    for(int i = 0; i < N; ++i)  cin >> R[i] >> C[i];

    vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
    auto MemoRec = [&](auto&& self, int l, int r) -> int {
        if(dp[l][r] != -1)  return dp[l][r];
        if(r - l <= 1)  return dp[l][r] = 0;
        int res = INF;
        for(int m = l + 1; m < r; ++m){
            int S = self(self, l, m) + self(self, m, r) + R[l] * R[m] * C[r - 1];
            chmin(res, S);
        }
        return dp[l][r] = res;
    };

    cout << MemoRec(MemoRec, 0, N) << endl;
}

C++: #include <iostream>

using namespace std;

const int N = 102;
int dp[N][N], r[N], c[N];

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d %d", &r[i], &c[i]);
    }
    for (int k = 2; k <= n; k++)
    {
        for (int i = 0; i <= n - k; i++)
        {
            int mn = 2e9;
            for (int j = i; j < i + k - 1; j++)
            {
                mn = min(mn, dp[i][j] + dp[j+1][i+k-1] + r[i]*c[j]*c[i+k-1]);
            }
            dp[i][i+k-1] = mn;
        }
    }
    printf("%d\n", dp[0][n-1]);
    return 0;
}