Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&a[i+1])
	}

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

	for d := 2; d <= n; d++ {
		for s := 0; s+d <= n; s++ {
			min := 1 << 30
			for j := s+1; j < s+d; j++ {
				val := m[s][j] + m[j][s+d] + a[s]*a[j]*a[s+d]
				if val < min {
					min = val
				}
			}
			m[s][s+d] = min
		}
	}
	fmt.Println(m[0][n])
}

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

const int MAX = 1 << 30;

struct Matrix {
  Matrix(int r,int c){
    row = r;
    col = c;
  }
  int row,col;
};

int dp[100][100];

int rec(int left, int right, vector<Matrix>& M)
{
  if(left==right){ return 0; }
  
  if(dp[left][right]<MAX){ return dp[left][right]; }
  
  if(right-left==1){
    dp[left][right] = M[left].row * M[left].col * M[right].col;
    return dp[left][right];
  }
  
  int answer = MAX;
  for(int mid=left;mid<right;mid++){
    //////////////
    //int a = rec(left,mid,M)+rec(mid+1,right,M) + M[mid].row * M[mid].col * M[mid+1].col;
    //cout << "left:" << left << ",mid:" << mid << ",right:" << right << ",answer:" << a << endl;
    //////////////
    answer = min(answer,rec(left,mid,M)+rec(mid+1,right,M) + M[left].row * M[mid].col * M[right].col);
  }
  
  dp[left][right] = answer;
  return dp[left][right];
}


int main(void){
    // Your code here!
    int n;
    cin >> n;
    
    if(n==1){
      cout << 0 << endl;
      return 0;
    }
    
    vector<Matrix> M;
    for(int i=0;i<n;i++){
      int row,col;
      cin >> row >> col;
      M.push_back(Matrix(row,col));
    }
    
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        dp[i][j] = MAX;
      }
    }
    
    rec(0,n-1,M);

    cout << dp[0][n-1] << 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++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep0(n) for (int i = 0; i < (int)(n); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define from1(obj) obj.begin() + 1, obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbp(p) cerr << #p << ":(" << p.first << ", " << p.second << ") "
#define dbpl(p) cerr << #p << ":(" << p.first << ", " << p.second << ")\n"
#define dbt3(t3) cerr << #t3 << ":(" << get<0>(t3) << ", " << get<1>(t3) << ", " << get<2>(t3) << ") "
#define dbt3l(t3) cerr << #t3 << ":(" << get<0>(t3) << ", " << get<1>(t3) << ", " << get<2>(t3) << ")\n"
#define dbt4(t4) cerr << #t4 << ":(" << get<0>(t4) << ", " << get<1>(t4) << ", " << get<2>(t4) << ", " << get<3>(t4) << ") "
#define dbt4l(t4) cerr << #t4 << ":(" << get<0>(t4) << ", " << get<1>(t4) << ", " << get<2>(t4) << ", " << get<3>(t4) << ")\n"
#define dbv(vec) cerr << #vec << ":{ "; for (auto e : vec) cerr << e << ", "; cerr << "}\n"
#define dbvp(vec) cerr << #vec << ":{ "; for (auto p : vec) cerr << "(" << p.first << ", " << p.second << "), "; cerr << "}\n"
#define dbvv(vv) cerr << #vv << ":{\n"; for (auto vec : vv) { cerr << "{ "; for (auto e : vec) cerr << e << ", "; cerr << "}\n"; } cerr << "}\n"
#define dbm(map) cerr << #map << ":{ "; for (auto p : map) cerr << "(" << p.first << ", " << p.second << "), "; cerr << "}\n"
#define YN(f) cout << (f ? "YES" : "NO") << "\n"
#define Yn(f) cout << (f ? "Yes" : "No") << "\n"
#define yn(f) cout << (f ? "yes" : "no") << "\n"
using ll = long long;
using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vb = vector<bool>;
using vvi = vector<vi>; using vvl = vector<vl>; using vvb = vector<vb>;
using pii = pair<int, int>; using pil = pair<int, ll> ; using pll = pair<ll, ll>; using pli = pair<ll, int>;
using vpii = vector<pii>; using vpll = vector<pll>;
using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;
using ti4 = tuple<int, int, int, int>; using tl4 = tuple<ll, ll, ll, ll>;
using mii = map<int, int>; using mil = map<int, ll>; using mll = map<ll, ll>; using mli = map<ll, int>;

const int INF = 1e8;
vi r, c;
vvi dp; // dp[i][j]:Ai~Ajのスカラー乗算の最小回数

int rec(int i, int j) {
	if (dp[i][j] != INF) return dp[i][j];
	int res = INF;
	rep2(k, i, j)	res = min(res, rec(i, k) + rec(k + 1, j) + r[i] * c[k] * c[j]);
	return dp[i][j] = res;
}

int main() {	
	// 行列の番号は0-ind, 0 ~ n-1
	int n; cin >> n;
	r.assign(n, 0); c.assign(n, 0);
	rep(i, n) cin >> r[i] >> c[i];
	dp.assign(n, {}); rep(i, n) dp[i].assign(n, INF);
	rep(i, n) rep(j, i + 1) dp[i][j] = 0;
	rep(i, n - 1) dp[i][i + 1] = r[i] * c[i] * c[i + 1];
	// dbvv(dp);
	rec(0, n - 1);
	// dbvv(dp);
	cout << dp[0][n - 1] << endl;
}

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<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 <stdio.h>
//#include <string.h>

using namespace std;

struct Matrix{
    int row;
    int col;
    int total;
};

Matrix mts[100][100];
int n;

int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; ++i){
        int r,c;
        scanf("%d %d", &r, &c);
        mts[0][i].row = r;
        mts[0][i].col = c;
        mts[0][i].total = 0;
    }

    for(int i=1; i<n; ++i){
        for(int j=0; j<n-i; ++j){
            int min = 0xfffffff;
            mts[i][j].row = mts[i-1][j].row;
            mts[i][j].col = mts[0][i+j].col;
            for(int k=0; k<i; ++k){
                int tmp = mts[k][j].total + mts[i-k-1][k+j+1].total + mts[k][j].row * mts[i-k-1][k+j+1].row * mts[i-k-1][k+j+1].col;
                min = min < tmp ? min : tmp;
            }
            mts[i][j].total = min;
        }
    }

    cout << mts[n-1][0].total << endl;

    return 0;
}