Go: package main

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

type Matrix struct {
	r int // num of row
	c int // num of column
}

type Range struct {
	start int // index, inclusive
	end   int // index, inclusive
}

func calcMultiplication(x, y Matrix) (Matrix, int) {
	if x.c != y.r {
		panic("cannnot calculate")
	}
	return Matrix{x.r, y.c}, x.r * y.c * x.c
}

func calcMinMatrixChainMultiplication(matrices []Matrix, r Range, memo map[Range]int) (Matrix, int) {
	if v, ok := memo[r]; ok {
		return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, v
	}
	if len(matrices) == 0 {
		panic("must not be empty")
	} else if len(matrices) == 1 {
		return matrices[0], 0
	} else if len(matrices) == 2 {
		return calcMultiplication(matrices[0], matrices[1])
	}

	var minCount int = math.MaxInt32

	for i := 0; i < len(matrices)-1; i++ {
		formerMatrices := matrices[:i+1]
		latterMatrices := matrices[i+1:]
		matF, countF := calcMinMatrixChainMultiplication(formerMatrices, Range{0, i}, memo)
		matL, countL := calcMinMatrixChainMultiplication(latterMatrices, Range{i + 1, len(matrices) - 1}, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, minCount
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, _ := nextInt(sc)
	matrices := make([]Matrix, n)
	for i := 0; i < n; i++ {
		r, _ := nextInt(sc)
		c, _ := nextInt(sc)
		matrices[i] = Matrix{r, c}
	}
	memo := make(map[Range]int)
	_, resultCount := calcMinMatrixChainMultiplication(matrices, Range{0, len(matrices) - 1}, memo)
	fmt.Println(resultCount)
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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

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

using namespace std;

int c[1005][1005];

int lcs( string X, string Y){

    int m, n, i, j;

    m = X.size();
    n = Y.size();

    X = ' ' + X;
    Y = ' ' + Y;

    int Lmax = 0;

    for( i = 1; i <= m ; i++){

        c[i][0] = 0;

    }

    for( i = 1; i <= n ; i++ ){

        c[0][i] = 0;

    }

    for( i = 1; i <= m; i++ ){

        for( j = 1; j <= n ; j++ ){

            if( X[i] == Y[j] ){

                c[i][j] = c[i-1][j-1] + 1;

            } else if( c[i-1][j] >= c[i][j-1] ){

                c[i][j] = c[i-1][j];

            } else {

                c[i][j] = c[i][j-1];

            }

            Lmax = max( Lmax, c[i][j] );

        }

    }

    return Lmax;

}

int main(){

    int n;
    string X, Y;

    cin >> n;

    for( int i = 0 ; i < n ; i++ ){

        cin >> X >> Y;

        cout << lcs( X, Y) << endl;

    }

    return 0;

}