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 <bits/stdc++.h>
using namespace std;

const int INF = 1 << 30;
int n;
vector<int> r, c;
vector<vector<int>> dp;

int dfs(int a, int b) {
    if (dp[a][b] != INF) return dp[a][b];
    if (a + 1 == b) return dp[a][b] = 0;
    int res = INF;
    for (int k = a + 1; k < b; ++k) {
        res = min(res, dfs(a, k) + dfs(k, b) + r[a] * r[k] * c[b - 1]);
    }
    return dp[a][b] = res;
}

int main() {
    cin >> n;
    r.resize(n);
    c.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> r[i] >> c[i];
    }
    dp.resize(n + 1, vector<int>(n + 1, INF));
    cout << dfs(0, n) << endl;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <climits>
 
using namespace std;
 
int f(const int);
 
int main()
{
    int N;
    pair<int,int> data[100];
    vector<vector<int> > vdata(100,vector<int>(100,INT_MAX));
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>data[i].first>>data[i].second;
    }
    vdata[0][0]=0;
    for(int j=1;j<N;j++){
        vdata[j][j]=0;
        for(int i=j-1;i>=0;i--){
            for(int k=i+1;k<=j;k++){
                int c=vdata[i][k-1]+vdata[k][j]+data[i].first*data[k].first*data[j].second;
                if(c<vdata[i][j]){
                    vdata[i][j]=c;
                }
            }
        }
    }
    cout<<vdata[0][N-1]<<endl;
}

C++: #include <iostream>

using namespace std;

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

    int dp[101][101];
    int N[101];
    for (int i = 1; i <= n; ++i) {
        cin >> N[i- 1] >> N[i];
    }

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

    for (int len = 2; len <= n ; ++len) {
        for (int i = 1; i <= n - len + 1; ++i) {
            int j = i + len - 1;
            dp[i][j] = (1 << 21);
            for (int k = i; k <= j - 1; ++k) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + N[i - 1] * N[k] * N[j]);
            }
        }
    }

    cout << dp[1][n] << endl;
    return 0;
}

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

typedef long long lli;

const lli INF = 1LL << 59;
const int N = 1000;

int n;
vector<lli> v;

void solve(){
  lli dp[N][N];
  fill(dp[0],dp[N],INF);
  
  for(int i=0;i<=n;i++) dp[i][i] = 0;
  for(int i=2;i<=n;i++){
    for(int j=0;j+i-1<n;j++){
      int next = j + i - 1;
      for(int k=j;k<next;k++){
        dp[j][next] = min(dp[j][next], dp[j][k] + dp[k+1][next] + v[j] * v[k+1] * v[next+1]);
      }
    }
  }

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

int main(){
  while(cin >> n){
    v.clear();
    for(int i=0;i<n;i++) {
      int a,b;
      cin >> a >> b;
      if(i == 0) v.push_back(a);
      v.push_back(b);
    }
    solve();
  }
}

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

int n, a[100], v[101];
long sum, m = 1000000000;
bool f[100], g[100];

void permsum(int p) {
	if (p == n - 1) {
		sum = 0;
		memset(g, 0, 100);
		for (int i = 0; i < n - 1; i++) {
			g[a[i]] = 1;
			int u = 1, w = 1;
			while (g[a[i] - u])
				u++;
			while (g[a[i] + w])
				w++;
			sum += v[a[i] - u] * v[a[i]] * v[a[i] + w];			
		}
		if (m > sum)
			m = sum;
	}
	else {
		for (int j = 0; j < n - 1; j++) {
			if (!f[j]) {
				a[p] = j + 1;
				f[j] = 1;
				permsum(p + 1);
				f[j] = 0;
			}
		}
	}
}

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++)
		cin >> v[i] >> v[i + 1];

	permsum(0);

	cout << m << endl;

	return 0;
}