Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func main() {
	sc.Split(bufio.ScanWords)
	var n int
	var M [101][101]int

	n = nextInt()

	P := make([]int, n*2)

	for i := 1; i <= n; i++ {
		P[i-1] = nextInt()
		P[i] = nextInt()
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			M[i][j] = 1000000
			for k := i; k <= j-1; k++ {
				M[i][j] = int(math.Min(float64(M[i][j]), float64(M[i][k]+M[k+1][j]+P[i-1]*P[k]*P[j])))
			}
		}
	}
	fmt.Println(M[1][n])
}

C++: #include<stdio.h>

int matrixChainOrder(int *p);
int m[101][101];
int n1;
int main(){
  
  int i,r,c,min;
  int p[100];
  scanf("%d",&n1);

  for(i=0;i<n1;i++){
    scanf("%d %d",&r,&c);
    if(i==n1-1) p[n1]=c;
    p[i]=r;
  }

  min = matrixChainOrder(p);
  printf("%d\n",min);

  return 0;
}

int matrixChainOrder(int *p){
  int n2,l,i,j,q,k;
  
  n2 = n1 - 1;
  for(i=1;i<=n2;i++){
    m[i][i] = 0;
  }
  for(l=2;l<=n2;l++){ 
    for(i=1;i<=n2-l+1;i++){ 
      j = i+l-1;
      m[i][j] = 1.0/0.0;
      for(k=i;k<j;k++){
	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[i][j];
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <cmath>
#include <string>
#include <cstdio>
using namespace std;
typedef long long int ll;
static int const 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 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 - 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;
}

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

static const int N = 100;

int main() {
	int n;
	int 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 = 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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
int n;
vector<ll> r, c;
auto memo = make_vec<ll>(110, 110);
ll dp(int i, int j) {
    if (memo[i][j] != -1) return memo[i][j];
    if (i == j) return memo[i][j] = 0;
    ll ret = LINF;
    for (int k = i; k < j; k++) {
        chmin(ret, dp(i, k) + dp(k + 1, j) + r[i] * c[k] * c[j]);
    }
    return memo[i][j] = ret;
}
int main() {
    cin >> n;
    r.resize(n);
    c.resize(n);
    rep(i, n) cin >> r[i] >> c[i];
    rep(i, 110) rep(j, 110) memo[i][j] = -1;
    cout << dp(0, n - 1) << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define chmax(a,b) a=max(a,b)
#define chmin(a,b) a=min(a,b)
#define INF 100100100100100

int main(){

    long long n; cin >> n;
    vector<long long> r(n);
    vector<long long> c(n);
    for(long long i = 0; i < n; i++){
        cin >> r[i] >> c[i];
    }

    vector<vector<long long> > dp(n + 1, vector<long long> (n + 1, INF));
    for(int i = 0; i < n; i++) dp[i][i + 1] = 0;
    
    for(int k = 2; k <= n; k++){
        for(int i = 0; i < n; i++){
            for(int j = 1; j < k; j++){
                if(i + k <= n){
                    chmin(dp[i][i + k], dp[i][i + j] + dp[i + j][i + k] + r[i] * c[i + j - 1] * c[i + k - 1]);
                }
            }
        }
    }

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

C++: #include <iostream>
#include <cstdlib>
#include <climits>

/*
 * http://ttrsq.exblog.jp/24318687/
 */

const int MAX_N = 100;
int DP[MAX_N + 1][MAX_N + 1] = {0};
int *p;

int best(int r, int c) {
  int min = INT_MAX;
  int num = c - r;

  for (int i = 0; i < num; i++) {
    min = std::min(min, DP[r][r + i] + DP[r + i + 1][c] + p[r - 1] * p[r + i] * p[c]);
  }
  
  return min;
}

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

  int n = 0;
  std::cin >> n;

  if (n == 1) {
    std::cout << "1" << std::endl;
  }
  
  p = (int *) std::malloc(sizeof(int) * n + 1);

  int bb = 0;

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

  for (int i = 1; i < n; i++) {
    DP[i][i + 1] = p[i - 1] * p[i] * p[i + 1];
  }

  for (int j = 0; j <= n - 3; j++) {
    for (int i = j + 3; i <= n; i++) {
      DP[i - 2 - j][i] = best(i - 2 - j, i);
    }
  }

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

  return 0;
}

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

static const in N = 100;

int main(){
  int j,i,l,n,p[N+1],m[N+1][N+1];
  scanf("%d",&n);

  for(i = 0;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(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;
#define int long long
int n,dp[111][111],p[111];

int rec(int i,int j){
  int Min=2*1e9;
  for(int k=0;k<i-1;k++){
    Min=min(Min,dp[j+1][j+k+1]+dp[j+k+2][j+i]+p[j]*p[j+k+1]*p[j+i]);
  }
  return Min;
}

signed main(){
  cin>>n;
  cin>>p[0]>>p[1];
  for(int i=2;i<=n;i++){
    cin>>p[i]>>p[i];
  }
  for(int i=0;i<n;i++){
    dp[i][i]=0;
    if(i)dp[i][i+1]=p[i-1]*p[i]*p[i+1];
  }

  for(int i=3;i<=n;i++){
    for(int j=0;j+i<=n;j++){
      dp[j+1][j+i]=rec(i,j);
    }
  }
  cout<<dp[1][n]<<endl;
  return 0;
}

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

#define N_MAX 100
#define C_MAX 100
#define R_MAX 100

int p[N_MAX+1];
int m[R_MAX+1][C_MAX+1];


int chain_multiplication(int n)
{
    for (int i = 0; i < n; i++) {
        m[i][i] = 0;
    }
    for (int l = 1; l < n; l++) {
        for (int i = 0; i < n - l; i++) {
            int j = i + l;
            m[i][j] = 100000000;
            for ( int k = i ; k < j; k++) {
                int a = m[i][k] + m[k+1][j] + p[i] * p[k+1] * p[j+1];
//                printf("i %d k %d j %d a %d min %d\n", i, k, j, a, m[i][j]);
                if (m[i][j] > a) {
                    m[i][j] = a;
                }
            }
//            printf("m[%d][%d] = %d\n", i, j, m[i][j]);
        }
    }
    return m[0][n-1];
}

int main() {
    int n;

    cin >> n;

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