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 <cstdio>
#include <map>
#define MAX_NUM 100
#define INFTY 1000000000
#define NIL -1

using namespace std;

//think easy make easy

int n;//num of input


pair<int,int> MatrixChain[MAX_NUM+1];
map<pair<int,int>,int> mapMC;

int calc(int s,int e){//s~eの連鎖行列積の最少計算回数を出力
	int min_time  = INFTY;
	int min_tmp;

	if(mapMC[pair<int,int>(s,e)] != NIL)
		return mapMC[pair<int,int>(s,e)];

	if(s == e){
		return 0;
	}
	for(int i=s;i<e;i++){
		min_tmp = calc(s,i) + calc(i+1,e) 
			+MatrixChain[s].first*MatrixChain[i].second*MatrixChain[e].second;
		if(min_tmp < min_time)
			min_time = min_tmp;
	}
	mapMC[pair<int,int>(s,e)] = min_time;//メモ
	return min_time;
}

int main(){
	scanf("%d",&n);
	int r,c;//row,column

	for(int i=0;i<n;i++){
		scanf("%d %d",&r,&c);
		MatrixChain[i] = pair<int,int>(r,c);//i:1~n-1
	}
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			mapMC[pair<int,int>(i,j)] = NIL;
		}
	}

	cout <<calc(0,n-1) <<endl;

	return 0;
}

C++: #include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <algorithm>

int n;
int r[100],c[100],dp[100][100]={0},ans=0;

int cal(int i,int j,int k)
{
	return r[i]*c[j]*c[k];
}

int main(void)
{
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		cin>>r[i]>>c[i];
	}
	for (int l=2;l<=n;l++)
	{
		for (int i=1;i+l-1<=n;i++)
		{
			//cout<<"i:"<<i<<" l:"<<l<<endl;
			if (l==2) dp[i][i+l-1]=dp[i+l-1][i]=r[i]*c[i]*c[i+l-1];
			else {
				int m=0x7fffff;
				for (int j=i;j<i+l-1;j++)
				{
					int n=dp[i][j]+dp[j+1][i+l-1]+r[i]*c[j]*c[i+l-1];
					m=m<n?m:n;
				}
				dp[i][i+l-1]=m;
			}
			//cout<<"dp[i][i+l-1]="<<dp[i][i+l-1]<<endl;
		}
	}
	/*for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
		{
			cout<<"i:"<<i<<" j:"<<j<<"dp[i][j]:"<<dp[i][j]<<endl;
		}
	}*/
	cout<<dp[1][n]<<endl;
	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<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;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;
}

C++: const int MAX_SIZE = 101;

int dp[MAX_SIZE][MAX_SIZE];

int solve(int p[], int begin,int end) {
	if (dp[begin][end] != -1)return dp[begin][end];
	if (begin >= end - 1)return 0;
	int res = -1;
	for (int i = begin + 1; i < end; i++) {
		int temp = solve(p, begin, i) + solve(p, i, end) + (p[begin] * p[i] * p[end]);
		if (res != -1) {
			res = min(temp, res);
		}
		else {
			res = temp;
		}
	}
	return dp[begin][end] = res;
}

int main() {
	int N;
	int p[MAX_SIZE + 1];

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

	for (int i = 0; i < MAX_SIZE; i++) {
		for (int j = 0; j < MAX_SIZE; j++) {
			dp[i][j] = -1;
		}
	}

	cout << solve(p, 0, N) << endl;
	return 0;
}

C++: #include <stdio.h>

int main() {
	int n, i, j, k, l, p[101], m[101][101];
	
	scanf("%d", &n);
	for ( i = 1; i <= n; i++) {
		scanf("%d", &p[i - 1]);
		scanf("%d", &p[i]);
	}
	
	for ( i = 1; i <= n; i++) {
		m[i][i] = 0;
	}
	for ( l = 2; l <= n; l++) {
		for ( i = 1; i <= n - l + 1; i++) {
			int j = i + l - 1;
			m[i][j] = ( 1 << 21);
			for ( k = i; k <= j - 1; k++) {
				if ( m[i][j] < m[i][k] + m[k + 1][j] + p[i - 1]*p[k]*p[j]) {
					m[i][j] = m[i][j];
				} else {
					m[i][j] = m[i][k] + m[k + 1][j] + p[i - 1]*p[k]*p[j];
				}
			}
		}
	}
	
	printf("%d\n", m[1][n]);
	
	return 0;
}

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

static const int N = 101;

int main(){
    //配列mにはn以下の行列連鎖積の最小回数が格納される
    int n,p[N],m[N][N];
    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++){
            //i,j,kは行列の範囲を表す
            //i<=k<j
            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;
}

C++: //?????????B 
#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[1][n]<<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],i,l,j,k;
    cin >> n;
    for(i=1;i<=n;i++)
    {
        cin >> p[i-1] >> p[i];
    }

    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] = (i << 21);
        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]);
        }
        }
    }

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

    return 0;
}