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])
}

Python: # -*- coding: utf-8 -*-


if __name__ == '__main__':

    n = int(input())
    p = [0]
    for _ in range(n):
        r, c = list(map(int, input().split(" ")))
        p[-1] = r
        p.append(c)
    m = [[0 for i in range(n+1)] for j in range(n+1)]
    
    for i in range(1, n+1):
        m[i][i] = 0
    
    for l in range(2, n+1):
        for i in range(1, n-l+2):
            j = i + l - 1
            m[i][j] = float("inf")
            for k in range(i, j):
                m[i][j] = min(m[i][j], m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j])

    print(m[1][n])

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int dp[111][111],n,L[111],R[111];
int f(int l,int r){
	if(dp[l][r]>-1)return dp[l][r];
	int res=1e9;
	for(int i=l;i<r;i++){
		res=min(res,f(l,i)+f(i+1,r)+L[l]*R[i]*R[r]);
	}
	return dp[l][r]=res;
}
main(){
	memset(dp,-1,sizeof(dp));
	r(i,111)dp[i][i]=0;
	cin>>n;
	r(i,n)cin>>L[i]>>R[i];
	cout<<f(0,n-1)<<endl;
}

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

struct Matrix
{
    int r;
    int c;
};
int Multiple(std::list<Matrix>::iterator Left)
{
    std::list<Matrix>::iterator Right = Left; Right++;
    int Count = Left->r * Right->c * Left->c;
    Right->r = Left->r;
    return Count;
}
bool CompareR(const Matrix& Left, const Matrix& Right)
{
    return Left.r < Right.r;
}
bool CompareC(const Matrix& Left, const Matrix& Right)
{
    return Left.c < Right.c;
}
int Mcm(const std::list<Matrix>& AList)
{
    std::list<Matrix> List = AList;
    const std::list<Matrix>::iterator MinR = std::min_element(List.begin(), List.end(), CompareR);
    const std::list<Matrix>::iterator MinC = std::min_element(List.begin(), List.end(), CompareC);
    int Count = 0;
    for(std::list<Matrix>::iterator It = MinC; It != List.begin(); It){
        --It;
        Count += Multiple(It);
        It = List.erase(It);
    }
    for(std::list<Matrix>::iterator It = MinR; It != --List.end(); It){
        Count += Multiple(It);
        It = List.erase(It);
    }
    Count += Multiple(List.begin());
    return Count;
}

int main()
{
    std::list<Matrix> List;
    int n;
    std::cin >> n;
    for(int i = 0; i < n; ++i){
        Matrix m;
        std::cin >> m.r >> m.c;
        List.push_back(m);
    }
    
    std::cout << Mcm(List) << std::endl;
    return 0;
}

C++: /*
    Problem 46
    https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_10_B
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* class */
class Matrix {
public:
    int r, c;
    Matrix () {}
    Matrix (int r, int c) : r(r), c(c) {}
};
/* global variables */
/* function */
/* main */
int main(){
    int n;
    cin >> n;
    vector<Matrix> M(n, Matrix());
    for (int i = 0; i < n; i++) {
        int r, c;
        cin >> r >> c;
        Matrix m = Matrix(r, c);
        M[i] = m;
    }

    // init dp
    vector<vector<int>> dp(n, vector<int>(n));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i == j) dp[i][j] = 0;
            else dp[i][j] = INF;
    // calc
    // ex) M0 * M1 * M2
    // w=1 , M0*M1, M1*M2
    // w=2 , M0*M1*M2 = min{M0*(M1*M2),  (M0*M1)*M2)}
    //   | 0 | 1 | 2
    // 0 | 0 |w=1|w=2
    // 1 |   | 0 |w=1
    // 2 |   |   | 0
    for (int w = 1; w < n; w++) {  // w+1個の連続したMatrixの積, w個の選択肢
        for (int i = 0; i < n-w; i++) {  // start位置 Mi * Mi+1 * ... * Mi+w
            int j = i + w;  // end位置 Mi * Mi+1 * ... * Mj
            for (int k = i; k < j; k++) {
                int cost = M[i].r * M[k].c * M[j].c;
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + cost);
            }
        }
    }

    cout << dp[0][n-1] << '\n';
}

C++: #include <stdio.h>
#include <algorithm>

using namespace std;


int main(){
	int n,minimum,tmp;
	scanf("%d",&n);
	int table[n][n];
	int data[n][2];

	for(int i=0;i<n;i++){
		for(int k=0;k<n;k++)table[i][k] = 0;
		scanf("%d %d",&data[i][0],&data[i][1]);
	}

	for(int i=n-2;i>=0;i--){
		for(int k=0;k<=i;k++){
			if(i==n-2){
				table[n-i-1][k] = data[k][0]*data[k][1]*data[k+1][1];
		}else{
				minimum = 2147483647;
				for(int p=0;p<=n-i-2;p++){
					tmp=(table[p][k]+table[n-i-p-2][k+p+1])+(data[k][0]*data[k+p][1]*data[k+n-i-1][1]);
					minimum = min(minimum,tmp);
				}
				table[n-i-1][k] = minimum;
			}
		}
	}

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

    return 0;
}

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

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