Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	mm := make([]int, n+1)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		mm[i] = getNextInt(scanner)
		mm[i+1] = getNextInt(scanner)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			for k := i; k <= j-1; k++ {
				d := dp[i][k] + dp[k+1][j] + mm[i-1]*mm[k]*mm[j]
				if dp[i][j] == 0 || dp[i][j] > d {
					dp[i][j] = d
				}
			}
		}
	}

	fmt.Fprintln(writer, dp[1][n])

	writer.Flush()
}

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++)
#define repi(i,a,b) for(int i = (a); i < (b); 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};
pii A[200];//firstは行secondを列とする
int dp[200][200];//dp[i][j]:=Ai...Ajの最小スカラー積とする
int n;
int f[200][200];//最適な括弧を挿入する位置
void solve(){
	for(int i =0;i < n;i++){
		for(int j =0;j < n;j++){
			if(i==j)dp[i][j]=0;
			else dp[i][j]=INF;
		}
	}
	
	for(int k =1;k <n;k++){//鎖の数
		for(int i =0;i <n-k;i++){//どこから始めるか
			for(int j =i;j <i+k;j++){//どこに括弧を挟むか
				int tmp =A[i].first*A[j].second*A[i+k].second;
				if(dp[i][i+k] >dp[i][j]+dp[j+1][i+k]+tmp){
					dp[i][i+k]=dp[i][j]+dp[j+1][i+k]+tmp;
					f[i][i+k] =j;
				}
			}
		}
	}
}

void print(int i,int j){//解を表示する
	if(i==j){
		cout <<"A"<<i;
	}else{
		cout <<"(";
		print(i,f[i][j]);
		print(f[i][j]+1,j);
		cout <<")";
	}
}


int main(){
	cin>>n;
	for(int i =0;i < n;i++){
		cin>>A[i].first>>A[i].second;
	}
	solve();
	cout <<dp[0][n-1]<<endl;
	//print(0,n-1);
	return 0;
}

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

static const int NMAX = 100;
static const int RMAX = 100;
static const int CMAX = 100;
static const int INF = (1 << 29);

int main() {
    int n; cin >> n;
    int M[NMAX+1][NMAX+1];
    int r[RMAX+1];
    int c[CMAX+1];
    int min_cost;

    for (int i = 1; i < n+1; ++i) cin >> r[i] >> c[i];

    for (int i = 0; i < n; ++i) {
        for (int j = 1; j < n+1-i; ++j) {
            for (int k = 1+i; k < n+1; ++k) {
                if (j==k) {
                    M[j][k] = 0;
                    continue;
                }

                min_cost = INF;
                for (int l = j; l < k; ++l) {
                    min_cost = min(min_cost, M[j][l] + M[l+1][k] + (r[j]*c[l]*c[k]));
                }
                M[j][k] = min_cost;
            }
        }
    } 

    // for (int i = 1; i < n+1; ++i) {
    //     for(int j = 1; j < n+1; ++j) {
    //         cout << M[i][j] << " ";
    //     }
    //     cout << endl;
    // }

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


    return 0;
}

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

typedef long long ll;
#define inf 1e9+7

int n;
ll dp[102][102];
ll p[102];

void init(){
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            dp[i][j]=inf;
        }
        dp[i][i]=0;
    }
}

void cal(int l, int r){
    if(l>=r)return;
    for(int k=l;k<r;k++){
        cal(l,k);
        cal(k+1,r);
        dp[l][r] = min(dp[l][r], dp[l][k]+dp[k+1][r]+p[l]*p[k+1]*p[r+1]);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    init();
    cin >> p[0] >> p[1];
    ll a,b;
    for(int i=1;i<n;i++){
        cin >> a >> b;
        p[i+1]=b;
    }
    cal(0,n-1);
    cout << dp[0][n-1] << endl;
    return 0;
}

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

#define REP(i, n) for(int i = 0; i < (n); i++)

typedef pair<int, int> pii;

const int N_SIZE = 100;

int dp[N_SIZE + 1][N_SIZE + 1];

void solve(int n, pii rc[])
{
  REP(i, n)
    dp[i][i] = rc[i].first * rc[i].second;
  REP(i, n - 1)
    dp[i][i + 1] = dp[i][i] * rc[i + 1].second;
  for(int i = 2; i < n; i++){
    for(int j = 0, k = i + j, m = 100000000; k < n; j++, k++){
      for(int l = j, x; l < k; l++){
	x = ((j == l) ? dp[j][l] * rc[k].second : dp[j][l]) + ((l + 1 == k) ? dp[l + 1][k] * rc[j].first : dp[l + 1][k]);
	if(j != l && l + 1 != k)
	  x += rc[j].first * rc[l].second * rc[k].second;
	m = min(m, x);
      }
      dp[j][k] = m;
      m = 100000000;
    }
  }
}
 
int main()
{
  int n;
  cin >> n;
  pii rc[n];
  REP(i, n)
    cin >> rc[i].first >> rc[i].second;
  solve(n, rc);
  cout << dp[0][n - 1] << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <sstream>
#include <numeric>

// namespace
using namespace std;

// int mcm(const vector<int> &p, int i, int j) { // i-th matrix has the size (p[i], p[i+1])
//     if (i == j) return 0;
//     int m = INT_MAX;
//     for (int k = i; k < j; ++k) {
//         m = min(m, mcm(p, i, k) + mcm(p, k + 1, j) + p[i] * p[k + 1] * p[j + 1]);
//     }
//     return m;
// }

// int mcm(const vector<int> &p, int i, int j, vector<vector<int>> &dp) { // i-th matrix has the size (p[i], p[i+1])
//     if (i == j) return 0;
//     if (dp[i][j] < INT_MAX) return dp[i][j];
//     int m = INT_MAX;
//     for (int k = i; k < j; ++k) {
//         m = min(m, mcm(p, i, k, dp) + mcm(p, k + 1, j, dp) + p[i] * p[k + 1] * p[j + 1]);
//     }
//     return dp[i][j] = m;
// }

int mcm(const vector<int> &p) { // i-th matrix has the size (p[i], p[i+1])
    int n = p.size() - 1;
    vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
    for (int i = 0 ; i < n; ++i) dp[i][i] = 0;
    for (int l = 1; l < n; ++l)
        for (int i = 0, j; (j = i + l) < n; ++i)
            for (int k = i; k < j; ++k)
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1]);
    return dp[0][n-1];
}

int main() {
    int n, r, c;
    cin >> n;
    vector<int> p;
    for (int i = 0; i < n; ++i) {
        cin >> r >> c;
        p.push_back(r);
    }
    p.push_back(c);
    auto ans = mcm(p);
    cout << ans << 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 ( 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;
}