Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&a[i+1])
	}

	m := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		m[i] = make([]int, n+1)
	}

	for d := 2; d <= n; d++ {
		for s := 0; s+d <= n; s++ {
			min := 1 << 30
			for j := s+1; j < s+d; j++ {
				val := m[s][j] + m[j][s+d] + a[s]*a[j]*a[s+d]
				if val < min {
					min = val
				}
			}
			m[s][s+d] = min
		}
	}
	fmt.Println(m[0][n])
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<ll> r(n),c(n);
    for(int i = 0; i < n; i++){
        cin >> r[i] >> c[i];
    }
    ll dp[n+1][n+1];
    pair<ll,ll> rc[n+1][n+1];
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= n; j++){
            dp[i][j] = INF;
        }
    }

    for(int l = 1; l <= n; l++){
        for(int i = 0; i < n; i++){
            int j = i+l;
            if(j > n) break;
            if(i+1 == j){
                dp[i][j] = 0;
                rc[i][j] = make_pair(r[i],c[i]);
            }
            for(int mid = i; mid <= j; mid++){
                ll tmp = dp[i][j];
                dp[i][j] = min(dp[i][j], dp[i][mid]+dp[mid][j]+rc[i][mid].first*rc[mid][j].second*rc[i][mid].second);
                if(dp[i][j] != tmp){
                    rc[i][j] = make_pair(rc[i][mid].first,rc[mid][j].second);
                }
            }
        }
    }

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

    return 0;
}

C++: #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] + 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,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;
struct matrix{
	long long x,y;
}m[110];
int n;
long long dp[110][110];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&m[i].x,&m[i].y);
	}
	for(int i=1;i<=n;i++){
		dp[i][i]=0;
	}
	for(int l=2;l<=n;l++){
		for(int i=1;i<=n-l+1;i++){
			int j=i+l-1;
			dp[i][j]=1<<20;
			for(int k=i;k<j;k++){
			    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+m[i].x*m[k].y*m[j].y);
			}
		}
	}
	//cout<<"i: "<<i<<" j: "<<j<<" dp: "<<dp[i][j]<<endl; 
	printf("%lld\n",dp[1][n]);
	return 0;
}

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

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

	vector<int> size(n+1);
	int r,c;
	for (int i=0;i<n;++i)
	{
		cin >> r >> c;
		size[i]=r;
	}
	size[n]=c;

	vector<vector<int>> m(n+1,vector<int>(n+1));
	const int infinity=32768*32767;

	// length=2
	for (int i=0;i<=n-(2-1);++i)
	{
		m[i][i+1]=0;
	}

	// length=3
	for (int i=0;i<=n-(3-1);++i)
	{
		m[i][i+2]=size[i]*size[i+1]*size[i+2];
	}

	// length>=4
	for (int i=4;i<=n+1;++i)
	{
		for (int j=0;j<=n-(i-1);++j)
		{
			int minimum=infinity;
			for (int k=1;k<=i-2;++k)
			{
				int sum=m[j][j+k]+m[j+k][j+i-1]+size[j]*size[j+k]*size[j+i-1];
				minimum=min(minimum,sum);
			}
			m[j][j+i-1]=minimum;
		}
	}

	cout << m[0][n] << endl;
}

C++: #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 <iostream>
#include <climits>
using namespace std;

long int M[101][101];
long int a[1000];
int n;
void solve(){
  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++){
	        long int tmp = M[i][k]+M[k+1][j]+a[i-1]*a[k]*a[j];
		M[i][j] = min(M[i][j],tmp);	
	    }
	}
    }
}

int main(){ 
   cin>>n;
   int p, q;
   for(int i=0; i<n; i++){
    cin>>p>>q;
    if(i == 0){
      a[i] = p;
      a[i+1] = q;
    }else{
      a[i+1] = q;
    }
  }

//   for(int i=0;i<2*n;i++)
//	cout<<a[i]<<endl;
   solve();
// for(int i=0;i<=n;i++){
//	for(int j=0;j<=n;j++){
//	    cout<<"("<<i<<","<<j<<")"<<M[i][j]<<" ";
//	}
//	cout<<endl;
 //  }
   cout<<M[1][n]<<endl;
   return 0;
}

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

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;


int main() {
    int n;
    cin >> n;
    vector<P> ps(n);
    rep(i, n) {
        //y,x
        cin >> ps[i].first >> ps[i].second;
    }
    vector<vector<ll>> dp(n, vector<ll>(n, INF));
    rep(i, n) {
        dp[i][i] = 0;
    }
    for (int len = 1; len < n; len++) {
        for (int left = 0; left < n-len; left++) {
            int right = left + len;
            for (int middle = left; middle < right; middle++) {
                ll add1 = ps[left].first * ps[middle].second * ps[right].second;
                ll u1 = dp[left][middle] + dp[middle+1][right] + add1;
                dp[left][right] = min(dp[left][right], u1);
            }
        }
    }
    cout << dp[0][n - 1] << 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 << "0" << 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;
}