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++: /**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <cstdint>
#include <iostream>
#include <string>
#include <algorithm>

constexpr int32_t kMaxNumberOfMatrices = 100;
constexpr int32_t kMaxNumberOfRowsAndCols = 100;

void CallMatrixChainMultiplication(std::istream &input_stream);

struct Matrix {
  int32_t number_of_rows{0};
  int32_t number_of_columns{0};
};

struct LookUp {
  bool calculated{false};
  int32_t mcm{INT32_MAX};
};

class MatrixChainMultiplication {
 public:
  MatrixChainMultiplication() noexcept;
  ~MatrixChainMultiplication() noexcept;

  void AddMatrix(const int32_t number_of_rows, const int32_t number_of_columns);
  int32_t CalculateMinMultiplyCount();

  static int32_t GetMultiplyCount(const Matrix &a, const Matrix &b);
  static Matrix GetMultipliedMatrix(const Matrix &a, const Matrix &b);
  static Matrix GenerateMatrix(const int32_t number_of_rows, const int32_t number_of_columns);

 private:
  int32_t CalculateMcmRecursively(const int32_t start, const int32_t end);
  bool IsInvalidIndex(const int32_t index) const noexcept;
  bool IsInvalidArg(const int32_t start, const int32_t end) const noexcept;
  bool IsAlreadyCalculated(const int32_t start, const int32_t end) const;
  int32_t GetCalculatedResult(const int32_t start, const int32_t end) const;
  void StoreCalculatedResult(const int32_t start, const int32_t end, const int32_t mcm);

  MatrixChainMultiplication(const MatrixChainMultiplication &obj) = delete;
  MatrixChainMultiplication &operator=(const MatrixChainMultiplication &obj) = delete;
  MatrixChainMultiplication(MatrixChainMultiplication &&obj) = delete;
  MatrixChainMultiplication &operator=(MatrixChainMultiplication &&obj) = delete;

 private:
  int32_t number_of_matrices_;
  Matrix matrices_[kMaxNumberOfMatrices];
  LookUp look_up_table_[kMaxNumberOfMatrices][kMaxNumberOfMatrices];
};

void CallMatrixChainMultiplication(std::istream &input_stream) {
  input_stream.tie(0);
  std::ios::sync_with_stdio(false);
  MatrixChainMultiplication *matrix_chain_multiplication = new MatrixChainMultiplication();
  try {
    int32_t number_of_matrices;
    input_stream >> number_of_matrices;
    for (int32_t i = 0; i < number_of_matrices; ++i) {
      int32_t number_of_rows, number_of_columns;
      input_stream >> number_of_rows >> number_of_columns;
      matrix_chain_multiplication->AddMatrix(number_of_rows, number_of_columns);
    }
    const int32_t multiply_count = matrix_chain_multiplication->CalculateMinMultiplyCount();
    std::cout << multiply_count << std::endl;
  } catch (...) {
    std::cerr << "ERROR: CallMatrixChainMultiplication()" << std::endl;
    throw;
  }
  delete matrix_chain_multiplication;
}

// ****************************************************

MatrixChainMultiplication::MatrixChainMultiplication() noexcept : number_of_matrices_(0) {}

MatrixChainMultiplication::~MatrixChainMultiplication() noexcept {}

void MatrixChainMultiplication::AddMatrix(const int32_t number_of_rows, const int32_t number_of_columns) {
  if (number_of_matrices_ >= kMaxNumberOfMatrices) {
    std::cerr << "ERROR: AddMatrix(): Number of matricis size exceeded a limit = " << kMaxNumberOfMatrices << std::endl;
    throw 1;
  }
  const int32_t store_index = number_of_matrices_;
  ++number_of_matrices_;
  matrices_[store_index] = GenerateMatrix(number_of_rows, number_of_columns);
}

int32_t MatrixChainMultiplication::CalculateMinMultiplyCount() {
  const int32_t count = CalculateMcmRecursively(0, number_of_matrices_ - 1);
  return count;
}

int32_t MatrixChainMultiplication::CalculateMcmRecursively(const int32_t start, const int32_t end) {
  int32_t mcm;
  if (start == end) {
    mcm = 0;
  } else if (IsInvalidArg(start, end)) {
    std::cerr << "ERROR: CalculateMcmRecursively(): Invalid arg start = " << start << ", end = " << end << std::endl;
    throw 1;
  } else {
    try {
      if (IsAlreadyCalculated(start, end)) {
        mcm = GetCalculatedResult(start, end);
      } else {
        mcm = INT32_MAX;
        for (int32_t i = start; i <= end - 1; ++i) {
          const int32_t temp_mcm =
              matrices_[start].number_of_rows * matrices_[i].number_of_columns * matrices_[end].number_of_columns +
              CalculateMcmRecursively(start, i) + CalculateMcmRecursively(i + 1, end);
          mcm = std::min(mcm, temp_mcm);
        }
        StoreCalculatedResult(start, end, mcm);
      }
    } catch (...) {
      std::cerr << "ERROR: CalculateMcmRecursively()" << std::endl;
      throw;
    }
  }
  return mcm;
}

bool MatrixChainMultiplication::IsInvalidIndex(const int32_t index) const noexcept {
  return ((index < 0) || (index > number_of_matrices_));
}

bool MatrixChainMultiplication::IsInvalidArg(const int32_t start, const int32_t end) const noexcept {
  const bool is_invalid_start = IsInvalidIndex(start);
  const bool is_invalid_end = IsInvalidIndex(end);
  const bool is_invalid_order = (start > end);
  return is_invalid_start || is_invalid_end || is_invalid_order;
}

bool MatrixChainMultiplication::IsAlreadyCalculated(const int32_t start, const int32_t end) const {
  if (IsInvalidArg(start, end)) {
    std::cerr << "ERROR: IsAlreadyCalculated(): start = " << start << ", end = " << end << std::endl;
    throw 1;
  }
  return look_up_table_[start][end].calculated;
}

int32_t MatrixChainMultiplication::GetCalculatedResult(const int32_t start, const int32_t end) const {
  if (IsInvalidArg(start, end)) {
    std::cerr << "ERROR: GetCalculatedResult(): start = " << start << ", end = " << end << std::endl;
    throw 1;
  }
  return look_up_table_[start][end].mcm;
}

void MatrixChainMultiplication::StoreCalculatedResult(const int32_t start, const int32_t end, const int32_t mcm) {
  if (IsInvalidArg(start, end)) {
    std::cerr << "ERROR: GetCalculatedResult(): start = " << start << ", end = " << end << std::endl;
    throw 1;
  }
  look_up_table_[start][end].mcm = mcm;
  look_up_table_[start][end].calculated = true;
}

int32_t MatrixChainMultiplication::GetMultiplyCount(const Matrix &a, const Matrix &b) {
  if (a.number_of_columns != b.number_of_rows) {
    std::cerr << "ERROR: GetMultiplyCount(): Invalid a.number_of_columns = " << a.number_of_columns
              << ", b.number_of_rows = " << b.number_of_rows << std::endl;
    throw 1;
  }
  return a.number_of_rows * a.number_of_columns * b.number_of_columns;
}

Matrix MatrixChainMultiplication::GetMultipliedMatrix(const Matrix &a, const Matrix &b) {
  if (a.number_of_columns != b.number_of_rows) {
    std::cerr << "ERROR: GetMultipliedMatrix(): Invalid a.number_of_columns = " << a.number_of_columns
              << ", b.number_of_rows = " << b.number_of_rows << std::endl;
    throw 1;
  }
  const Matrix result = GenerateMatrix(a.number_of_rows, b.number_of_columns);
  return result;
}

Matrix MatrixChainMultiplication::GenerateMatrix(const int32_t number_of_rows, const int32_t number_of_columns) {
  Matrix mat;
  mat.number_of_rows = number_of_rows;
  mat.number_of_columns = number_of_columns;
  return mat;
}

int main(int argc, char *argv[]) {
  try {
    CallMatrixChainMultiplication(std::cin);
  } catch (...) {
    std::cerr << "ERROR: main()" << std::endl;
  }
  return 0;
}