Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

type matrixSize struct{ row, col int }

func calcNum(i, j matrixSize) int {
	return i.row * j.col * i.col
}

func parseProblem() (int, []matrixSize) {
	n := scanInt(sc)
	sizes := make([]matrixSize, n)
	for i := 0; i < n; i++ {
		sizes[i] = matrixSize{row: scanInt(sc), col: scanInt(sc)}
	}
	return n, sizes
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = math.MaxInt64
		}
	}
}

func resolve(n int, sizes []matrixSize) int {
	// TODO 復習
	initDP(n)
	_rec(sizes, 0, n)
	return dp[0][n]
}

func _rec(sizes []matrixSize, l, r int) int {
	if dp[l][r] != math.MaxInt64 {
		return dp[l][r]
	}
	if r-l <= 1 {
		dp[l][r] = 0
		return 0
	}
	num := math.MaxInt64
	for i := l + 1; i < r; i++ {
		v := _rec(sizes, l, i) + _rec(sizes, i, r) + calcNum(matrixSize{sizes[l].row, sizes[i].row}, matrixSize{sizes[i].row, sizes[r-1].col})
		if v < num {
			num = v
		}
	}
	dp[l][r] = num
	return dp[l][r]
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: // ?????¬???????????¬??????

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N;
int R[110], C[110];

// [i, j) ???????????????????????°????°?
int dp[110][110];

signed main() {
    cin >> N;
    rep(i,0,N) cin >> R[i] >> C[i];
    repq(i,0,N) {
        repq(j,0,N) {
            if(i < j && j-i != 1) {
                dp[i][j] = dp[i][j-1] + R[i] * R[j-1] * C[j-1];
                // printf("dp[%lld][%lld] = %lld\n", i, j, dp[i][j]);
            }
        }
    }

    repq(i,3,N) {
        rep(j,0,N-i+1) {
            rep(k,j+1,j+i) {
                chmin(dp[j][j+i], 
                      dp[j][k] + dp[k][j+i] + R[j] * R[k] * C[j+i-1]);
            }
        }
    }
    cout << dp[0][N] << endl;
    return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

typedef struct matrix{
  int row;
  int column;
  struct matrix *next;
  struct matrix *back; 
}Matrix;

Matrix *push_back(Matrix *begin, int row, int column) {
  Matrix *p = (Matrix*)malloc(sizeof(Matrix));
  p->row = row;
  p->column = column;
  p->next = NULL;
  p->back = NULL;
  if(begin == NULL) return p;
  Matrix *p2 = begin;
  while(p2->next != NULL) p2 = p2->next;
  p2->next = p;
  p->back = p2;
  return begin;
}

Matrix *product_count(Matrix *x, int type, long *count) {
  Matrix *a,*b;
  if(type == 0) {
    a = x->back;
    b = x;
  }
  else {
    a = x;
    b = a->next;
  }
  *count = 0;
  if(a == NULL || b == NULL) return x;
  *count += a->row * a->column * b->column;
  Matrix *c = (Matrix*)malloc(sizeof(Matrix));
  c->row = a->row;
  c->column = b->column;
  c->next = b->next;
  c->back = a->back;
  if(a->back != NULL) a->back->next = c;
  if(b->next != NULL) b->next->back = c;
  free(a);
  free(b);
  return c;
}

int main()
{
  int i,n,r,c;
  Matrix *begin = NULL;
  scanf("%d",&n);
  for (i = 0; i < n; i++) {
    scanf("%d %d",&r,&c);
    begin = push_back(begin,r,c);
  }
  Matrix *min = begin, *p = begin;
  while (p->next != NULL) {
    if(min->column > p->column) min = p;
    p = p->next;
  }
  long sum = 0,count;
  if(min->column <= begin->row) {
    begin = NULL;
    while(1) {
      min = product_count(min,0,&count);
      if(count != 0) sum += count;
      else break;
    }
    if(min->next != NULL) {
      min = min->next;
      while(1) {
	min = product_count(min,1,&count);
	if(count != 0) sum += count;
	else break;
      }
      min = product_count(min,0,&count);
      sum += count;
    }
  }
  else{
    min = begin;
    begin = NULL;
    while(1) {
      min = product_count(min,1,&count);
      if(count != 0) sum += count;
      else break;
    }
  }
  cout << sum << endl;
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

template <typename T>
void printlnVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << endl;
  }
}

template <typename T>
void printVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << " ";
  }
  cout << endl;
}

long long calcurate_cost(pair<long long, long long> M1, pair<long long, long long> M2)
{
  return M1.first * M1.second * M2.second;
}

pair<long long, long long> product_M(pair<long long, long long> M1, pair<long long, long long> M2)
{
  return make_pair(M1.first, M2.second);
}

int main()
{
  int n;
  cin >> n;
  vector<long long> M(n + 1);
  rep(i, n)
  {
    cin >> M[i] >> M[i + 1];
  }

  vector<vector<long long>> dp(n, vector<long long>(n, INT64_MAX));

  rep(i, n)
  {
    dp[i][i] = 0;
  }
  rep(i, n - 1)
  {
    dp.at(i).at(i + 1) = M[i] * M[i + 1] * M[i + 2];
  }

  for(int step = 2; step < n; step++)
  {
    for(int l = 0; l + step < n; l++)
    {
      int r = l + step;

      for(int c = l; c < r; c++)
      {
        long long cost = M[l] * M[c + 1] * M[r + 1];
        dp[l][r] = min(dp[l][r], dp[l][c] + dp[c + 1][r] + cost);
      }
    }
  }

  long long ans = dp[0][n - 1];
  cout << ans << endl;
  return 0;
}