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<climits>
using namespace std;
int m[100][100];

int MCM(int p[],int n) {
	for (int i = 0; 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] = INT_MAX;
			for (int k = i; k <= j; k++) {
				int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
				if (m[i][j] > q) {
					m[i][j] = q;
				}

			}
		}

	}
	return m[1][n];
}

int main() {
	int n, p[200];
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> p[i] >> p[i + 1];
	}
	cout << MCM(p, n) << endl;
	return 0;
}


int main() {
	int n, p[200];
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> p[2*i] >> p[2*i + 1];
	}
	cout << MCM(p, n) << endl;
	return 0;
}

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

int n;
int matrix[110][2];
int _dp[110][110];

int dp(int begin,int end){
	if(_dp[begin][end]) return _dp[begin][end];
	if(begin + 1 == end){
		_dp[begin][end] == 0;
		return 0;
	}
	if(begin + 2 == end){
		int temp = matrix[begin][0] * matrix[begin][1] * matrix[end-1][1];
		_dp[begin][end] = temp;
		return temp;
	}
	int min = INF;
	for(int i = begin+1; i<end; i++){
		int temp = dp(begin, i) + dp(i, end)
			+ matrix[begin][0] * matrix[i][0] * matrix[end-1][1];
		if(temp < min) min = temp;
	}
	_dp[begin][end] = min;
	return min;
}

int main(void){
	int n;
	cin >> n;
	
	for(int i=0; i<n; i++) cin >> matrix[i][0] >> matrix[i][1];
	
	cout << dp(0,n) << endl;
	
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 100

int main(){
  int n,dammy;
  cin>>n;
  int p[MAX+1];
  cin>>p[0];
  for(int i=0;i<n-1;i++){
    cin>>p[i+1];
    cin>>dammy;
  }
  cin>>p[n];
  int C[MAX+1][MAX+1];
  for(int i=1;i<=n;i++) C[i][i]=0;
  for(int h=2;h<=n;h++){
    for(int s=1;s<=n-h+1;s++){
      int e=s+h-1;
      C[s][e]=C[s][s]+C[s+1][e]+p[s-1]*p[s]*p[e];
      for(int j=s+1;j<e;j++){
        C[s][e]=min(C[s][e],C[s][j]+C[j+1][e]+p[s-1]*p[j]*p[e]);
      }
    }
  }
  cout<<C[1][n]<<endl;
}

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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


int main() {

    int n;
    cin >> n;
    vector<int> R(n);
    vector<int> C(n);
    REP(i,n) cin >> R[i] >> C[i];
    vector<vector<int>> dp(n,vector<int>(n,INF));

    REP(i,n) dp[i][i] = 0;

    FOR(w,1,n){
        FOR(l,0,n-w){
            int r = l+w; 
            FOR(m,l,r){
                dp[l][r] = min(dp[l][r], dp[l][m] + dp[m+1][r] + R[l]*C[m]*C[r]);
            }
        }

    }

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


    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
using namespace std;
  int n;

int m[101][101];
  int p[101];
void chain(){
  int n1;
  int j;
  int q;
  n1=n;
  //ok
  // cout <<"n1 "<<n1<<endl;
  for(int i=1;i<=n1;i++){
    m[i][i]=0;
  }
  for(int l=2;l<=n1;l++){
    for(int i=1;i<=n1-l+1;i++){
      j=i+l-1;
      m[i][j] = INT_MAX;
      for(int k=i;k<=j-1;k++){
	q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
	//	cout<<"q "<<q<<endl;
	if(q<m[i][j]){
	  m[i][j]=q;
	  //	  cout<<s[i][j]<<endl;
	}
      }
    }
  }
}

int main(){

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

  //ok
  /*
 for(int i=0;i<=n;i++){
   cout<<"p "<<p[i]<<endl;
 }
  */
  chain();
  /*  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout<<m[i][j]<<endl;
    }
   }
*/
  cout<<m[1][n]<<endl;

  return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>

using namespace std;
int N = 100;

int main(){
  int i,j,k,l;
  int n,p[N+1],m[N+1][N+1];
  cin >> n;
  for(i = 1; i <= n; i++){
    cin >> p[i-1];
    cin >> p[i];
  }
  for(i = 1; i <= n; i++){
    m[i][i] = 0;
  }
  for(l = 2; l <= n; l++){
    for(i = 1; i <= n-l+1;i++){
      j = i+l-1;
      m[i][j] = (1 << 21);
      for(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<iostream>
#include<algorithm>
using namespace std;
static const int N = 100;


int main() {
	int n;
	cin >> n;
	int p[N],m[N+1][N+1];
	for (int i = 1;i <= n;i++) {
		cin >> p[i - 1] >> p[i];
	}
	for (int i = 0;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<iostream>
#include<algorithm>
using namespace std;
static const int N=100;
int main(){
  int j,i,k,n,l,p[N+1],m[N+1][N+1];
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d%d",&p[i-1],&p[i]);
  }
  for(i=1;i<=n;i++) m[i][i]=0;
  for(l=2;l<=n;l++){
    for(i=1;i<=n-l+1;i++){
      j=i+l-1;
      m[i][j]=(1<<21);
	for(k=i;k<=j-1;k++){
	  m[i][j]=min(m[i][j],m[i][k]+m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
        }
    }
  }
  printf("%d\n",m[1][n]);
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
static const int MAX = 100;
#define INF (1 << 30);

int store[MAX][MAX] = {{}};

struct matrix {
  int row;
  int col;
};

int nMul(matrix x, matrix y) { return x.row * x.col * y.col; }

matrix mult(matrix x, matrix y) {
  matrix mat;
  mat.row = x.row;
  mat.col = y.col;
  return mat;
}

int main() {
  matrix mat[MAX];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> mat[i].row;
    cin >> mat[i].col;
  }

  int mcm[MAX][MAX] = {0};
  i         k         j
  for (int j = 1; j < n; j++) {
    for (int i = j-1; i >= 0; i--) {
      int minimum = INF;
      for (int k = i; k < j; k++) {
        int joint = nMul(mult(mat[i], mat[k]), mult(mat[k+1], mat[j]));
        int tmp = mcm[i][k] + mcm[k+1][j] + joint;
        minimum = min(minimum, tmp);
      }
      mcm[i][j] = minimum;
    }
  }

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