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<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[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>

using namespace std;

const int INF = 1 << 29;

int main()
{
	int n;
	cin >> n;

	int left[100], right[100];
	int m[100][100];

	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			m[i][j] = INF;

	for(int i = 0; i < n; i++)
	{
		cin >> left[i] >> right[i];
		m[i][i] = 0;
	}

	for(int l = 1; l < n; l++)
	{
		for(int i = 0; i < n - l; i++)
		{
			int j = i + l;
			for(int k = i; k < j; k++)
			{
				m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + left[i] * right[k] * right[j]);
			}
		}
	}

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

C++: #include <iostream>
#include <cstdlib>
#include <climits>

/*
 * http://ttrsq.exblog.jp/24318687/
 */

const int MAX_N = 100;
int DP[MAX_N + 1][MAX_N + 1] = {0};
int *p;

int best(int r, int c) {
  int min = INT_MAX;
  int num = c - r;

  for (int i = 0; i < num; i++) {
    min = std::min(min, DP[r][r + i] + DP[r + i + 1][c] + p[r - 1] * p[r + i] * p[c]);
  }
  
  return min;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int n = 0;
  std::cin >> n;
  
  p = (int *) std::malloc(sizeof(int) * n + 1);

  int bb = 0;

  std::cin >> p[0];
  std::cin >> p[1];
  
  for (int i = 2; i <= n; i++) {
    std::cin >> bb;
    std::cin >> p[i];
  }

  if (n == 1) {
    std::cout << "0" << std::endl;

    return 0;
  }  

  for (int i = 1; i < n; i++) {
    DP[i][i + 1] = p[i - 1] * p[i] * p[i + 1];
  }

  for (int j = 0; j <= n - 3; j++) {
    for (int i = j + 3; i <= n; i++) {
      DP[i - 2 - j][i] = best(i - 2 - j, i);
    }
  }

  std::cout << DP[1][n] << std::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[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;
}

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

#define REP(i, n) for(int i = 0; i < (n); i++)

typedef pair<int, int> pii;

const int N_SIZE = 100;

int dp[N_SIZE + 1][N_SIZE + 1];

void solve(int n, pii rc[])
{
  REP(i, n)
    dp[i][i] = rc[i].first * rc[i].second;
  REP(i, n - 1)
    dp[i][i + 1] = dp[i][i] * rc[i + 1].second;
  for(int i = 2; i < n; i++){
    for(int j = 0, k = i + j, m = 100000000; k < n; j++, k++){
      for(int l = j, x; l < k; l++){
	x = ((j == l) ? dp[j][l] * rc[k].second : dp[j][l]) + ((l + 1 == k) ? dp[l + 1][k] * rc[j].first : dp[l + 1][k]);
	if(j != l && l + 1 != k)
	  x += rc[j].first * rc[l].second * rc[k].second;
	m = min(m, x);
      }
      dp[j][k] = m;
      m = 100000000;
    }
  }
}
 
int main()
{
  int n;
  cin >> n;
  pii rc[n];
  REP(i, n)
    cin >> rc[i].first >> rc[i].second;
  solve(n, rc);
  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 <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <climits>

#define DEBUG

#ifdef DEBUG

#define printd printf

#else

#define printd(...)

#endif

int main() {
    int n;
    int shapes[100][2];

    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> shapes[i][0] >> shapes[i][1];
    }

    constexpr uint64_t INF = UINT64_MAX;
    uint64_t dp[100][100];
    for (int i = 0; i < 100; i++) {
        dp[i][i] = 0;
    }
    for (int width = 2; width <= n; width++) {
        for (int l = 0; l <= n - width; l++) {
            auto min = INF;
            for (int split = l + 1; split < l + width; split++) {
                auto times = dp[l][split - 1] + dp[split][l + width - 1] + shapes[l][0] * shapes[split][0] * shapes[l + width - 1][1];
                if (times < min) {
                    min = times;
                }
            }
            dp[l][l + width - 1] = min;
        }
    }
    std::cout << dp[0][n - 1] << std::endl;

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

vector<ll> r(101);
vector<ll> c(101);
vector<vector<ll>> DP(101,vector<ll> (101,-1));

ll inf=1e18;



ll dp(ll l,ll h) {
  if(DP[l][h]>-1) {
    return DP[l][h];
  }
  ll res=inf;
  if(l+1==h) {
    res=0;
  }
  else {
    for(ll i=l+1;i<h;i++) {
      res=min(res,dp(l,i)+dp(i,h)+r[i]*r[l]*c[h-1]);
    }
  }
  return DP[l][h]=res;
}

int main() {
  ll n;
  cin >> n;
  for(ll i=0;i<n;i++) {
    cin >> r[i] >> c[i];
  }
  cout << dp(0,n) << endl;
}

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

using namespace std;

static const int N = 100;
static int m[N + 1][N + 1] = {}, p[N + 1] = {};

static inline void matrixChainMultiplication(int n) {
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      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]); 
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  matrixChainMultiplication(n);
  
  cout << m[1][n] << endl;

  return 0;
}