Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	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] = 1 << 21
			//kまでで区切る
			for 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])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[1][n])
}

C++: #include <iostream>
#include <vector>
#include <functional>


std::vector<std::vector<int>> mats, cost;


int calcMin(int start, int end) {
    if (cost[start][end] != -1) return cost[start][end];

    int min_cost;
    for (int i = start; i < end; i++) {
        int tmp_cost = calcMin(start, i) + calcMin(i + 1, end)
            + mats[start][0] * mats[i][1] * mats[end][1];
        if (i == start) {
            min_cost = tmp_cost;
        }
        if (min_cost > tmp_cost) min_cost = tmp_cost;
    }
    cost[start][end] = min_cost;
    return cost[start][end];
}

int main() {
    int n;
    std::cin >> n;
    
    mats = std::vector<std::vector<int>>(n, std::vector<int>(2));
    cost = std::vector<std::vector<int>> (n, std::vector<int>(n, -1));

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

    for (int i = 0; i < n; i++) {
        cost[i][i] = 0;
    }

    std::cout << calcMin(0, n - 1) << std::endl;
}

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

struct Matrix {
  int numOfRow;
  int numOfColumn;
};


struct MinMatrix {
  int minMultiply;
  int numOfRow;
  int numOfColumn;
};


MinMatrix getMinScalarMultiply(const Matrix matrices[], int start, int end)
{
  DPRINTF("start %d  end %d\n", start, end);

  assert(start < end);

  if (start >= end)
    return MinMatrix();

  if (start + 1 == end)
  {
    MinMatrix mtx;

    mtx.minMultiply = 0;
    mtx.numOfRow = matrices[start].numOfRow;
    mtx.numOfColumn = matrices[start].numOfColumn;

    return mtx;
  }


  MinMatrix minMtx;
  minMtx.minMultiply = INT_MAX;

  for (int i = start + 1; i < end; i++)
  {
    MinMatrix mtx0 = getMinScalarMultiply(matrices, start, i);
    MinMatrix mtx1 = getMinScalarMultiply(matrices, i, end);

    MinMatrix mtx;

    mtx.minMultiply = mtx0.minMultiply + mtx1.minMultiply + mtx0.numOfRow * mtx0.numOfColumn * mtx1.numOfColumn;
    mtx.numOfRow = mtx0.numOfRow;
    mtx.numOfColumn = mtx1.numOfColumn;

#if DEBUG
    DPRINTF("start %d  end %d  i %d : mtx.minMultiply %d  min.minMultiply %d\n",
        start, end, i, mtx.minMultiply, minMtx.minMultiply);
    DPRINTF("  mtx0.minMultiply %d  mtx1.minMultiply %d\n",
        mtx0.minMultiply, mtx1.minMultiply);
#endif

    if (minMtx.minMultiply > mtx.minMultiply)
      minMtx = mtx;
  }

  DPRINTF("start %d  end %d  multiply %d\n", start, end, minMtx.minMultiply);

  return minMtx;
}


int calcMinScalarMultiply(const Matrix matrices[], int num)
{
  if (num <= 1)
    return 0;

  MinMatrix mtx = getMinScalarMultiply(matrices, 0, num);

  return mtx.minMultiply;
}


int main() {
  int numOfMatrix;
  cin >> numOfMatrix; cin.ignore();

  Matrix  matrices[numOfMatrix];
  for (int i = 0; i < numOfMatrix; i++)
  {
    cin >> matrices[i].numOfRow; cin >> matrices[i].numOfColumn; cin.ignore();
  }

#if DEBUG
  for (int i = 0; i < numOfMatrix; i++)
  {
    DPRINTF("Matrix %d: %d %d\n", i, matrices[i].numOfRow, matrices[i].numOfColumn);
  }
#endif

  cout << calcMinScalarMultiply(matrices, numOfMatrix) << endl;

  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int> >
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define inf (1ll<<60)

using namespace std;

const int mod=1e9+7;

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

void run(){
    int n;
    cin>>n;
    vi r(n),c(n);
    rep(i,n){
        cin>>r[i]>>c[i];
    }
    int dp[n+1][n+1];
    MEMSET(dp);
    REP(a,2,n){
        rep(i,n-a+1){
            int j=i+a-1;
            dp[i][j]=inf;
            REP(k,i,j-1){
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+r[i]*c[k]*c[j]);
            }
        }
    }
    cout<<dp[0][n-1]<<endl;
}

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 <= m; j++)c[0][j] = 0;

	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= m; 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<iostream>
#include<algorithm>
using namespace std;

static const int N = 100;

int main() {
	int n, p[N + 1], m[N + 1][N + 1];
	cin >> n;
	for (int i = 1; i <= n; i++) {
		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] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
				cout << m[i][j] << endl;
			}
		}
	}

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

	return 0;
}

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

static const int N=100;

int main()
{
    int n;
    int p[N+1],m[N+1][N+1];
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        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]=min(m[i][j],m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
            }
        }
    }

    cout<<m[1][n]<<endl;
 	return 0;
}