Go: package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)

	P := make([]int, n+1)
	for i := 1; i <= n; i++ {
		l := nextInt(sc)
		P[i-1] = l
		m := nextInt(sc)
		P[i] = m
	}

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

	for l := 2; l <= n; l++ {
		for i := 1; i <= n - l + 1; i++ {
			j := i + l - 1
			matrix[i][j] = (1 << 21)
			for k := i; k <= j-1; k++ {
				matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k+1][j] + P[i-1] * P[k] * P[j])
			}
		}
	}
	fmt.Println(matrix[1][n])
}

C++: #include <stdio.h>

#define NMAX 100
#define INF 200000000
#define min(a, b) (a < b ? a : b)

int n;
int m[NMAX + 10][NMAX + 10]; // 1-origin
int p[NMAX + 10];            // 1-origin, column number array

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d %d", &p[i - 1], &p[i]);
        m[i][i] = 0; // single matrix cost 0
    }
    // l: length of consecutive matrix
    for (int l = 2; l <= n; l++) {
        // i,j: consectutive matrix sequence [i, j]
        // j<=n,j-i+1=l --> j=l+i-1<=n --> i<=n-1+1
        for (int i = 1; i <= n - l + 1; i++) {
            int j = l + i - 1;
            // printf("i=%d,j=%d\n", i, j);
            m[i][j] = INF;
            // k: cut point [i,j] --> [i,k],[k+1,j]
            // k>=i,k+1<=j --> i<=k<=j-1
            for (int k = i; k <= j - 1; k++) {
                int val = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
                // printf("val=%d\n", val);
                if (val < m[i][j])
                    m[i][j] = val;
            }
        }
    }
    printf("%d\n", m[1][n]);
    return 0;
}

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

constexpr 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; 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<cstdio>
#include<iostream>
#include<vector>
using namespace std;
const int MAX_V = 100;
vector<pair<int,int> > pairs;
int main(){
  int n; scanf("%d",&n);
  for(int i=0;i<n;i++){
    pair<int,int> p;
    scanf("%d %d",&p.first,&p.second);
    pairs.push_back(p);
  }
  long long int sum=0;
  while(pairs.size()>1){
    vector<pair<int,int> >::iterator min_ptr;
    int min=0x7fffffff;
    for(auto iter=pairs.begin()+1;iter<pairs.end();iter++){
      int localMin = (*(iter-1)).first;
      if(localMin > (*iter).second) localMin = (*iter).second;
      if(min>localMin){
	min=localMin;
	min_ptr=iter;
      }
    }
    sum+=(*(min_ptr-1)).first*(*min_ptr).first*(*min_ptr).second;
    (*(min_ptr-1)).second=(*min_ptr).second;
    pairs.erase(min_ptr);
  }
  printf("%lld\n",sum);
}

C++: #include <iostream>

using namespace std;
#define MAX 101
int M[MAX][MAX],p[MAX];
int n;

int main()
{
    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<algorithm>
using namespace std;

int main()
{
	int n, i, j, l,k;
	int A[101];
	int q[100][100];
	cin >> n;
	for (i = 1; i < n; i++) {
		cin >> A[i - 1] >> A[i];
	}
	for (l = 2; l < n + 1; l++) {
		for (i = 1; i < n - l + 2; i++) {
			j = i + l - 1;
			for (k = i; k < j; k++) {
				q[i][j] = min(q[i][j], q[i][k] + q[k + 1][j] + A[i - 1] * A[k] * A[j]);
			}
		}
	}
	cout << q[1][n] << endl;

}

C++: #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
ll dp[101][101];
ll a[102];
ll solve(ll le,ll ri){
	if(dp[le][ri]!=-1)return dp[le][ri];
	ll num=1e18;
	for(int i=le+1;i<ri;i++){
		num=min(num,solve(le,i)+solve(i,ri)+a[le]*a[ri]*a[i]);
	}
	return dp[le][ri]=num;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<n;i++)dp[i][i+1]=0;
    for(int i=0;i<n;i++){
    	cin>>a[i]>>a[i+1];
    }
    cout << solve(0LL,n) <<endl;
    return 0;
}

C++: #include <stdio.h>
#define N 101

int m[N][N];
int p[N];

void matrixChainOrder(int n){
  int i,j,k,l ,q;

  for( l = 2 ; l <= n ; l++ ){
    for( i = 1 ; i < (n+l-1) ; i++){
      j = i + l - 1;
      m[i][j] = -1;
      for( k = i ; k <= (j-1) ; k++ ){
	q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
	if( m[i][j] == -1 || m[i][j] > q){
	  m[i][j] = q;
	}
      }
    }
  }

}


int main(){
  int n, i, j, k;
  int trash;


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

  matrixChainOrder(n);
  printf("%d\n" ,m[1][n]);

  return 0; // Don't forget return 0
}

C++: #include<stdio.h>

#define N 101

int main(){
  int n;
  int p[N];
  int m[N][N];
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    scanf("%d%d",&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;
      /*2<<21*/
      m[i][j]=2097152;
      for(int k=i;k<=j-1;k++){
	/*さいしょう比較*/
	if(m[i][j]<m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j])
	
	  m[i][j]=m[i][j];
	else
	  m[i][j]=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
      }
    }
  }
  printf("%d\n",m[1][n]);
  
  return 0;


  }

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using qi = queue<int>;
using Graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)

ll dp(int* r, int* c, int n) {
    ll table[n][n];
    REP(i, n) table[i][i] = 0;
    FOR(i, 1, n) {
        REP(j, n-i) {
            table[j][j+i] = table[j][j+i-1] + r[j]*c[j+i-1]*c[j+i];
            FOR(k, j, j+i) {
                table[j][j+i] = min(table[j][k] + r[j]*c[k]*c[j+i] + table[k+1][j+i], table[j][j+i]);
            }
        }
    }
    return table[0][n-1];
}

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

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

    // a に含まれる n 個の値で k を作ったときの最大値は何か
    int table = dp(r, c, n);

    cout << table << "\n";
}