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 <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>

struct Matrix
{
	int rows;
	int columns;
};

int main()
{
	int n;
	std::cin >> n;
	//There's n matrixes.
	
	if(n == 1)
	{
		std::cout << 0 << std::endl;
		return 0;
	}
	
	Matrix matrix[1 + n];
	
	for(int i = 1;i <= n;i++)
	{
		//cin >> r, rows >> c, columns.
		std::cin >> matrix[i].rows >> matrix[i].columns;
	}
	
	long long int DPMem[1 + n][1 + n] = {0};
	
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			DPMem[i][j] = 9223372036854775807;
	
	//DP
	
	//init DPMem of M(i) * M(i) and M(i) * M(i + 1).
	for(int i = 1;i <= n - 1;i++)
	{
		DPMem[i][i] = 0;
		DPMem[i][i + 1] = matrix[i].rows * matrix[i].columns * matrix[i + 1].columns;
		
		//Debug.
		//printf("DPMem[%i][%i] = %lli \n",i,i + 1,DPMem[i][i + 1]);
	}
	DPMem[n][n] = 0;
	
	//Debug.
	//std::cout << std::endl;
	
	//DP main.
	for(int len = 3;len <= n;len++)		//len refers to length of multiplying matrix chain
	{
		//Debug.
		//printf("Length = %i \n",len);
		for(int head = 1;head + len - 1 <= n;head++)
		{
			int tail = head + len - 1;
			for(int mid = head;mid < tail;mid++)
			{
				//Debug.
				/*
				printf("DPMem[%i][%i] = %lli \n",head,tail,DPMem[head][tail]);
				printf("head = %i, mid = %i,mid + 1 = %i, tail = %i \n",head,mid,mid + 1,tail);
				*/
				
				DPMem[head][tail] = std::min(DPMem[head][tail],DPMem[head][mid] + DPMem[mid + 1][tail] + matrix[head].rows * matrix[mid].columns * matrix[tail].columns);
				
				//Debug.
				/*
				printf("DPMem[%i][%i] = %lli \n",head,mid,DPMem[head][mid]);
				printf("DPMem[%i][%i] = %lli \n",mid + 1,tail,DPMem[mid + 1][tail]);
				printf("matrix[%i].rows * matrix[%i].columns(matrix[%i].rows) * matrix[%i].columns = %i \n",head,mid,mid + 1,tail,matrix[head].rows * matrix[mid].columns * matrix[tail].columns);
				printf("DPMem[%i][%i] = %lli \n",head,tail,DPMem[head][tail]);
				std::cout << std::endl;
				*/
			}
		}
	}

	//Output.
	std::cout << DPMem[1][n] << std::endl;
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<ll> r(100);
vector<ll> c(100);
vector<vector<ll>> dp(101, vector<ll>(101,-1));

ll solve(ll a, ll b)
{
    if(dp.at(a).at(b) != -1){
        return dp.at(a).at(b);
    }
    if(b-a == 1){
        return dp.at(a).at(b) = 0;
    }else if(b-a == 2){
        return dp.at(a).at(b) = r.at(a)*c.at(a)*c.at(b-1);
    }else{
        vector<ll> tmp;
        ll i;
        for(i = a+1;i <= b-1;++i){
            ll tmp1 = solve(a, i);
            ll tmp2 = solve(i, b);
            tmp.push_back(tmp1 + tmp2 + r.at(a)*r.at(i)*c.at(b-1));
        }
        sort(tmp.begin(), tmp.end());
        return dp.at(a).at(b) = tmp.front();
    }
}

int main()
{
    ll i,j;
    ll n;
    cin >> n;
    for(i = 0;i < n;++i){
        cin >> r.at(i) >> c.at(i);
    }
    ll ans = solve(0,n);
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
const int INF=1<<21;
int p[105];
int m[105][105];
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;

        /* l==1时，是每两个相乘，l=3个时，是三个与剩下的几个相乘，l=4，是四个矩阵与剩下的
        相乘的最小*/
    for(int l=2;l<=n;l++)       //对于每个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++)
            {
                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;

const int MAX_SIZE = 101;

int dp[MAX_SIZE][MAX_SIZE];

int solve(int p[], int begin,int end) {
	if (dp[begin][end] != -1)return dp[begin][end];
	if (begin >= end - 1)return 0;
	int res = -1;
	for (int i = begin + 1; i < end; i++) {
		int temp = solve(p, begin, i) + solve(p, i, end) + (p[begin] * p[i] * p[end]);
		if (res != -1) {
			res = min(temp, res);
		}
		else {
			res = temp;
		}
	}
	return dp[begin][end] = res;
}

int main() {
	int N;
	int p[MAX_SIZE + 1];

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

	for (int i = 0; i < MAX_SIZE; i++) {
		for (int j = 0; j < MAX_SIZE; j++) {
			dp[i][j] = -1;
		}
	}

	cout << solve(p, 0, N) << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define INFTY (1<<21)

static const int N = 100;

int p[N + 1];
int m[N + 1][N + 1];

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] = INFTY;
			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 <bits/stdc++.h>
using namespace std;

// (n, m) @ (m, k) = (n, k)のとき、スカラー乗算の回数はn*m*k回
// 最適な分割を探していく

struct matrix
{
    int r, c;
};

int min_chain_mul(vector<matrix> M) {
    int c[M.size() + 1][M.size() + 1];
    for (int i = 1; i <= M.size(); i++) {
        c[i][i] = 0;
    }

    for (int l = 2; l <= M.size(); l++) { // span
        for (int i = 1; i <= M.size() - l + 1; i++) {
            int j = i + l -1;
            c[i][j] = (1 << 21);
            for (int k = i; k <= j - 1; k++) {
                c[i][j] = min(c[i][j], c[i][k]+c[k+1][j] + M.at(i-1).r * M.at(k-1).c * M.at(j-1).c);
            }
        }
    }
    // for (int i = 1; i <= M.size(); i++)
    //     cout << c[i][i+1] << endl;
    return c[1][M.size()];
}

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

    vector<matrix> M;
    for (int i = 0; i < n; i++)
    {
        matrix m;
        cin >> m.r >> m.c;
        M.push_back(m);
    }

    cout<< min_chain_mul(M) << endl;
}

C++: ///
// File:  alds1_10_b.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 21:37:26 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

static vector<uint32_t> matrix(101, 0);
static uint64_t dp[101][101];

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 1; i <= n; i++) {
    cin >> matrix[i - 1] >> matrix[i];
  }

  for (uint32_t l = 2; l <= n; l++) {
    for (uint32_t i = 1; i <= n - l + 1; i++) {
      uint32_t j = i + l - 1;
      uint64_t min_val = UINT64_MAX;
      for (uint32_t k = i; k <= j - 1; k++) {
        min_val = min(min_val, dp[i][k] + dp[k + 1][j] + matrix[i - 1] * matrix[k] * matrix[j]);
      }
      dp[i][j] = min_val;
    }
  }

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

  return 0;
}