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 "bits/stdc++.h"
#include "math.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll,ll> P;
typedef vector<P> vp;
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define drep(i,a,b) for(ll i=(a);i>=(b);--i)
#define SIZE(a) int((a).size())
#define out(a) cout<<(a)<<endl;
const int INF=INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];

int main(){
  ll n;cin>>n;
  vll p(n+1);
  vvll dp(n,vll(n,MOD));
  rep(i,0,n){
    cin>>p[i]>>p[i+1];
  }
  rep(i,0,n){
    dp[i][i]=0;
  }
  rep(haba,0,n){
    rep(i,0,n-haba){
      int j=i+haba;
      rep(k,i,j){
        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i]*p[k+1]*p[j+1]);
      }
    }
  }
  cout<<dp[0][n-1]<<endl;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <utility>
#include <map>
int needleX[4] = { -1,0,1,0 };
int needleY[4] = { 0,1,0,-1 };

int main(void) {
    int n;

    std::cin >> n;
    std::vector<int> p(100+1);
    for (int i = 1; i <= n; i++) {
        std::cin >> p[i-1] >> p[i];
    }
    int m[100 + 1][100 + 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;
            m[i][j] = (1<<21);
            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]);
            }
        }
    }
    /*for (int i = 1; i <= n; i++) {
        for (int j = i+1; j <= n; j++) {
            std::cout << m[i][j] << " ";
        }
        std::cout << std::endl;
    }*/
    std::cout << m[1][n] << std::endl;
    return 0;
}

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

long long int n, mat[100+1][100+1], len[100+1];

int main(void){
  int i, j, k, l;

  scanf("%d", &n);
  
  for(int i=1; i<=n; i++){
    scanf("%d %d", &len[i-1], &len[i]);
  }
  
  for(l=2; l<=n; l++){
    for(i=1; i<=n-l+1; i++){
      j=i+l-1;
      mat[i][j]=INT_MAX;

      for(k=i; k<=j-1; k++){
	mat[i][j]=min(mat[i][j], mat[i][k]+mat[k+1][j]+len[i-1]*len[k]*len[j]);
      }
    }
  }

  printf("%d\n", mat[1][n]);
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <list>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int main() {

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

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

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

	return 0;
}

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

#define N 100

int M[101][101], p[101], n;

int solve()
{
	int l, i, j, k;
	for (l = 2; l <= n; l++) {
		for (i = 1; i <= n - l + 1; i++) {
			j = i + l - 1;
			M[i][j] = INT32_MAX;
			for (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]);
		}
	}
	return M[1][n];
}

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

	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;

	cout << solve() << endl;
	return 0;
}

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