Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(m[0][n-1])
}

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

C++: #include<iostream>
using namespace std;
int min(int a,int b){return a<b?a:b;}
int p[101],m[101][101];
int main()
{	int n;
	cin>>n;
	for(int i=1;i<=n;i++)//总共有n+1个有效数据
		cin>>p[i-1]>>p[i];//保证只有n个有效的数据重复的不管
	for(int i=1;i<=n;i++)
		m[i][i]=0;
	for(int l=2;l<=n;l++)//这个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++)//从i开始,对l开始累加
			{
				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;
}