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 <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;
}

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<string>
#include<algorithm>
#include<cstdio>
using namespace std;

static const int N = 100;



int main(void) {
	int n, p[N + 1], m[N + 1][N + 1];

	scanf("%d", &n);

	for (int i = 1; i <= n; i++) {
		scanf("%d%d", &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]);
			}
		}
	}

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

	return 0;
}

C++: #include <stdio.h>
  
#define INF 10000000
  
int main(){
  int n,i,j,k,l,t,m[101],c,o;
  int mm[101][101];
   
   
  scanf("%d",&n);
  for(i = 1 ; i < n+1; i++){
    scanf("%d%d",&m[i-1],&m[i]);
  }
   
for( i = 1 ; i <= n ; i++ ){
    mm[i][i] = 0;
  }  
   
  for(l = 2 ; l <n+1 ; l++){
    for(i=1;i<n-l+2;i++) {
        o=i+l-1;
    mm[i][o] = INF;
   
    for(k = i ; k < o ; k++){
t=mm[i][k] + mm[k+1][o] + m[i-1] * m[k] *m[o];
 
        if(t < mm[i][o])
          mm[i][o] = t;
        }
      }
    }
  printf("%d\n",mm[1][n]);
  return 0;
  }

C++: #include <algorithm>
#include <iostream>
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;
}

C++: #include<iostream>
#include<vector>
#define NIL -1
using namespace std;

struct M{
    int r;
    int c;
};

int calc(int s, int e, const vector<M>& M_set,
         vector<vector<int> >& MEMO) {
    if (s == e) return MEMO[s][e] = 0;
    if (MEMO[s][e] != NIL) return MEMO[s][e];
    int min = 1e9;
    for (int split = s; split < e; split++) {
        int val = calc(s, split, M_set, MEMO)
                  + calc(split + 1, e,M_set, MEMO)
                  + M_set[s].r * M_set[split].c * M_set[e].c;
        if (min > val) min = val;
    }
    return MEMO[s][e] = min;
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > MEMO(n, vector<int>(n, NIL));
    vector<M> M_set(n);
    for (int i = 0; i < n; i++) {
        cin >> M_set[i].r >> M_set[i].c;
    }
    cout << calc(0, n - 1, M_set, MEMO) << endl;
    return 0;
}