Go: package main

/*
連鎖行列積
*/

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type mem *[][]int

func main() {
	scanner := makeScanner(1024)
	n := eGetInt(scanner)
	p := make([]int, n+1)
	for i := 0; i < n; i++ {
		rowcol := eGetInts(scanner)
		p[i] = rowcol[0]
		p[i+1] = rowcol[1]
	}
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		m[i] = make([]int, n)
	}
	// 初期値の格納
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			if i == j {
				m[i][j] = 0 //  同じ行列へのコストは当然0
			} else {
				m[i][j] = math.MaxInt64
			}
		}
	}

	// num : 行列の数. num == 1の場合は初期値0を格納ずみ
	for num := 2; num <= n; num++ {
		// s : スタート行列番号
		for s := 0; s+num <= n; s++ {
			// t : 終了行列番号
			t := s + num - 1
			// k : 仕切
			for k := s; k < t; k++ {
				m[s][t] = min(m[s][t], m[s][k]+m[k+1][t]+p[s]*p[k+1]*p[t+1])
			}
		}
	}
	fmt.Println(m[0][n-1])
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}

// func min(a []int) int {
// 	m := a[0]
// 	for _, v := range a {
// 		if v < m {
// 			m = v
// 		}
// 	}
// 	return m
// }

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

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(){
	int i=0,j,k;
    for (; i <= SIZE; i++ )
    for (j = 0; j <= SIZE; j++ ) C[i][j] = INFTY;
    
    for (i = 0; i <= SIZE; i++ ) C[i][i] = 0;
    
    for (j = 1; j < SIZE; j++ ){
        for (i = 0; i < SIZE - j; i++ ){
            for (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++: #include <iostream>
#include <algorithm>
using namespace std;

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 ( 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>
#define NMAX 100
#define MAX 100000000
using namespace std;

struct Matrix{
  long long r;
  long long c;
};

Matrix m[NMAX];
int n;

long long minmulti_[NMAX][NMAX];

long long minmulti(int begin,int end){
  if(begin==end){return 0;}
  if(begin+1==end){return m[begin].r*m[begin].c*m[end].c;}
  if(minmulti_[begin][end]!=0) return minmulti_[begin][end];
  long long min=MAX;
  long long num;
  for(int i=begin;i<end;i++){
     num = minmulti(begin,i) + minmulti(i+1,end);
     num += m[begin].r*m[i].c*m[end].c;
     if(num<min){min=num;}
  }
  minmulti_[begin][end]=min;
  return min;
}

int main(){
  for(int i=0;i<NMAX;i++){
    for(int j=0;j<NMAX;j++){
      minmulti_[i][j]=0;
    }
  }
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>m[i].r>>m[i].c;
  }
  cout<<minmulti(0,n-1)<<endl;
}

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

int main()
{
    int n;
    cin >> n;
    long long p[n + 1];
    for (int i = 0; i < n; i++)
    {
        cin >> p[i] >> p[i + 1];
    }
    long long m[n + 1][n + 1];
    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; 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<climits>
#include<utility>
#include<algorithm>
#include<cstring>

using namespace std;

long long calc_min_cost(pair<int, int>*, int);

int main() {
	int n,mini;
	cin >> n;
	pair<int, int>* mat = new pair<int, int>[n];
	for (int i = 0;i < n;i++) {
		cin >> mat[i].first >> mat[i].second;
	}
	if (n == 1) {
		cout << "0\n";
		return 0;
	}
	int** first_last = new int* [n];
	for (int i = 0;i < n;i++) {
		first_last[i] = new int[n - i];
		first_last[i][0] = 0;
	}
	for (int i = 1;i < n;i++) {
		for (int j = 0;j < n - i;j++) {
			mini = INT_MAX;
			for (int k = 0;k < i;k++) {
				mini = min(mini, first_last[j][k] + first_last[j + k + 1][i - k - 1] + mat[j].first * mat[j + k].second * mat[j + i].second);
			}
			first_last[j][i] = mini;
		}
	}
	cout << first_last[0][n - 1] << endl;
}

long long calc_min_cost(pair<int, int>* mat, int n) {
	pair<int, int> *tmp = new pair<int,int>[n];
	long long cost=-1, mini = INT_MAX;
	if (n == 2) {
		return mat[0].first * mat[0].second * mat[1].second;
	}
	for (int i = 0;i < n-1;i++) {
			cost = (long long)(mat[i].first) * mat[i].second * mat[i+1].second;
			memcpy(tmp, mat, sizeof(pair<int,int>) * n);
			tmp[i].second = tmp[i+1].second;
			for (int j = i+1;j < n-1;j++) {
				tmp[j] = tmp[j + 1];
			}
			cost += calc_min_cost(tmp, n - 1);
			mini = min(mini, cost);
	}
	free(tmp);
	return mini;
}