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

int *r, *c;

int main() {
  int n, i;

  scanf("%d", &n);

  r = (int *) malloc(sizeof(int)*n);
  c = (int *) malloc(sizeof(int)*n);

  for(i = 0; i < n; i++) {
    scanf("%d %d", &r[i], &c[i]);
  }

  printf("%d\n", cost(0, n-1));

  free(r);
  free(c);

  return 0;
}

int cost(int a, int b) {
  if (a == b) {
    return 0;
  } else {
    int total, minTotal = 1000000, m;

    for(m = a; m < b; m++) {
      total = cost(a, m) + cost(m+1, b) + r[a]*c[m]*c[b];
      if (total < minTotal)
        minTotal = total;
    }

    return minTotal;
  }
}

C++: #include<iostream>
#include<climits>
using namespace std;
int main(){
  int n,data[101]; cin>>n;
  for(int i=0;i<n;++i) cin>>data[i]>>data[i+1];
  int dp[100][100]={};
  for(int num=2;num<=n;++num){
    for(int first=0;first<=n-num;++first){
      int end=first+num-1;
      dp[first][end]=INT_MAX/2;
      for(int i=first;i<=end;++i){
	if(dp[first][end]>dp[first][i]+dp[i+1][end]+data[first]*data[i+1]*data[end+1])
	  dp[first][end]=dp[first][i]+dp[i+1][end]+data[first]*data[i+1]*data[end+1];
      }
    }
  }
  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<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 <bits/stdc++.h>
 
using namespace std;

using usize = ::std::size_t;
using u64 = ::std::uint_least64_t;
u64 N;
static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;
vector<u64> rows(110), columns(110);
vector<vector<u64>> dp(110, vector<u64> (110, -1));

u64 mcm(u64 i, u64 j){
    if (~dp[i][j]) {
        return dp[i][j];
    }
    dp[i][j] = Inf;
    if (i == j) {
        dp[i][j] = 0;
    } else {
        for (u64 k = i; k < j; k++) {
            dp[i][j] = min(dp[i][j], mcm(i, k) + mcm(k + 1, j) + rows[i] * columns[k] * columns[j]);
        }
    }
    return dp[i][j];
}

int main(int argc, char *argv[])
{
    
 
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin >> N;
    
    
    for (int i = 0; i < N; i++) {
        cin >> rows[i] >> columns[i];
    }
    
    cout << mcm(0, N - 1) << 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[100] = {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 INF 1000000000
typedef long long ll;
using namespace std;

int main()
{
	ll n;
	cin >> n;
	vector<ll> M(n + 1);
	cin >> M[0];
	for (int i = 1; i < n; i++) {
		cin >> M[i] >> M[i];
	}
	cin >> M[n];
	vector<vector<ll>> dp(n, vector<ll>(n, INF)), mon(n, vector<ll>(n));
	for (int i = 0; i < n; i++) {
		dp[i][i] = 0;
		mon[i][i] = i;
	}
	for (ll k = 1; k < n; k++) {
		for (ll i = 0; i + k < n; i++) {
			for (ll j = mon[i][i + k - 1]; j <= min(mon[i + 1][i + k], i + k - 1); j++) {
				ll cost = dp[i][j] + dp[j + 1][i + k] + M[i] * M[j + 1] * M[i + k + 1];
				if (cost < dp[i][i + k]) {
					dp[i][i + k] = cost;
					mon[i][i + k] = j;
				}
			}
		}
	}
	cout << dp[0][n - 1] << endl;
	return 0;
}