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 <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n+1); ++i)
using namespace std;
using ll = long long;
const int INF = +100100100;
// typedef pair<int,int> P;

vector<int>P;

int dp(int n){
    vector<vector<int>> D(n,vector<int>(n));
    rep(j,n){
        for(int i = j;i>=0;--i){
            if(i==j)D[i][j]=0;
            else{
                int mini = INF;
                for(int k = i;k<j;++k){
                    int rel = D[i][k]+D[k+1][j]+P[i]*P[k+1]*P[j+1];
                    mini = min(mini,rel);
                }
                D[i][j]=mini;
            }
        }
    }
    return D[0][n-1];
}

int main(){
    int n;
    cin>>n;
    rep(i,n){
        int x,y;cin >> x>>y;
        if(i==0){
            P.push_back(x);
            P.push_back(y);
        }
        else{
            P.push_back(y);
        }
    }
    cout << dp(n) << endl;
    return 0;
}

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

const int INF = INT_MAX/3;
typedef vector<int> vi;
typedef pair<int,int> pii;

int main(){
    int n;cin>>n;
    vector<pii> p(n+1);
    for(int i=0;i<n;i++) cin>>p[i].first>>p[i].second;

    vector<vi> mat(n+1,vi(n+1,INF));    //mat[i][j] : Mi*...*Mj???????°??????°
    for(int i=0;i<n;i++) mat[i][i] = 0;

    for(int sz=2;sz<=n;sz++){
        /*
         * mat[i][j] = min( mat[i][k]+mat[k+1][j]+Mi.first*Mk+1.first*Mk+1.second )
         */
      //  cout << "size = " << sz << endl;
        for(int i=0;i<=n-sz;i++){

            int j = i + sz - 1;

       //     cout << i << " : " << j << endl;

            for(int k=i;k<=j-1;k++){
        //        cout << i << ":" << j << ":" << mat[i][k] << ":" << mat[k+1][j] << endl;
                int tmp = mat[i][k] + mat[k+1][j] + p[i].first * p[k+1].first * p[j].second;
                //cout << tmp << endl;
                mat[i][j] = min(mat[i][j] , mat[i][k] + mat[k+1][j] + p[i].first * p[k+1].first * p[j].second);
            }
        }
    }

    /*
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout << mat[i][j] << "\t";
        }
        cout << endl;
    }
    */

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,s,n) for(int i = (s); i < (n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
  
  int n;
  cin >> n;
  int dp[n+1][n+1];
  rep(i, 1, n+1) dp[i][i] = 0;
  int p[n+1];
  rep(i, 0, n) cin >> p[i] >> p[i+1];
  
  rep(l, 2, n+1) {
    rep(i, 1, n-l+2) {
      int j = i+l-1;
      dp[i][j] = 1000000007;
      rep(k, i, j) {
        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] << "\n";
}

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 <vector>
#include <limits>

using namespace std;

// M1, M2, M3, ... , Mn-1, Mn
// M[i,j] = 0 if i = j
// M[i,j] = min { M[i,j] = M[i,k] + M[k+1,j] + p_{i-1} * p_k * p_j } where i <= k < j

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

    vector<int> P(n+1);
    for (int i = 1; i < n+1; ++i) {
        cin >> P[i-1] >> P[i];
    }

    int M[n+1][n+1];
    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] = std::numeric_limits<int>::max();
            for (int k = i; k <= j - 1; ++k) {
                int q = M[i][k] + M[k+1][j] + P[i-1] * P[k] * P[j];
                M[i][j] = min(M[i][j], q);
            }
        }
    }
    cout << M[1][n] << endl;

    return 0;
}

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

int MatrixChainMaltiplication(int *p, int n) {
    int m[n+1][n+1];
    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]);
            }
        }
    }
    return m[1][n];
}

int
main()
{
    int n;
    scanf("%d", &n);
    int p[n+1];
    for (int i=0; i<n; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        if (i == 0) p[i] = a;
        p[i+1] = b;
    }
    printf("%d\n", MatrixChainMaltiplication(p, n));
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long llong;
#define MAX 100
#define INFTY 9999999

// 行列群を保持しておく
vector<int> vecMtr(MAX);
vector<vector<int>> dp(MAX,vector<int>(MAX));

int
MultipleMatrixChain(int size)
{
  for (int i = 1; i < size; i++)
  {
    dp[i][i] = 0;
  }
  
  for (int l = 2; l <= size; l++)
  {
    for (int i = 1; i <= size-l+1; i++)
    {
      int j = i + l - 1;
      dp[i][j] = INFTY;
      for (int k = i; k <= j-1; k++)
      {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]+ vecMtr[i-1]*vecMtr[k]*vecMtr[j]);
      }
    }
  }
  return dp[1][size];
}

int main() {
  int n;
  cin >> n;
  
  int r = 0;
  int c = 0;

  for (int i = 1; i <= n; i++)
  {
      cin >> r >> c;
      vecMtr[i-1] = r;
      vecMtr[i] = c;
  }
  
  cout << MultipleMatrixChain(n) << "\n";
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, r[100], c[100], dp[100][101];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d%d", &r[i], &c[i]), dp[i][i] = 0;
	for(int i = 1; i < n; i++) {
		for(int j = 0; i + j <= n; j++) {
			dp[j][i + j] = min(dp[j][i + j - 1], dp[j + 1][i + j]) + r[j] * c[i + j - 1];
		}
	}
	printf("%d\n", dp[0][n]);
	return 0;
}