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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;


#define min(a, b) ((a) < (b) ? (a) : (b))

int N;
const int MAX_N = 110;
const int INF = 0x3fffffff;
int A[MAX_N];
int B[MAX_N];
int dp[MAX_N][MAX_N];

void init() {
    for(int i = 0; i < N; ++i)
        scanf("%d%d",&A[i], &B[i]);
    for(int i = 0 ;i < N; ++i)
        for(int j = 0; j < N; ++j)
            dp[i][j] = INF;
    for(int i = 0; i < N; ++i)
        dp[i][i] = 0;
}

int solve() {
    for(int k = 1; k < N; ++k) {
        for(int i = 0; i < N - k; ++i) {
            for(int j = i; j < i + k; ++j) {
                printf("i = %d j = %d k = %d\n", i, j, k);
                int sz = A[i] * B[j] * B[i + k];
                dp[i][i + k] = min(dp[i][i + k],dp[i][j] + dp[j+1][i + k] + sz);
            }
        }
    }
    // for(int i = 0; i < N; ++i) {                                                                                                                                              
    //  for(int j = 0; j < N; ++j)                                                                                                                                               
    //      printf("%d ", dp[i][j]);                                                                                                                                             
    //  printf("\n");                                                                                                                                                            
    // }                                                                                                                                                                         
    return dp[0][N - 1];
}

int main() {
    while(scanf("%d",&N) == 1) {
        init();
        printf("%d\n", solve());
    }
    return 0;
}

C++: #include <iostream>
#include<climits>
#include<vector>
using namespace std;
 
int MCM(int p[],int n) {
    vector<vector<int> > m(n,vector<int>(n));
    for (int i = 0; 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] = INT_MAX;
            for (int k = i; k <= j-1; k++) {
                int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
                if (m[i][j] > q) {
                    m[i][j] = q;
                }
 
            }
        }
 
    }
    return m[1][n];
}
 
int main() {
    int n, p[200];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> p[i] >> p[i + 1];
    }
    cout << MCM(p, n) << endl;
    return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
 
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
 
using namespace std;
 
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
 
const int INF=1<<29;
const double EPS=1e-9;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int dp[100][100] = {{0}};
std::vector<int> r;
std::vector<int> c;
int N;

int dfs(int i, int j) {
	if (dp[i][j] != 0) {return dp[i][j];}
	if (j == i) {
		return 0;
	}
	if (j - i == 1) {
		return dp[i][j] = r[i] * c[i] * c[j];
	}
	int ans = 1000000000;
	for (int k = i; k < j; ++k) {
		ans = std::min(ans, dfs(i, k) + dfs(k + 1, j) + r[i] * c[k] * c[j]);
	}
	return dp[i][j] = ans;
}

int main(int argc, char const *argv[]) {
	cin >> N;
	for (int i = 0; i < N; ++i) {
		int a, b;
		cin >> a >> b;
		r.push_back(a);
		c.push_back(b);
	}
	int ans = dfs(0, N - 1);
	cout << ans << endl;

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define PRINT(x) cout<<(x)<<endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_N = 100;

int n;
int r[MAX_N], c[MAX_N];

int dp[MAX_N][MAX_N];

int main() {
    cin >> n;
    REP(i, n) cin >> r[i] >> c[i];

    REP(i, n) {
        REP(j, n) dp[i][j] = MOD;
    }

    REP(i, n) dp[i][i] = 0;

    REP2(k, 1, n) {
        REP(i, n - k) {
            int j = i + k;
            REP2(l, i, j) {
                dp[i][j] = min(dp[i][j], dp[i][l] + dp[l+1][j] + r[i]*c[l]*c[j]);
            }
        }
    }

    PRINT(dp[0][n-1]);
    return 0;
}

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

const ll INF = 1e+18 + 10;
const int MAX_N = 100;
ll dp[MAX_N][MAX_N];

int main()
{
    int n;
    cin >> n;
    vector<int> r;

    for (int i = 0; i < n; i++)
    {
        int row, col;
        cin >> row >> col;
        if (i == 0)
        {
            r.push_back(row);
        }
        r.push_back(col);
    }

    // DPの初期化
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i == j)
            {
                dp[i][i] = 0;
            }
            else
            {
                dp[i][j] = INF;
            }
        }
    }

    // DPの更新
    for (int j = 1; j < n; j++)
    {
        for (int i = 0; i + j < n; i++)
        {
            for (int k = 0; k <= i + j; k++)
            {
                ll prod = r[i] * r[k + 1] * r[i + j + 1];
                dp[i][i + j] = min(dp[i][i + j], dp[i][k] + dp[k + 1][i + j] + prod);
            }
        }
    }

    cout << dp[0][n - 1] << endl;
    return 0;
}

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

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