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>
#include <cstdlib>
#include <cstdio>

using namespace std;

typedef struct matrix{
  int row;
  int column;
  struct matrix *next;
  struct matrix *back; 
}Matrix;

Matrix *push_back(Matrix *begin, int row, int column) {
  Matrix *p = (Matrix*)malloc(sizeof(Matrix));
  p->row = row;
  p->column = column;
  p->next = NULL;
  p->back = NULL;
  if(begin == NULL) return p;
  Matrix *p2 = begin;
  while(p2->next != NULL) p2 = p2->next;
  p2->next = p;
  p->back = p2;
  return begin;
}

Matrix *product_count(Matrix *x, int type, int *count) {
  Matrix *a,*b;
  if(type == 0) {
    a = x->back;
    b = x;
  }
  else {
    a = x;
    b = a->next;
  }
  *count = 0;
  if(a == NULL || b == NULL) return x;
  *count += a->row * a->column * b->column;
  Matrix *c = (Matrix*)malloc(sizeof(Matrix));
  c->row = a->row;
  c->column = b->column;
  c->next = b->next;
  c->back = a->back;
  if(a->back != NULL) a->back->next = c;
  if(b->next != NULL) b->next->back = c;
  free(a);
  free(b);
  return c;
}

Matrix *product_atob(Matrix *a, Matrix *b, int type,  long *sum) {
  int count;
  if(a == b) return a;
  if(type == 0) {
    while(a->next != b) {
      b = product_count(b, 0, &count);
      *sum += count;
    }
  }
  else {
   while(a->next != b) {
      a = product_count(a, 1, &count);
      *sum += count;
    }
  }
  a = product_count(a, 1, &count);
  *sum += count;
  return a;
}

Matrix *solve(Matrix *x, long *sum) {
  if(x->next == NULL) return x;
  Matrix *min = x->next, *p = x;
  while (p->next != NULL) {
    p = p->next;
    if(min->column > p->column) min = p;
  }
  if(min->column < x->row) {
    x = product_atob(x, min, 0, sum);
    if(x->next != NULL) {
      p = solve(x->next,sum);
      x = product_atob(x,p,0,sum);
    }
  }
  else if(min->column > x->row) {
    if(x->back == NULL || x->back->row > x->row) {
      while(min->next != NULL) min = min->next;
      x = product_atob(x, min, 1, sum);
    }
    else{
      x = product_atob(x,min,1,sum);
    }
  }
  else {
    Matrix *min2 = x->next;
    p = x->next;
    while (p != min) {
      if(min2->column > min->column && min2->column > p->column) min2 = p;
      p = p->next;
    }
    x = product_atob(x, min2, 1, sum);
    product_atob(x->next, min, 0, sum);
    x = product_atob(x, x->next, 1, sum);
    if(x->next != NULL) {
      p = solve(x->next,sum);
      x = product_atob(x,p,0,sum);
    }
    p = solve(x,sum);
  }
  return x;
}

int main()
{
  int i,n,r,c;
  Matrix *begin = NULL;
  scanf("%d",&n);
  for (i = 0; i < n; i++) {
    scanf("%d %d",&r,&c);
    begin = push_back(begin,r,c);

  }
  long sum = 0;
  begin = solve(begin, &sum);
  cout << sum << endl;
  
  return 0;
}

C++: #include <stdio.h>
//https://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
//return the cost
/*int cal(int a[],int i, int j){
    if(i==j) return 0;
    int min = 1<<30;
    //printf("%d\n",min);
    for(int k = i; k<j; k++){
        int count = cal(a, i, k)+cal(a,k+1, j)+a[i-1]*a[k]*a[j];
        if(count<min) min = count;
    }
    
    return min;
}*/

int main()
{
    int n;
    scanf("%d",&n);
    int x[n+2], y[n+2];
    int dp[n+2][n+2];
    for(int i = 1; i<=n; i++){
        scanf("%d %d",&x[i],&y[i]);
        dp[i][i] = 0;
    } x[n+1] = y[n];
    dp[n+1][n+1] = 0;
    int min = 1<<30;
    for(int m = 2; m<=n+1; m++){
        for(int i = 2; i<=n+2-m ; i++){
            int j = i+m-1;
            dp[i][j] = 1<<30;
            for(int k = i; k<j; k++){
                int count = dp[i][k]+dp[k+1][j]+x[i-1]*x[k]*x[j];
                if(count<dp[i][j]) dp[i][j] = count;
            }
        }
        
    }
    
    //printf("%d\n",cal(x, 2,n+1));
    printf("%d\n",dp[2][n+1]);
    return 0;
}

C++: #include <stdio.h>
#define SIZE 110
#define INF 1000000009

int dp[SIZE][SIZE];
int in[SIZE];

int min(int a, int b){
    return (a<b ? a : b);
}

int main(){
    int n,i,j,k;
    scanf("%d",&n);
    for(i=1; i<=n; i++){
        scanf("%d %d",&in[i-1],&in[i]);
    }

    for(i=1; i<=n; i++) dp[i][i] = 0;

    for(k=2; k<=n; k++){
        for(i=1; i<=n-k+1; i++){
            int v;
            v = i+k-1;
            dp[i][v] = INF;
            for(j=i; j<=v-1; j++){
                dp[i][v] = min(dp[i][v],dp[i][j]+dp[j+1][v]+in[i-1]*in[j]*in[v]);
            }
        }
    }

    printf("%d\n",dp[1][n]);

    return 0;
}

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

using ll = long long;
#define REP(i,n) for(int i=0; i < (n); ++i)
#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
// #define INF 1e9
const ll INF = 1LL << 60;

int n;
vector<int> r, c;
ll dp[105][105];

ll solve(){
  REP(i, n+1){
    dp[i][i+1] = 0;
  }
  FOR(w, 2, n+1){
    REP(left, n){
      int right = left + w;
      if(right > n) break;
      FOR(i, left+1, right){
        dp[left][right] = min(dp[left][right], dp[left][i]+dp[i][right] + r[left] * c[right-1]*r[i]);
      }
    }
  }
  return dp[0][n];
}

int main(){
  cin >> n;
  r.resize(n), c.resize(n);
  REP(i, n){
    cin >> r[i] >> c[i];
  }
  REP(i, n+3){
    REP(j, n+3){
      dp[i][j] = INF;
    }
  }
  cout << solve() << endl;
  return 0;
}