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

int main()
{
	int n;
	int v[101];
	long m[100][100];
	const long nil = 999999999;

	cin >> n;

	for (int i = 0; i < n; i++)
		cin >> v[i] >> v[i + 1];

	for (int i = 0; i < 100; i++)
		for (int j = 0; j < 100; j++)
			if (i == j)
				m[i][j] = 0;
			else
				m[i][j] = nil;
	
	for(int i = n - 2; i > -1; i--)
		for(int j = i + 1; j < n; j++)
			for (int k = i; k < n; k++)
				m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + v[i] * v[k + 1] * v[j + 1]);

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

	return 0;
}

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

struct mat {
	ll l, c, r;
};

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

	mat a[110][110];
	int N; cin >> N;
	ll l, r;
	for (int i = 0; i < N; i++) {
		cin >> l >> r;
		a[i][i].l = l;
		a[i][i].r = r;
		a[i][i].c = 0;
	}
	for (int l = 1; l < N;l++) {
		for (int i = 0; i+l < N; i++) {
			ll m = 1000010;
			a[i][i + l].l = a[i][i].l;
			a[i][i + l].r = a[i + l][i + l].r;
			for (int j = i; j < i + l; j++) {
				m = min(a[i][j].l * a[i][j].r * a[j + 1][i + l].r + a[i][j].c + a[j + 1][i + l].c, m);
			}
			a[i][i + l].c = m;
		}
	}
	cout << a[0][N-1].c << endl;
}

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

static const int N = 1000;

int main()
{
    int n, p[N + 1];
    vector<vector<int>> m(N + 1, vector<int>(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.at(i).at(i) = 0;
    }
    for (int l = 2; l <= n; l++)
    {
        for (int i = 1; i <= n - 1; i++)
        {
            int j = i + l - 1;
            m.at(i).at(j) = (1 << 21);
            for (int k = i; k <= j - 1; k++)
            {
                m.at(i).at(j) = min(m.at(i).at(j), m.at(i).at(k) + m.at(k+1).at(j) + p[i - 1] * p[k] * p[j]);
            }
        }
    }
    cout << m[1][n] << endl;
    return 0;
}

C++: /**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int r[45] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);


int f(int n, int k, int m)
{
	return f(n,k) + f(k+1,m) + (r[n] * r[k+1] * r[m+1]);
}

int f(int n, int m)
{
	if (n==m) return 0;
	int & ra = a[n][m];
	if (ra) return ra;
	ra = INT_MAX;
	for (int k=n;k<m;k++)
	{
		int ans = f(n,k,m);
		if (ans < ra) ra = ans;
	}
	return ra;
}

int main()
{
	int rr = 0;
	int cc = 0;
	int n = 0;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cin >> rr >> cc;
		r[i]=rr;
		r[i+1]=cc;
	}
	for (int i=0;i<n-1;i++)
	{
		a[i][i+1] = r[i] * r[i+1] * r[i+2];
	}
	int ans = f(0,n-1);
	cout << ans << endl;
}

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

#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    //A*Bの行列とB*Cの行列、計A*B*C回の乗算をしてA*Cの行列になる
    int N; cin >> N;
    int R[N], C[N]; rep(i, N) cin >> R[i] >> C[i];
    int dp[N+1][N+1];
    rep(i, N+1) rep(j, N+1) dp[i][j] = INF;
    rep(i, N+1) dp[i][i+1] = 0;
    for(int haba = 2; haba <= N; haba++){
        for(int left = 0; left < N; left++){
            int right = left + haba;
            if(right > N) break;
            for(int boarder = left+1; boarder <= right-1; boarder++){
                dp[left][right] = min(dp[left][right], dp[left][boarder] + dp[boarder][right] + R[left]*C[boarder-1]*C[right-1]);
            }
        }
    }

    cout << dp[0][N] << endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define INF (1 << 21)
int main(){

  //-------データの入力------------
  int n;
  cin >> n;
  int row[n];
  int column[n];
  for(int i = 0; i < n; i++){
    cin >> row[i] >> column[i];
  }
  //-----------------------------

  //[始点][終点（始点+幅）]の最小コスト
  int dp[n+1][n+1];

  //INFで初期化
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      dp[i][j] = INF;
    }
  }

  //幅0はコスト0
  //dp[i][i+0] = 0
  for(int i = 0; i < n; i++){
    dp[i][i] = 0;
  }

  //幅:j = 0 から探索開始
  for(int j = 0; j <= n; j++){
    //始点: i
    for(int i = 0; i < n; i++){
      //仕切りを置く位置: k
      // (M_0, M_1, M_2, M_3) | (M_4, M_5)
      for(int k = i + 1; k <= i + j; k++){
        //現在のdp[i][i+j]より小さければ更新
        dp[i][i+j] = min(dp[i][i+j], dp[i][k-1] + dp[k][i+j] + row[i] * column[k-1] * column[i+j]);
      }
    }
  }
  /*
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  //始点０、幅n-1の最小コスト=> n個の行列の連鎖の計算に最低限必要な乗算の回数　を出力
  cout << dp[0][n-1] << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int n;
struct rcdata {
    int rr; int c;
};
vector<rcdata> m(110);
long long dp[110][110];

long long rec(int l, int r) {
    // cout << "l " << l << " r " << r << endl;
    if(dp[l][r] != 1e9) return dp[l][r];
    if(abs(l-r) <= 1) return dp[l][r] = 0;
    for(int mid = l+1; mid <= r-1; ++mid) {
        long long total = rec(l, mid)+rec(mid,r)+(m.at(l).rr*m.at(mid).rr*m.at(r-1).c);
        chmin(dp[l][r], total);
    }
    return dp[l][r];
}

int main() {
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> m.at(i).rr >> m.at(i).c;
    }

    for(int i = 0; i <= n; ++i) {
        for(int j = 0; j <= n; ++j) {
            dp[i][j] = 1e9;
        }
    }

    cout << rec(0, n) << endl;
}

C++: /**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int r[45] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);


int f(int n, int k, int m)
{
	return f(n,k) + f(k+1,m) + (r[n] * r[k+1] * r[m+1]);
}

int f(int n, int m)
{
	if (n==m) return 0;
	int & ra = a[n][m];
	if (ra) return ra;
	ra = INT_MAX;
	for (int k=n;k<m;k++)
	{
		int ans = f(n,k,m);
		if (ans < ra) ra = ans;
	}
	return ra;
}

int main()
{
	int rr = 0;
	int cc = 0;
	int n = 0;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cin >> rr >> cc;
		r[i]=rr;
		r[i+1]=cc;
	}
	for (int i=0;i<n-1;i++)
	{
		a[i][i+1] = r[i] * r[i+1] * r[i+2];
	}
	int ans = f(0,n-1);
	cout << ans << endl;
}