Go: package main

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

type Matrix struct {
	r int // num of row
	c int // num of column
}

type Range struct {
	start int // index, inclusive
	end   int // index, inclusive
}

func (r Range) length() int {
	return r.end - r.start + 1
}

func calcMultiplication(x, y Matrix) (Matrix, int) {
	if x.c != y.r {
		panic("cannnot calculate")
	}
	return Matrix{x.r, y.c}, x.r * y.c * x.c
}

func calcMinMatrixChainMultiplication(matrices []Matrix, r Range, memo map[Range]int) (Matrix, int) {
	if v, ok := memo[r]; ok {
		return Matrix{matrices[r.start].r, matrices[r.end].c}, v
	}
	if r.length() == 0 {
		panic("must not be empty")
	} else if r.length() == 1 {
		return matrices[r.start], 0
	}

	var minCount int = math.MaxInt32

	for i := 0; i < r.length()-1; i++ {
		formerRange := Range{r.start, r.start + i}
		latterRange := Range{r.start + i + 1, r.end}
		matF, countF := calcMinMatrixChainMultiplication(matrices, formerRange, memo)
		matL, countL := calcMinMatrixChainMultiplication(matrices, latterRange, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	memo[r] = minCount
	return Matrix{matrices[r.start].r, matrices[r.end].c}, minCount
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, _ := nextInt(sc)
	matrices := make([]Matrix, n)
	for i := 0; i < n; i++ {
		r, _ := nextInt(sc)
		c, _ := nextInt(sc)
		matrices[i] = Matrix{r, c}
	}
	memo := make(map[Range]int)
	_, resultCount := calcMinMatrixChainMultiplication(matrices, Range{0, len(matrices) - 1}, memo)
	fmt.Println(resultCount)
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include<bits/stdc++.h>

using namespace std;

struct Matrix
{
	int r,c;
};

int main()
{
	Matrix Mat[100]; 
	int n,dp[100][100];
	memset(dp,0,sizeof(dp));
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d%d",&Mat[i].r,&Mat[i].c);
	for(int i=1;i<n;i++)
		for(int j=0;i+j<n;j++)
		{
			dp[j][i+j]=INT_MAX;
			for(int k=j;k<i+j;k++)
				dp[j][i+j]=min(dp[j][i+j],dp[j][k]+dp[k+1][i+j]+Mat[j].r*Mat[i+j].c*Mat[k].c);
		}
	printf("%d\n",dp[0][n-1]);
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define rep(i, a, n) for(int i = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int i = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int i = (int)(a);(x);++(i))
#define repr(i, a, n) for(int i = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int i = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x)/gcd(x,y)*(y))
#define isin(x, l, r) ((l) <= (x) and (x) < (r))
#define fcout(n) cout << fixed << setprecision(n)
#define rcout(n) cout << right << setw(n)
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define PI (acosl(-1))
#define EPS (1e-10)
#define INF LLONG_MAX
#define INF32 INT32_MAX
#define MOD 1000000007LL
using namespace std;
using VI = vector<int>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<PII>;
using VTIII = vector<TIII>;
using Complex = complex<double>;
template<typename T, template<typename> typename C = less> using PQ = priority_queue<T, vector<T>, C<T>>;
template<typename T> inline bool chmax(T& a, const T& b, bool t = false){ if(a<b or (t and a == b)){ a=b; return true; } return false; }
template<typename T> inline bool chmin(T& a, const T& b, bool t = false){ if(a>b or (t and a == b)){ a=b; return true; } return false; }


int n;
int r[110];
int c[110];
int dp[110][110];


signed main(){
    cin >> n;
    rep(i, 0, n) cin >> r[i] >> c[i];

    rep(i, 0, n) rep(j, 0, n) if(i != j) dp[i][j] = INF;
    rep(l, 1, n) rep(i, 0, n-l) rep(j, i, i+l) chmin(dp[i][i+l], dp[i][j]+dp[j+1][i+l]+r[i]*c[j]*c[i+l]);

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

    return 0;
}

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

void MatrixChain(int, int[], int **, int **);
void print(int, int, int **);

int main()
{
    int n;
    cin >> n;
    int *tempP = new int[2 * n]; //p[0]为A1的行数；p[i]为Ai的列数
    
    int *p = new int[n + 1];    //p[0]为A1的行数；p[i]为Ai的列数
    int **m = new int *[n + 1]; //m[i][j]为Ai连乘到Aj的最少乘次数
    int **s = new int *[n + 1]; //s[i][j]为 Ai连乘到Aj的最优解的第一层断点
    for (int i = 1; i <= n; i++)
    {
        m[i] = new int[n + 1];
        s[i] = new int[n + 1];
    }
    
    for (int i = 0; i < 2 * n; i++)
    {
        cin >> tempP[i];
    }
    
    for (int i = 0; i <= n; i++)
    {
        p[i] = i == 0 ? tempP[0] : tempP[i*2-1];
    }
    
    // for (int i = 0; i <= n; i++)
    //     cin >> p[i];
    MatrixChain(n, p, m, s);
    cout    << m[1][n] << endl; //最优值存储在m[1][n]
        //print(1, n, s);
    
//    for (int idx = 1; idx <= n; idx++)
//    {
//        for (int jdx = 1; jdx <= n; jdx++)
//        {
//            cout << m[idx][jdx] << " ";
//        }
//        cout << endl;
//    }
    return 0;
}

void MatrixChain(int n, int p[], int **m, int **s)
{
    int i, j, k, temp;
    //以下双重循环对m[][]的上三角进行填表
    //填表顺序为自底向上，自左向右
    for (i = n; i >= 1; i--) //i表示行
    {
        for (j = i; j <= n; j++) //j表示列，因为只填上三角，所以j的初值为i
        {
            //以下按照矩阵连乘问题的递归公式来求每一个m[i][j]
            if (i == j)
            {
                m[i][j] = 0;
            }
            else
            {
                m[i][j] = m[i][i] + m[i + 1][j] + p[i - 1] * p[i] * p[j]; //若i与j不相等，m[i][j]的初值为断点为i时的最优值
                
                s[i][j] = i;                //记录当前断点位置为i
                for (k = i + 1; k < j; k++) //k用于尝试不同的断点
                {
                    temp = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
                    if (temp < m[i][j]) //当前k值作为Ai连乘到Aj的断点能获得更少计算量
                    {
                        m[i][j] = temp; //刷新最优值
                        s[i][j] = k;    //刷新相应断点
                    }
                }
            }
        }
    }
}

void print(int i, int j, int **s)
{
    if (i == j)
        cout << "A" << i;
    else
    {
        cout << "(";
        print(i, s[i][j], s);
        print(s[i][j] + 1, j, s);
        cout << ")";
    }
}

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

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

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