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 (r Range) length() int {
	return r.end - r.start + 1
}

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[r.start].r, matrices[r.end].c}, v
	}
	if r.length() == 0 {
		panic("must not be empty")
	} else if r.length() == 1 {
		return matrices[r.start], 0
	}

	var minCount int = math.MaxInt32

	for i := 0; i < r.length()-1; i++ {
		formerRange := Range{r.start, r.start + i}
		latterRange := Range{r.start + i + 1, r.end}
		matF, countF := calcMinMatrixChainMultiplication(matrices, formerRange, memo)
		matL, countL := calcMinMatrixChainMultiplication(matrices, latterRange, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	memo[r] = minCount
	return Matrix{matrices[r.start].r, matrices[r.end].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++: //
//  main.cpp
//  matrix_chain_multiplication
//
//  Created by ???????????? on 2017/10/17.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#define INFTY (1 << 21)


using namespace std;

int p[110];

void matrixChainMultiplication(int n){
    int 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] = INFTY;
            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;
}


int main(int argc, const char * argv[]) {
    int n, i, pi, pi_1;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        scanf("%d %d", &pi, &pi_1);
        if(i) p[i+1] = pi_1;
        else {
            p[0] = pi;
            p[1] = pi_1;
        }
    }
    matrixChainMultiplication(n);
//    std::cout << "Hello, World!\n";r
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N = 1000;

int lcs(string X, string Y) {
	int c[N + 1][N + 1];
	int m = X.size();
	int n = Y.size();
	int maxl = 0;
	X = ' ' + X;
	Y = ' ' + Y;
	for (int i = 1; i <= m; i++)c[i][0] = 0;
	for (int j = 1; j <= n; j++)c[0][j] = 0;

	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			if (X[i] == Y[j]) {
				c[i][j] = c[i - 1][j - 1] + 1;
			}
			else {
				c[i][j] = max(c[i - 1][j], c[i][j - 1]);
			}
			maxl = max(maxl, c[i][j]);
		}
	}
		return maxl;
}

	int main() {
		string s1, s2;
		int n;
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> s1 >> s2;
			cout << lcs(s1, s2) << endl;
		}

		return 0;
	}

C++: #include<stdio.h>

#define N 106
#define INF 100000000

int P[N];
int m[N][N];


int matrixchainorder(int*,int);

int main(){
  int i;
  int num;

  scanf("%d",&num);
  

  for(i=1;i<=num;i++){
    //printf("%d\n",i);
    scanf("%d%d",&P[i-1],&P[i]);
  }
  
  printf("%d\n",matrixchainorder(P,num));

  return 0;
}


int matrixchainorder(int *P,int length){
  int n,i,j,k;
  int q;
  int l;

  n = length;

  for(i=1;i<=n;i++){
    m[i][i] = 0;
  }

  for(l = 2;l <= n;l++){
    for(i=1;i<=n-l+1;i++){
      j = i+l-1;
      m[i][j] = INF;
      for(k=i;k<=j-1;k++){
	q = m[i][k] + m[k+1][j] + P[i-1]*P[k]*P[j];
	if(m[i][j] > q){
	  m[i][j] = q;
	  //printf("q = %d\n",q);
	}
      }
    }
  }

  /*printf("DEBUG\n");
  for(int a = 7; a >= 1; a--){
    for(int b = 0; b <= 6; b++){
      printf("%d ", m[b][a]);

    }
    printf("\n");
  }
  */

  return m[1][n];
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

ll dp[110][110];

int main() {
    ll N;
    cin >> N;
    vll p;
    rep(i, N) {
        ll a, b;
        cin >> a >> b;
        if(i == 0) p.emplace_back(a);
        p.emplace_back(b);
    }

    rep(i, 110) rep(j, 110) dp[i][j] = INF;
    rep(i, 100) dp[i][i] = 0;

    for(ll l = 2; l <= N; l++) rep(i, N-l+1) {
        ll j = i + l - 1;
        for(ll k = i; k < j; k++) chmin(dp[i][j], dp[i][k] + dp[k+1][j] + p[i] * p[k+1] * p[j+1]);
    }
    cout << dp[0][N-1] << endl;
}

C++: #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
ll n;
vector<ll> r(100), c(100);
vector<vector<ll> > dp(101, vector<ll>(101, -1));
int dfs(int v, int w) {
    // cout << v << ":" << w << endl;
    if(dp[v][w] != -1) {
        return dp[v][w];
    } else {
        ll tmp = INT_MAX / 3;
        for(int i = v + 1; i < w; i++) {
            tmp = min(tmp, dfs(v, i) + dfs(i, w) + r[v] * r[i] * c[w - 1]);
        }
        dp[v][w] = tmp;
        return tmp;
    }
}
int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> r[i] >> c[i];
    }
    for(int i = 0; i < n; i++) {
        dp[i][i] = 0;
        dp[i][i + 1] = 0;
    }
    dfs(0, n);
    cout << dp[0][n] << endl;
}

C++: #include <iostream>

using namespace std;

const int N = 100;

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

int main()
{
    int n;
    int rc[N+1];
    int mat[N + 1][N] = {};
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> rc[i] >> rc[i + 1];
    }

    // 行列の長さl
    for (int l = 2; l < n + 1; l++)
    {
        // i番目の積 積の数は : n - l + 1
        for (int i = 1; i < n - l + 2; i++)
        {
            mat[l][i] = (1 << 21);
            for (int k = 1; k < l; k++)
            {
                mat[l][i] = min(mat[l][i], mat[k][i] + mat[l - k][i + k] + rc[i-1] * rc[i+k-1] * rc[i+l-1]);
            }
        }
    }

    cout << mat[n][1] << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

ll dp[100][100];
vector<pair<int,int>> matrix(100);


ll kukandp(int i, int j){
    if(dp[i][j]!=INF)return dp[i][j];
    if(j-i==0)dp[i][j]=0;
    if(j-i==1)dp[i][j]=matrix[i].first*matrix[i].second*matrix[j].second;

    if(j-i>1){
        for(int k=i; k<j; k++){
            dp[i][j]=min(dp[i][j], kukandp(i,k)+kukandp(k+1,j)+matrix[i].first*matrix[k].second*matrix[j].second);
        }
    }

    return dp[i][j];
}

int main(){
    int N; cin>>N;
    rep(i,N){
        cin>>matrix[i].first>>matrix[i].second;
    }
    rep(i,100)rep(j,100)dp[i][j]=INF;
    cout<<kukandp(0,N-1)<<endl;
    return 0;
}