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[k+1][j] + p[i-1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#define INT_MAX 100000000
using namespace std;
int index;
int mm[1000][1000];
int mine = INT_MAX;

void solve(int p[]){
  int n = index-1,j,q;
  for(int i=1;i<=n;i++)mm[i][i]=0;
  for(int l=2;l<=n;l++){
    for(int i=1;i<=n-l+1;i++){
      j = i+l-1;
      mm[i][j] = INT_MAX;
      for(int k=i;k<=j-1;k++){
	q = mm[i][k] + mm[k+1][j] + p[i-1]*p[k]*p[j];
	if(q<mm[i][j]){
	  mm[i][j] = q;
       
	}

      }
    }
  }




}

int main(){
  int p[1000],a,b;
  scanf("%d",&index);
  //for(int i=0;i<=index;i++)for(int j=0;j<=n;j++)mm[i][j] = INT_MAX;
  for(int i=0;i<index;i++){
    scanf("%d %d",&a,&b);
    p[i] = a;
    if(i == index-1)p[i+1] = b;
  }

  index = index+1;
  solve(p);
 
  printf("%d\n",mm[2][index-2]);
  return 0;
}

C++: // http://algorithms.blog55.fc2.com/blog-entry-66.html

#include <iostream>
using namespace std;
#include <stdio.h>

#define MAX 100
#define INFTY (1 << 21)

int SIZE;
int R[MAX + 2];
int C[MAX+1][MAX+1]; // cost table
int B[MAX+1][MAX+1]; // best table

void compute(){
    for ( int i = 0; i <= SIZE; i++ )
    for ( int j = 0; j <= SIZE; j++ ) C[i][j] = INFTY;
    
    for ( int i = 0; i <= SIZE; i++ ) C[i][i] = 0;
    
    for ( int j = 1; j <= SIZE - 1; j++ ){
        for ( int i = 0; i < SIZE - j; i++ ){
            for ( int k = i; k < i + j; k++ ){
                // C[i][i+j] = min( C[i][i+j], 
                //                  C[i][k-1] + C[k][i+j] + R[i]*R[k]*R[i+j+1]);
                int cost = C[i][k] + C[k+1][i+j] + R[i]*R[k+1]*R[i+j+1];
                if ( cost < C[i][i+j] ){
                    C[i][i+j] =  cost;
                    B[i][i+j] = k;
                }
                
            }
        }
    }
}

void order( int i, int j ){
    if ( i == j ) cout << "M" << i;
    else{
        cout << "(";
        order( i, B[i][j]-1 ); order(B[i][j], j);
        cout << ")";
    }
}

void input(){
    cin >> SIZE;
	int z,i=0;
    for (; i < SIZE; i++ ) cin >> R[i]>>z;
	R[i]=z;
	//SIZE++;
}

void output(){
    cout <<  C[0][SIZE-1] << endl;
    //order(1, SIZE);
    //cout << endl;
}

main(){
    input();
    compute();
    output();
}

C++: // Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
#include <algorithm>
#include <limits.h>
using namespace std;

static const int N = 100;;

int main() {
	int n; cin >> n;
	int p[N + 1], m[N + 1][N + 1]; // m_ij is partial LCS between i to j.
	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++) { // l is a length of partial LCS.
		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[j] * p[k]);
			}
		}
	}
	cout << m[1][n] << endl;

	return 0;
}

C++: #include <iostream>

using namespace std;

static const int MAX = 100;

class MatrixChainMultiplication {
private:
    int MCM[MAX + 1][MAX + 1];
    int p[MAX + 1];
    int n;

    void set()
    {
        cin >> n;
        cin >> p[0];
        int _;
        for(int i = 1; i <= n; i++)
        {
            cin >> p[i];
            if(i == n)
                break;
            cin >> _;
        }
    }

public:
    MatrixChainMultiplication()
    {
        set();
    }

    int matrix_chain_multiplication()
    {
        for(int i = 1; i <= n; i++)
            MCM[i][i] = 0;

        for(int l = 2; l <= n; l++)
        {
            for(int i = 1; i <= n - l + 1; i++)
            {
                int j = i + l - 1;
                MCM[i][j] = (1 << 21);
                for(int k = i; k <= j - 1; k++)
                    MCM[i][j] =
                            min(MCM[i][j], MCM[i][k] + MCM[k + 1][j] + p[i - 1] * p[k] * p[j]);
            }
        }

        return MCM[1][n];
    }
};

int main()
{
    MatrixChainMultiplication mcm;
    cout << mcm.matrix_chain_multiplication() << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
//using ll = long long;
#define MAX 100000000000000;

/*
int MCM(int i, int j, int p[]) {
  int min = MAX;
  if (i == j) return 0;
  if (i < j) {
    int temp;
    for (int k = i; k < j; k++) {
      temp = MCM(i, k, p)+MCM(k+1, j, p)+p[i-1]*p[k]*p[j];
      if (min > temp) min = temp;
    }
  }
  return min;
}
*/

int main(void) {
  int n; cin >> n;
  int r[n]; int c[n];
  for (int i = 0; i < n; i++) {
    cin >> r[i] >> c[i];
  }

  long long m[n+1][n+1]; int p[n+1];
  for (int i = 0; i < n; i++) p[i] = r[i]; p[n] = c[n-1];
  for (int i = 1; i <= n; i++) m[i][i] = 0;
  //int ans = MCM(1, n, p);
  //cout << ans << endl;  
  for (int k = 1; k < n; k++) { //kはm[i][j]におけるj-iの値
    for (int i = 1; i+k <= n; i++) { //iはm[i][j]におけるi
    long long temp; m[i][i+k] = MAX; //tempmin_はm[i][i+k]の値
      for (int l = i; l < i+k; l++) { //lはm[i][i+k]を分割する地点
        temp = m[i][l]+m[l+1][i+k]+p[i-1]*p[l]*p[i+k];
        if (m[i][i+k] > temp) m[i][i+k] = temp;
      }
    }
  }
  cout << m[1][n] << endl;
}

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

const int N=100;

int main(){
	int n;
	long m[N+1][N+1], p[N+1];
	
	scanf(" %d", &n);
	for(int i=1; i<=n; ++i)
		scanf(" %ld %ld", &p[i-1], &p[i]);
		
	for(int i=0; i<=n; ++i) printf("p[%d]=%ld\n", i, p[i]);
		
	for(int i=1; i<=n; ++i) m[i][i]=0;
	
	for(int s=2; s<=n; ++s){ //s??????????????????
		for(int i=1; i+s-1<=n; ++i){ //?§????i
			int j=i+s-1; //??????j
			m[i][j]=100000000; //????????§???????????§?????????
			for(int k=i; k<=j-1; ++k){ //?????????k
				m[i][j]=min(m[i][j], m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
				//printf("go %d %d %d\n",i, j, k);
			}
		}
	}
	
	printf("%ld\n", m[1][n]);
}