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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dp[101][101];

int main() {
  int n;
  cin >> n;
  vector<int> p(n + 1);
  rep(i, n) cin >> p[i] >> p[i + 1];

  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n - l + 1; ++i) {
      int j = i + l - 1;
      dp[i][j] = __INT_MAX__;
      for (int k = i; k <= j - 1; ++k) {
        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  cout << dp[1][n] << endl;

  return 0;
}

C++: // n個の行列が与えられたとき、スカラー乗算の回数が最小になるように
// 行列の計算順序を決定する連鎖行列積問題(Matrix Chain Multiplication)

#include <iostream>
#include <algorithm>
using namespace std;

static const int N = 100;
static const int INF = (1 << 21);

int main() {
	int p[N + 1]; // 行列M_iの行数と列数を記録する(M_i = P_i-1 * P_iになる)
	int m[N + 1][N + 1]; // 行列演算の最小回数(コスト)の表

	int n; cin >> n; // 行列の個数

	// n個の行列の行と列を記録する( (2*3) * (3*4) => {2, 3, 4} )
	for (int i = 1; i <= n; i++) {
		cin >> p[i - 1] >> p[i];
	}

	for (int i = 1; i <= n; i++) m[i][i] = 0; // mの対角成分を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] = INF; // INF
			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 <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;


int n;
int m[101][101];
int p[101];

void matrixChainMultiplication() {
    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 ; 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() {
    // 制約見て！！！デカかったらlong longにすること！
    cin >> n;
    int _;
    rep(i, n) {
        if(i == 0) {
            scanf("%d %d", &p[i], &p[i + 1]);
        } else {
            scanf("%d %d", &_, &p[i + 1]);
        }
    }
    matrixChainMultiplication();
    return 0;
}

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) {
                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 <cstring>

int min_count[100][100];

int count(int *r, int *c, int start, int end) {
    if (min_count[start][end] != 0) {
        return min_count[start][end];
    }
    if (end - start == 0) {
        return 0;
    }
    int min = 1000000;
    for (int i = start; i < end; i++) {
        min = std::min(min, count(r, c, start, i) + r[start] * c[i] * c[end] + count(r, c, i + 1, end));
    }
    min_count[start][end] =min;
    return min_count[start][end];
}

int main() {
    std::memset(min_count, 0, sizeof(min_count));
    int n;
    std::cin >> n;
    int r[n], c[n];
    for (int i = 0; i < n; i++) {
        std::cin >> r[i] >> c[i];
    }

    std::cout << count(r, c, 0, n - 1) << std::endl;
}

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

static const int N = 100;
int p[N+1];
int m[N+1][N+1];

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

    for(int i = 1; i <= h; i++) m[i][i] = 0;

    // lはこれから計算する行列積を成す行列の数2->h(DP)
    for(int l = 2; l <= h; l++){
        // iはこれから計算する行列積の先頭の行列
        for(int i = 1; i <= h-l+1; i++){
            // jはこれから計算する行列積の末尾の行列(これを含む)
            int j = i + l -1;
            m[i][j] = 1<<30;
            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[1][h] << 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; 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 <bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> p( n + 1 );
	for( int i = 1; i < n + 1; i++ ) {
		cin >> p[i - 1] >> p[i];
	}

	vector<vector<int>> m( n + 1, vector<int>( n + 1 ) );
	for( int l = 2; l < n + 1; l++ ) {
		for( int i = 1; i < n - l + 1 + 1; i++ ) {
			int j = i + l - 1;
			m[i][j] = INT32_MAX / 4;
			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[1][n] << endl;
}