Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	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
			//kまでで区切る
			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])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[1][n])
}

C++: #include<iostream>
#include<algorithm>
using namescape 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;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>
using namespace std;
#define INF 10000000

int M[101][101],p[101];

int main() {
  int n; 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] = INF;
      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 <cstring>
#include <climits>
#include <algorithm>

using namespace std;

int r[100], c[100];
int dp[100][100];

int optMat(int left, int right)
{
	if (dp[left][right] >= 0){
		return (dp[left][right]);
	}
	if (left == right){
		return (0);
	}
	
	int res = INT_MAX;
	for (int i = left; i < right; i++){
		res = min(res, optMat(left, i) + optMat(i + 1, right) + r[left] * c[i] * c[right]);
	}
	
	return (dp[left][right] = res);
}

int main()
{
	int N;
	
	scanf("%d", &N);
	
	for (int i = 0; i < N; i++){
		scanf("%d %d", &r[i], &c[i]);
	}
	
	memset(dp, -1, sizeof(dp));
	
	printf("%d\n", optMat(0, N - 1));
	
	return (0);
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
using namespace std;

struct Matrix{int r,c;};

int main(){
	int n;
	int i,k,l,f;
	cin >> n;
	vector<Matrix> M(n);
	
	for (i=0;i<n;i++){
		cin >> M[i].r;
		cin >> M[i].c;
	}
	
	int DPtable[n][n];
	for (i=0;i<n;i++){
		DPtable[i][i]=0;
	}
	
	for (k=1;k<n;k++){
		for (i=0;i<n-k;i++){
			int m=INT_MAX;
			for (l=0;l<=k-1;l++){
				f=M[i].r*M[i+l].c*M[i+k].c;
				if (DPtable[i][i+l]+DPtable[i+l+1][i+k]+f<m){
					m=DPtable[i][i+l]+DPtable[i+l+1][i+k]+f;
				}
			}
			DPtable[i][i+k]=m;
			
		}
	}
	
	cout << DPtable[0][n-1] <<endl;
	
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 100

struct Matrix {int r, c; };

int main(){
    int n; cin >> n;
    struct Matrix M[MAXN];
    for(int i=0; i<n; ++i){
        cin >> M[i].r >> M[i].c;
    }
    
    int dp[MAXN+1][MAXN];
    for(int i=0; i<n; ++i){
        dp[1][i] = 0;
    }
    for(int m=2; m<=n; ++m){
        for(int i=0; i<=n-m; ++i){
            priority_queue<int, vector<int>, greater<int> > pq;
            for(int j=1; j<m; ++j){
                //cout << "here with m= " << m << " i = " << i << " j = " << j << endl;
                int x = dp[j][i] + dp[m-j][i+j] + M[i].r*M[i+j].r*M[m+i-1].c;
                pq.push(x);
            }
            dp[m][i] = pq.top();
        }
    }
    cout << dp[n][0] << endl;
}

C++: // #include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <set>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i, n) for(int i=0;i<n;i++)
#define irep(i, n) for(int i=n-1;i>=0;i--)
#define lrep(i, n) for(long long i=0;i<n;i++)
typedef long long ll;
typedef vector<long long> llv;
typedef vector<vector<long long> > llvv;

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;
}

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

#define INFTY (1<<21)
#define MAX_N 100

int m[MAX_N + 1][MAX_N + 1], p[MAX_N + 1];
int n;

void matrixChainMultiplication(){
	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] = 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]);
			}
		}
	}
}

int main(void){
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> p[i - 1] >> p[i];
	}

	matrixChainMultiplication();

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

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <list>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int main() {

	int n, p[101], m[101][101];

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> p[i - 1] >> p[i];
		m[i][i] = 0;
	}

	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= n - i + 1; j++) {
			int k = i + j - 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;
	return 0;
}

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

#define MAX ((int)110)
#define endl '\n'

using namespace std;

typedef long long ll;

int n, m[ MAX ];
ll DP[ MAX ][ MAX ];

ll solve(){
	int j;
	ll cost;
	for(int len = 2 ; len <= n ; len++){
		for(int i = 1 ; i <= n - len + 1 ; i++){
			j = i + len - 1;
			DP[ i ][ j ] = LONG_LONG_MAX;
			for(int k = i ; k < j ; k++){
				cost = DP[ i ][ k ] + DP[ k + 1 ][ j ] + m[ i - 1 ] * m[ k ] * m[ j ];
				DP[ i ][ j ] = min(DP[ i ][ j ], cost);
			}
		}
	}
	return DP[ 1 ][ n ];
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

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