Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

type matrixSize struct{ row, col int }

func calcNum(i, j matrixSize) int {
	return i.row * j.col * i.col
}

func parseProblem() (int, []matrixSize) {
	n := scanInt(sc)
	sizes := make([]matrixSize, n)
	for i := 0; i < n; i++ {
		sizes[i] = matrixSize{row: scanInt(sc), col: scanInt(sc)}
	}
	return n, sizes
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = math.MaxInt64
		}
	}
}

func resolve(n int, sizes []matrixSize) int {
	// TODO 復習
	initDP(n)
	_rec(sizes, 0, n)
	return dp[0][n]
}

func _rec(sizes []matrixSize, l, r int) int {
	if dp[l][r] != math.MaxInt64 {
		return dp[l][r]
	}
	if r-l <= 1 {
		dp[l][r] = 0
		return 0
	}
	num := math.MaxInt64
	for i := l + 1; i < r; i++ {
		v := _rec(sizes, l, i) + _rec(sizes, i, r) + calcNum(matrixSize{sizes[l].row, sizes[i].row}, matrixSize{sizes[i].row, sizes[r-1].col})
		if v < num {
			num = v
		}
	}
	dp[l][r] = num
	return dp[l][r]
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<ll,pii> iii;

const ll oo = 0x3f3f3f3f;

int N, M, a[MAX][MAX];

int R(vector<int> h){

    int rec = 0;

    stack<int> st;

    vector<int> l(M, 0);

    for(int I = 0; I < M;++I){

        while(!st.empty() && h[st.top()] >= h[I])
            st.pop();
        if(st.empty()){
            l[I] = 0;
        }
        else l[I] = st.top() + 1;

        st.push(I);
    }

    while(!st.empty())
        st.pop();

    vector<int> r(M, 0);

    for(int I = M - 1;I >= 0;--I){

        while(!st.empty() && h[st.top()] >= h[I])
            st.pop();
        if(st.empty()){
            r[I] = M-1;
        }
        else r[I] = st.top() - 1;

        st.push(I);
    }

    for(int I = 0;I < M;++I){
        rec = max(rec, (r[I] - l[I] + 1)*h[I]);
    }

    return rec;
}

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

    cin >> N >> M;

    vector<int> h(M, 0);

    for(int I = 0;I < N;++I){
        for(int K = 0;K < M;++K) {
            cin >> a[I][K];
            if (a[I][K])
                a[I][K] = 0;
            else a[I][K] = 1;
        }
        if(!I){
            for(int K = 0;K < M;++K)
                h[K] = a[I][K];
        }
    }

    int solve = R(h);

    for(int I = 1;I < N;++I){

        for(int K = 0;K < M;++K) {

            if (a[I][K])
                a[I][K] += a[I - 1][K];
        }

        for(int K = 0;K < M;++K)
            h[K] = a[I][K];

        solve = max(solve, R(h));
    }

    cout << solve << endl;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
#include<list>
#include <array>
using namespace std;
#define i64  long long
#define int  long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define INF I64_MAX
#define MOD 1000000007
#define MEM_SIZE = 1e5+5;

template<typename T> void DEBUG(T e){std::cout << e << std::endl;}
template<class T> void DEBUG(string str, T e){std::cout <<str << ">>" << e << std::endl;}
template<typename T> void DEBUG(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ DEBUG(v); } }
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}




//-----CODE------//


void solve(void)
{
  int N;
  cin>>N;
  vector<int> INPUT(N+1);
  for (int i = 0; i < N; i++)
  {
    cin>>INPUT[i]>>INPUT[i+1];
  }
  vector<vector<int> > DP(N+1,vector<int>(N+1,INF));
  for (int i = 0; i <= N; i++)
  {
    DP[i][i] = 0;
  }

  for (int l = 2; l <= N; l++)
  {
    for (int i = 1; i <= N - l +1; i++)
    {
      int j = i + l -1;
      for (int k = i; k <= j-1; k++)
      {
        DP[i][j] = min(DP[i][j],DP[i][k] + DP[k+1][j] +INPUT[j]*INPUT[k]*INPUT[i-1]);
      }
    }
  }
  cout<<DP[1][N]<<endl;
  
  
  
  return; 
}


int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(9);
  solve();

 
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
    int n;
    scanf("%d", &n);
    vector<int> p(n+1);
    rep(i, n) {
        int r, c;
        scanf("%d %d", &r, &c);
        if (i == 0) p[i] = r;
        p[i+1] = c;
    }

    int m[n+1][n+1];
    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;
            int MIN;
            for (int k = i; k < j; k++) {
                if (k == i) MIN = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j];
                else MIN = min(MIN, m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]);
            }
            m[i][j] = MIN;
        }
    }
    printf("%d\n", m[1][n]);
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const double PI = acos(-1);
const ll MOD = 1000000007;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

int main() {
  int N; cin >> N;
  vector<ll> r(N),c(N);
  rep(i,N) cin >> r[i] >> c[i];

  vector<vector<ll>> dp(N, vector<ll>(N,1e18));
  rep(i,N) dp[i][i] = 0;
  
  for (int j = 1; j < N; j++) {
    for (int i = 0; i < N-j; i++) {
      for (int k = i; k < i + j; k++) {
        chmin(dp[i][i+j],dp[i][k] + dp[k+1][i+j] + r[i] * c[k] * c[i+j]); 
      }
    }
  }

  cout << dp[0][N-1] << endl;
}

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

int main() {
    int n;
    scanf("%d", &n);

    int p[n+1], m[n][n];
    
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &p[i], &p[i+1]);
    }

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

    for (int l = 1; l < n; l++) {
        for (int i = 0; i < n - l; i++) {
            int j = i + l;
            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] * p[k+1] * p[j+1]);
            }
            // printf("m[%d][%d] = %d\n", i, j, m[i][j]);
        }
        // printf("--\n");
    }
    printf("%d\n", m[0][n-1]);
    return 0;
}