Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	p := make([]int, N+1)
	dp := make([][]int, N+1)
	for i, _ := range dp {
		dp[i] = make([]int, N+1)
	}
	for i := 0; i < N; i++ {
		p[i] = sc.nextInt()
		p[i+1] = sc.nextInt()
	}
	for i := 1; i <= N; i++ {
		dp[i][i] = 0
	}
	for l := 2; l <= N; l++ {
		for i := 1; i <= N-l+1; i++ {
			j := i + l - 1
			dp[i][j] = 1 << 60
			for k := i; k <= j-1; k++ {
				dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j])
			}
		}
	}

	fmt.Fprintln(wtr, dp[1][N])
	wtr.Flush()
}

C++: #include <iostream>
#include <vector>
using namespace std;
const int INF = 1001001001;

int main() {
    int n;
    cin  >> n;
    vector<int> p(n+1);
    int temp;
    for(int i=0; i<n-1; i++) {
        cin >> p[i] >> temp;
    }
    cin >> p[n-1] >> p[n];
    
    vector<int> a(n);
    vector< vector<int> > A(n,a);
    for(int i=0; i<n; i++) A[i][i] = 0;

    for(int l=2; l<=n; l++) {
        for(int i=0; i<n-l+1; i++) {
            int j = i+l-1;
            A[i][j] = INF;
            for(int k=i; k<j; k++) {
                int q = A[i][k] + A[k+1][j] + p[i]*p[k+1]*p[j+1];
                if(q<A[i][j]) A[i][j] = q;
            }
        }
    }

    cout << A[0][n-1] << endl;
    return 0;
}

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