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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
//#define ll long long
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 (a>b) { a=b; return 1; } return 0; }
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define all(x) (x).begin(),(x).end()
ll t1,t2,t3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;

int main(){
  ll n;
  cin >> n;
  vector<ll> vec(n+1,0);
  vector<vector<ll>> mul(n+1,vector<ll>(n+1,100000));
  for(int i = 1;i <= n;i++){
    mul[i][i] = 0;
  }
  rep(i,n){
    cin >> t1 >> t2;
    vec[i] = t1;
    vec[i+1] = t2;
  }
  for(int l = 2;l <= n;l++){
    for(int i = 1;i <= n-l+1;i++){
      int j = i + l - 1;
      mul[i][j] = (1 << 21);
      for(int k = i;k <= j-1;k++){
        chmin(mul[i][j],mul[i][k] + mul[k+1][j] + vec[i-1] * vec[k] * vec[j]);
      }
    }
  }
  cout << mul[1][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 <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

template <typename T>
using vector2 = vector<vector<T>>;
template <typename T>
vector2<T> initVec2(size_t n0, size_t n1, T e = T()){
    return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T>
using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()){
    return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

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

    int n;
    cin >> n;
    vector<int> r(n), c(n);
    rep(i, n) cin >> r[i] >> c[i];

    auto dp = initVec2<int>(n, n, INF);
    rep(i, n){
        dp[i][i] = 0;
    }
    for(int l = 1; l < n; l++){
        rep(i, n){
            int j = i + l;
            if(j >= n){
                continue;
            }
            for(int k = i; k <= j; k++){
                dp[i][j] = min(dp[i][j], dp[i][k-1] + dp[k][j] + r[i] * r[k] * c[j]);
            }
        }
    }
    cout << dp[0][n-1] << endl;

    return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;

template<typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;

template<typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;

template<typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define REP2(i,s,e) for(int i=(int)(s);i<(int)(e);++i)
#define RREP(i,s,e) for(int i=(int)(s);i>=(int)(e);--i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin(),ie=(c).end();i!=ie;++i)
#define ALL(c) (c).begin(), (c).end()

const double PI = acos(-1);
const ll MOD = 1e9+7;

int n;
string s;

struct X {
    int r, c;
    ll cost;
};

int main() {
    cin >> n;
    V<Pii> v(n);
    REP(i, n) {
        int r, c;
        cin >> r >> c;
        v[i] = {r, c};
    }

    V<V<X>> dp(n, V<X>(n));
    REP(i, n) {
        X x = {v[i].first, v[i].second, 0LL};
        dp[i][i] = x;
    }
    REP2(k, 1, n) {
        REP(i, n-k) {
            int R = dp[i][i].r, C = dp[i+k][i+k].c;
            X x = {R, C, (ll)1e18};
            REP2(j, i, i+k) {
                ll cost = dp[i][j].cost + dp[j+1][i+k].cost + R*C*dp[i][j].c;
                if (x.cost > cost) {
                    x.cost = cost;
                }
            }
            dp[i][i+k] = x;
        }
    }
    std::cout << dp[0][n-1].cost << '\n';

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int N;

struct Matrix{ int R, W; };
Matrix M[MAX_N];

int mem[MAX_L][MAX_L];
Matrix memM[MAX_L][MAX_L];


void eval(Matrix M[], int s, int e){

	if (s == e){
		mem[s][e] = 0;
		memM[s][e] = M[s];
		return;
	}

	int minval = 1000000000;
	for (int mid = s; mid < e; mid++){
		Matrix m1 = memM[s][mid];
		Matrix m2 = memM[mid+1][e];
		int val = mem[s][mid] + mem[mid+1][e] + (m1.W * m1.R * m2.W);
		
		if (val < minval){
			minval = val;
			mem[s][e] = val;
			Matrix m; m.R = m1.R; m.W = m2.W;
			memM[s][e] = m;
		}
	}

}

int solve(Matrix M[], int N){

	for (int d = 0; d <= N; d++)
		for (int s = 0; s + d <= N; s++)
		{
			eval(M, s, s + d);
		}

	return mem[0][N];
}

int main() {

	cin >> N;
	rep(i, N){
		Matrix m;
		cin >> m.R;
		cin >> m.W;
		M[i] = m;
	}

	cout << solve(M, N) << endl;

#ifdef DEBUGRUN
	rep (i, N){
		rep(j, N){
			cerr << mem[i][j] << ' ';
		}
		cerr << endl;
	}
#endif

}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
 
 
int main()
{
    int n;
    cin >> n;
    vector<int> p(n + 1);
    for(int i = 0; i <= n; i++){
        int a,b;
        cin >> a >> b;
        if(!i) {
         p[i] = a;
         i++;
        }
        p[i] = b;
    }
    
    vector<vector<int>> m(n + 1, vector<int>(n + 1, 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 MOD 1000000007

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

 vector<long long> p(n+1);
 for(int i = 1;i <= n;i++) {
   cin >> p.at(i-1) >> p.at(i);
 }

 vector<vector<long long> > dp(n+1,vector<long long>(n+1,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<<21);
     for(int k = i;k <= j-1;k++) {
       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;
 
 
  
}