Go: package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)

	P := make([]int, n+1)
	for i := 1; i <= n; i++ {
		l := nextInt(sc)
		P[i-1] = l
		m := nextInt(sc)
		P[i] = m
	}

	matrix := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		matrix[i] = make([]int, n+1)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n - l + 1; i++ {
			j := i + l - 1
			matrix[i][j] = (1 << 21)
			for k := i; k <= j-1; k++ {
				matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k+1][j] + P[i-1] * P[k] * P[j])
			}
		}
	}
	fmt.Println(matrix[1][n])
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <limits>

const int N = 102;

long m[N][N], p[N+1];

int matrixChainOrder(int n){

  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] = std::numeric_limits<long>::max();
      for(int k=i; k<=j-1; k++){
        m[i][j] = std::min(m[i][j], m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]);
      }
    }
  }
  return m[1][n - 1];
}

int main(){
  int n ;
  std::cin >> n; //number of matrix

  for(int i=1; i<=n; i++){
    std::cin >> p[i-1] >> p[i];
  }
  // for (int i = 0; i <=n; ++i) std::cout << p[i] << std::endl;
  std::cout << matrixChainOrder(n + 1) << std::endl;

  return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <string>
#include <typeinfo>
#include <cstring>
#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 - 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>

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