Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	p := make([]int, N+1)
	dp := make([][]int, N+1)
	for i, _ := range dp {
		dp[i] = make([]int, N+1)
	}
	for i := 0; i < N; i++ {
		p[i] = sc.nextInt()
		p[i+1] = sc.nextInt()
	}
	for i := 1; i <= N; i++ {
		dp[i][i] = 0
	}
	for l := 2; l <= N; l++ {
		for i := 1; i <= N-l+1; i++ {
			j := i + l - 1
			dp[i][j] = 1 << 60
			for 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])
			}
		}
	}

	fmt.Fprintln(wtr, dp[1][N])
	wtr.Flush()
}

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;
}