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;

#define FOR(i,s,e) for(int (i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)
#define RFOR(i,e,s) for(int (i)=(e)-1;(i)>=(int)(s);(i)--)
#define RREP(i,e) RFOR(i,e,0)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;
typedef priority_queue<int> PQI;
typedef priority_queue<PII> PQII;

const double EPS = 1e-10;
const int INF = 1e9;
const int N = 100;
int n;
int r[N], c[N];
int dp[N+1][N];

int main() {
  scanf("%d ", &n);
  REP(i,n) scanf("%d%d ", r+i, c+i);

  REP(i,n+1) REP(j,n) dp[i][j] = INF;
  REP(j,n) dp[1][j] = 0;
  FOR(i,2,n+1) REP(j,n-i+1) FOR(k,1,i)
    dp[i][j] = min(dp[i][j], dp[k][j] + r[j]*c[j+k-1]*c[i+j-1] + dp[i-k][j+k]);

  printf("%d\n", dp[n][0]);

  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];
    int i, l, j, k;

    cin >> n;
    for (i = 1; i <= n; i++) {
        cin >> 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[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 FOR(i,a,b) for(int i=a;i<b;++i)
#define REP(i,n) FOR(i,0,n)

const int MAX_N = 100;
const int INF = 0x3f3f3f3f;

int main() {
  int n, p[MAX_N + 1], m[MAX_N + 1][MAX_N + 1] = {0}; 

  cin >> n;
  REP(i, n) cin >> p[i] >> p[i + 1];

  REP(i, n) REP(l, n - i) {
    if (i == 0) continue;
    int r = l + i;
    m[l][r] = INF;
    FOR(j, l, r) m[l][r] = min(m[l][r], m[l][j] + m[j + 1][r] + p[l] * p[j + 1] * p[r + 1]);
  }
  cout << m[0][n - 1] << endl;

  return 0;
}

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

int r[105];
int c[105];
int cache[105][105];

int f(int a, int b) {
    if (a == b) return 0;
    if (cache[a][b] == -1) {
        cache[a][b] = 2000000000;
        for (int i = a; i < b; i++) {
            int res = f(a, i) + f(i + 1, b) + r[a] * c[i] * c[b];
            if (res < cache[a][b]) cache[a][b] = res;
        }
    }
    return cache[a][b];
}

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> r[i];
        cin >> c[i];
    }
    memset(cache, -1, sizeof(cache));
    cout << f(0, n-1) << endl;
}

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

int memo[110][100];
int mat[110][2];

int dfs(int l, int r) {
    if (l + 1 >= r) return 0;
    if (memo[l][r]) return memo[l][r];

    int mi = 1000000007;
    for (int i = l + 1; i < r; ++i) {
        mi = min(mi, dfs(l, i) + dfs(i, r) + mat[l][0] * mat[i][0] * mat[r - 1][1]);
    }
    return memo[l][r] = mi;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> mat[i][0] >> mat[i][1];
    }
    cout << dfs(0, n) << "\n";
    return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<stack>
#include<queue>
#include<sstream>
#include<string>
#include<set>
#include<array>
#include<cassert>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i);

using namespace std;

const int MAX=101;
const int INF=1<<29;

int dp[MAX][MAX];
int n;
int c[MAX];
int r[MAX];

int main(){

	fill_n((int *)dp,sizeof(dp)/sizeof(int),INF);
	fill_n((int *)r,sizeof(r)/sizeof(int),0);
	fill_n((int *)c,sizeof(c)/sizeof(int),0);

	cin >> n;

	rep(i,n){
		cin >> r[i] >> c[i];	
	}

	rep(i,n){
		dp[i][i+1] = 0;
	}

	for(int R=2;R<=n;R++){
		for(int L=0;L+R-1<n;L++){
			for(int M=L+1;M<=L+R-1;M++){
				dp[L][L+R] = min(dp[L][L+R],dp[L][M] + dp[M][L+R] + (r[L] * c[M-1] * c[L+R-1]));
			}
		}
	}

	cout << dp[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[i][j] << endl;
			}
		}
	}

	cout << m[1][n] << endl;

	return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
  int i, j, n, k, m[101], dp[101][101], s, t;
  
  scanf("%d", &n);
  for (i=0; i<n*2; i++)
    scanf("%d", &m[i]);  

  for (i=0; i<n; i++)
    for (j=0; j<n; j++)
      dp[i][j]=0;
 
  for (k=2; k<n; k++) {
    for (i=1; i<n-k+1; i++) {
     j=i+k-1;
     for (s=i; s<i; s++)
       dp[i][j]=m[s]*dp[i][j];
     for(t=0; t<j-1; t++)
       dp[i][j]=min(dp[i][j], dp[i][k]+dp[k+1][j]+m[i-1]*m[t]*m[j]);
    }
  }
  printf("%d",dp[1][n]);
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, s, n) for(int i = s; i < n; i++)
using namespace std;

vector<int> r(100), c(100);
vector<vector<int>> dp(100, vector<int>(100, -1));
int rec(int left, int right){
  if (right - left == 0) return 0;
  if (right - left == 1) return r[left] * r[right] * c[right];
  if (dp[left][right] != -1) return dp[left][right];
  int min;
  rep2(mid, left, right){
    min = rec(left, mid) + rec(mid+1, right) + r[left] * c[mid] * c[right];
    if (dp[left][right] == -1 || dp[left][right] > min) dp[left][right] = min;
  }
  return dp[left][right];
}

int main(){
  int n;
  cin >> n;
  rep(i, n) cin >> r[i] >> c[i];
  int ans = rec(0, n-1);
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
static const int INFTY = (1 << 21);

int main() {
    int n;
    cin >> n;
    int p[n + 1], m[n + 1][n + 1];
    for (int i = 1; i <= n; i++) {
        cin >> p[i - 1] >> p[n];
        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] = INFTY;
            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 <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
	int INFTY = 1000000000;

	int n;
	scanf("%d", &n);

	int p[n + 1];
	for (int i = 0; i < n; i++) {
		scanf("%d %d", p + i, p + i + 1);
	}

	int m[n + 1][n + 1];

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			m[i][j] = 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] = INFTY;
			for (int k = i; k < j; ++k) {
				m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
				cout << i << ":" << j << "," << m[i][j] << endl;
			}
		}
	}
	cout << m[1][n] << endl;

	return 0;
}