Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	p := make([]int, N+1)
	dp := make([][]int, N+1)
	for i, _ := range dp {
		dp[i] = make([]int, N+1)
	}
	for i := 0; i < N; i++ {
		p[i] = sc.nextInt()
		p[i+1] = sc.nextInt()
	}
	for i := 1; i <= N; i++ {
		dp[i][i] = 0
	}
	for l := 2; l <= N; l++ {
		for i := 1; i <= N-l+1; i++ {
			j := i + l - 1
			dp[i][j] = 1 << 60
			for k := i; k <= j-1; k++ {
				dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j])
			}
		}
	}

	fmt.Fprintln(wtr, dp[1][N])
	wtr.Flush()
}

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

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

int main() {
    // dp[i][j] i番目からj番目までの最小値
    int n;
    cin >> n;
    vector<int> r(n), c(n);
    rep(i, n) cin >> r[i] >> c[i];
    vector<vector<int>> dp(n, vector<int>(n, 1e9));
    rep(i, n) dp[i][i] = 0;
    for (int w = 1; w < n; w++) {
        //cout << "w=" << w << " ";
        for (int a = 0; a + w < n; a++) {
            //cout << "a=" << a << " ";
            int d = 1e9;
            for (int b = a; b <= a + w - 1; b++) {
                //cout << "b=" << b << " ";
                d = min(d, dp[a][b] + dp[b + 1][a + w] + r[a] * c[b] * c[a + w]);
            }
            dp[a][a + w] = d;
            //cout << "dp[" << a << "][" << a + w << "]=" << dp[a][a + w] << "\n";
        }
    }
    cout << dp[0][n - 1] << "\n";
}