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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(n) begin(n),end(n)


const int INF = 1e9;
int main() {
    int n;
    cin >> n;
    vector<int> p(n+1);
    int dp[105][105];
    rep(i,n) cin >> p[i] >> p[i+1];
    rep(i,n){
        rep(j,n) dp[i][j] = INF;
    }
    int mn;
    for(int i = 0; i < n; ++i) dp[i][i] = 0;
    for(int l = 2; l <= n; ++l){
        for(int i = 1; i + l -1 <= n; ++i){
            int j = i + l -1;
            for(int k = i; k < j; ++k){
                if(k==i) mn = dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j];
                else mn = min(mn, dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j]);
            }
            dp[i][j] = mn;
        }
    }
    cout << dp[1][n] << endl;
    return 0;
}

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