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++: //全くわからんので解き方を見てみる
//行列dpを作って、dp[i][j]はi~jのかけ算をした際の最小の回数
//これを順に計算すれば確かに最小のものは出そう

#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

int main(){
  int INF=1000000000;
  int n;cin >> n;
  vector< vector<int> > dp(n,vector<int>(n,INF));
  //vector< vector< pair<int,int> > > x(n,vector< pair<int,int> >(n,make_pair(0,0)));
  vector< pair<int,int> > m(n);
  for(int i=0;i<n;i++){
    int r,c;cin >> r >> c;
    m[i]=make_pair(r,c);
  }

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

  for(int l=0;l<n;l++){//長さのループ
    for(int i=0;i<n-l;i++){//行のループ
      if(l==0){dp[i][i+l]=0;}
      for(int j=i;j<i+l;j++){//更新のループ(d[i][i+l])
        dp[i][i+l]=min({dp[i][i+l],dp[i][j]+dp[j+1][i+l]+m[i].first*m[j].second*m[i+l].second});
      }
    }

  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  cout<< dp[0][n-1] << endl;

}

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

#define vec                vector<int>
#define vecll              vector<ll>
#define vecd               vector<double>
#define vecst              vector<string>
#define vecb               vector<bool>
#define vec2(var, n, m)    vector<vector<int>>  var(n, vector<int>(m, 0))
#define vecb2(var, n, m)   vector<vector<bool>> var(n, vector<bool>(m, false))
#define vecll2(var, n, m)  vector<vector<ll>>   var(n, vector<ll>(m, INF))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var)       accumulate(all(var), 0)
#define f_sumll(var)     accumulate(all(var), 0LL)
#define chmin(v1, v2)    v1 = min(v1, v2)
#define chmax(v1, v2)    v1 = max(v1, v2)
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2)     cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
void prtok(bool ok){prt(ok ? "YES" : "NO");}
//----------------------------------------------------------------

int main(void) {
  int n;
  cin >> n;
  vecll r(n), c(n);
  rep(i,n){
    cin >> r[i] >> c[i];
  }

  ll INF = 1e18;
  vecll2(dp, n, n);

  rep(i,n){
    dp[i][i]=0;
  }

  REP(num,2,n+1){
    rep(i,n-num+1){
      REP(k,i,i+num-1){
        ll tmp = dp[i][k] + dp[k+1][i+num-1] + r[i]*c[k]*c[i+num-1];
        chmin(dp[i][i+num-1], tmp);
      }
    }
  }

  prt(dp[0][n-1]);
}

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

int N ;
int a[101][101];
int m[101][2];

int main() {
    cin >> N;
    for (int i = 1; i <= N; ++i) {
        cin >> m[i][0] >> m[i][1];
    }

    for (int j = 1; j <= N; ++j) {
        a[j][j] = 0;
    }

    for (int k = 1; k < N; ++k) {
        for (int l = 1; l <= N- k ; ++l) {
            int j = l + k;
            int mn = INT_MAX;
            for (int p = l; p <= j-1 ; ++p) {
                mn = min(mn, a[l][p] + a[p+1][j] + m[l][0] * m[p][1] * m[j][1]);
            }
            a[l][j] = mn;
        }
    }
    cout << a[1][N] << endl;

    return 0;
}

C++: #include <stdio.h>
const int N=110;
const int INF=1<<30;
int min(int a,int b){
    return a>b?b:a;
}
int main(){
    int n,dp[N][N],a[N];
    scanf("%d%d%d",&n,&a[0],&a[1]);
    for(int i=1;i<n;i++)scanf("%d%d",&a[i],&a[i+1]);
    for(int k=1;k<n;k++){
        for(int i=0;i<n-k;i++){
            dp[i][i+k]=INF;
            for(int j=i;j<i+k;j++)dp[i][i+k]=min(dp[i][i+k],dp[i][j]+dp[j+1][i+k]+a[i]*a[j+1]*a[i+k+1]);
        }
    }
    printf("%d\n",dp[0][n-1]);
}

C++: #include<iostream>
#include<algorithm>

using namespace std;

int main  () {

    int i, j, k, l, n, p[101], m[101][101];

    cin >> n;

    for (i = 1; i <= n; i++) {
        cin >> p[i - 1] >> 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] = 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 <stdio.h>
#include <algorithm>
using namespace std;
int n, r[100], c[100], dp[100][101];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d%d", &r[i], &c[i]), dp[i][i] = 0;
	for(int i = 1; i < n; i++) {
		for(int j = 0; i + j <= n; j++) {
			dp[j][i + j] = min(dp[j][i + j - 1], dp[j + 1][i + j]) + c[j] * c[i + j - 1];
		}
	}
	printf("%d\n", dp[0][n]);
	return 0;
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define INF 1020304050

int main()
{
  int n;cin>>n;
  vector<int> r(n);
  vector<int> c(n);
  int i,j,k;
  rep(i,n)cin>>r[i]>>c[i];
  vector<vector<int>> dp(n,vector<int>(n,INF));
  rep(i,n)dp[i][i]=0;
  rep(i,n-1)dp[i][i+1]=r[i]*c[i]*c[i+1];
  for(i=2;i<n;i++)
  {
    rep(j,n-i)
    {
      for(k=1;k<=i;k++)
      {
        dp[j][j+i]=min(dp[j][j+i],dp[j][j+k-1]+dp[j+k][j+i]+r[j]*r[j+k]*c[j+i]);
      }
    }
  }
  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; 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;
}