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

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define MAX 105

ll p[MAX], m[MAX][MAX];

int main(){
    int n; cin >> n;
    for(int i = 0; i <= n; i++){
        int a,b; cin >> a >> b;
        if(i == 0){
            p[i] = a; i++; p[i] = b;
        }else{
            p[i] = b;
        }
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i == j) m[i][j] = 0;
            else m[i][j] = INT_MAX;
        }
    }

    for(int len = 2; len <= n; len++){ //計算する行列の個数
        for(int i = 1; i <= n-len+1; i++){ //左端
            int j = i + len -1; //右端
            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]);
                
            }
        }
    }

    // for(int i = 1; i <= n; i++){
    //     for(int j = 1; j <= n; j++){
    //         cout << m[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    cout << m[1][n] << endl;

    return 0;
}