Go: package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)

	P := make([]int, n+1)
	for i := 1; i <= n; i++ {
		l := nextInt(sc)
		P[i-1] = l
		m := nextInt(sc)
		P[i] = m
	}

	matrix := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		matrix[i] = make([]int, n+1)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n - l + 1; i++ {
			j := i + l - 1
			matrix[i][j] = (1 << 21)
			for k := i; k <= j-1; k++ {
				matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k+1][j] + P[i-1] * P[k] * P[j])
			}
		}
	}
	fmt.Println(matrix[1][n])
}

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

C++: #include<bits/stdc++.h>
#define INF 1e9

using namespace std;

int main()
{
  int n;
  int a[105];
  
  cin >> n;

  for(int i=0;i < n;i++){
    cin >> a[i] >> a[i+1];
  }
  //dp[i][j] := iからj番目の行列積に必要なスカラー積の最小値
  int dp[105][105] = {};

  //init
  for(int i=0;i < n;i++){
    for(int j=0;j < n;j++){
      if(i == j) dp[i][j] = 0;//iからiまでは当然 0回
      else dp[i][j] = INF;
    }
  }
  
  for(int m = 0; m < n ;m++){
    for(int i = 0; m+i < n ;i++){
      int j = m+i;
      for(int k = i; k < j ;k++){
	dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + a[i]*a[k+1]*a[j+1]);
      }
    }
  }
  cout << dp[0][n-1] << endl;
  return 0;
}

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

int main() {
	int n, p[101], m[101][101];
	std::cin >> n;
	for (int i = 1; i <= n; i++) {
		std::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] = std::min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}
	std::cout << m[1][n] << std::endl;
	return 0;
}