Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

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

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(m[0][n-1])
}

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

const int N = 1000;

int main() {
    int n;
    int m[N + 1][N + 1];
    int p[N+ 1];
    for (int i = 1; i <= N; i++)
        m[i][i] = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> p[i] >> p[i + 1];
    }
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++) {
            int k = j + i - 1;
            m[j][k] = (1<<21);
            for (int l = j; l < k; 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define FORR(I,A,B) for(ll I = (B-1); I >= (A); --I)
const ll INF=1e18+7;
const ll MOD=1e9+7;


int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n;
	cin>>n;
	vector<ll> p(n+1);
	FOR(i,1,n+1){
		cin>>p[i-1]>>p[i];
	}
	ll dp[n+1][n+1];
	FOR(i,0,n+1)FOR(j,0,n+1)dp[i][j]=0;
	FOR(l,2,n+1){
		FOR(i,1,n+2-l){
			int j = i + l - 1;
			dp[i][j] = (1 << 21);
			FOR(k,i,j){
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j]);
			}
		}
	}
	cout << dp[1][n] << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

ll dp[110][110];
ll matrix[110][2];

int main() {
  int N;
  cin >> N;

  REP(i, N) {
    cin >> matrix[i][0] >> matrix[i][1];
  }

  // for (int i = 0; i < N - 1; i++) {
  //   dp[i][i + 1] = matrix[i][0] * matrix[i][1] * matrix[i + 1][1];
  // }
  for (int i = 0; i < N; i++) {
    dp[i][i] = 0;
  }

  // cout << dp[0][1] << endl;

  for (int l = 2; l <= N; l++) {
    for (int i = 0; i < N - l + 1; i++) {
      int j = i + l - 1;

      dp[i][j] = INF;

      for (int k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + matrix[i][0] * matrix[k][1] * matrix[j][1]);
      }
    }
  }




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

  return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
//think easy make easy

int q;//num of datasets
string X;
string Y;
char map[MAX_LENGTH+1][MAX_LENGTH+1];

int findMaxLength(int x,int y){//x:Xの終端.y:Yの終端(0オリジン)
	if(x < 0 || y < 0) return 0;
	if(map[x][y] == NIL){
		if(X.at(x) == Y.at(y)){
			return findMaxLength(x-1,y-1) +1;
		}else{
			return max(findMaxLength(x-1,y),findMaxLength(x,y-1));
		}
	}else return map[x][y];
}

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

	for(int i=0;i<q;i++){
		cin >>X; cin >> Y;
		for(int i=0;i<X.size();i++){
			for(int j=0;j<Y.size();j++){
				map[i][j] = NIL;
			}
		}
	
		cout <<findMaxLength(X.size()-1,Y.size()-1) <<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>

void Print( const std::vector<std::vector<int>>& m ){
    int n = m.size();
    for( int j = n; j > 0; --j ){
        for( int i = 1; i < j; ++i ){
            std::cout << m[i][j] << "\t";
        }
        std::cout << "\n";
    }
}

void MatrixChainOrder( const std::vector<int>& p, std::vector<std::vector<int>>& m ){
    int n = p.size() - 1;
    for( int i = 1; i <= n; ++i ){
        m[i][i] = 0;
    }

    constexpr int INF = 1 << 25;
    int j;
    int q;
    for( int l = 2; l <= n; ++l ){
        for( int i = 1; i <= n-l+1; ++i ){
            j = i + l - 1;  
            m[i][j] = INF;
            for( int k = i; k <= j-1; ++k ){
                q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j];
                m[i][j] = ( m[i][j] < q )? m[i][j] : q;
            }
        }
    }
}

int main(){
    int n;
    std::cin.tie( 0 );
    std::cin >> n;

    std::vector<int> p( n+1 );
    std::vector<std::vector<int>> m( n+1 );
    for( auto& elem : m ){
        elem = std::vector<int>( n+1 );
    }

    int dump;
    for( int i = 0; i < n; ++i ){
        std::cin >> p[i] >> dump;
    }
    p[n] = dump;

    MatrixChainOrder( p, m );

    std::cout << m[1][n] << std::endl;

    //Print( m );

}

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

int main()
{
	int n, p = 0, m = 101, mini;
	int v[101];

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> v[i] >> v[i + 1];
		if (m > v[i]) {
			m = v[i];
			mini = i;
		}
	}

	for (int j = 0; j < n; j++)
		p += v[j] * v[j + 1];
	p += v[0] * v[n];

	if (mini == 0)
		p -= v[0] * v[1];
	else if (mini == n)
		p -= v[n - 1] * v[n];
	else
		p -= v[mini] * (v[mini + 1] + v[mini - 1]);

	p *= v[mini];

	cout << p << endl;

	return 0;
}

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

static const int N = 100;

int main(void){
    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;
}