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>
using namespace std;

int main() {
    int max = 100;
    int n;
    cin >> n; // take the input
    int p[max + 1], m[max + 1][max + 1];
    for (int i = 1; i <= n; i++) {
        cin >> p[i - 1] >> p[i]; // scan in the matrix
    }

    for (int i = 1; i <= n; i++) {
        m[i][i] = 0;
        for (int t = 2; t <= n; t++) {
            for (int i = 1; i <= n - t + 1; i++) {
                int j = i + t - 1;
                m[i][j] = (1 << 21);
                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>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;
typedef vector<pll> vec;

lli n;
vec rc;
mat dp;

lli solve(lli x,lli y){
    if(x == y) return dp[x][y] = 0;
    if(dp[x][y]) return dp[x][y];
    lli ret = 1ll << 61;
    for(lli i = x;i < y;i++){
        ret = min(ret,solve(x,i) + solve(i+1,y) + rc[i].second*rc[x].first*rc[y].second);
    }
    return dp[x][y] = ret;
}


int main(){
    cin >> n;
    rc = vec(n);
    dp = mat(n,vll(n));
    for(lli i = 0;i < n;i++) cin >> rc[i].first >> rc[i].second;
    cout << solve(0,n-1) << endl;
    return 0;

}

C++: #include <bits/stdc++.h>
#define N 110
using namespace std;
int A[N];
int dp[N][N];

int Min(int &a,int b){return a = min(a,b);}
int main(){
  int n;
  cin>>n;

  for(int i=0,cnt=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    if(i == 0) A[cnt++] = a;
    A[cnt++] = b;
  }

  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++)dp[i][j] = 1e9;
    dp[i][i] = 0;
  }
  
  for(int len=0;len<n;len++)
    for(int l=1;l<n;l++){
      int r = l+len;
      if(l+len>=N)continue;
      for(int m = l;m<r;m++){
        Min(dp[l][r],dp[l][m] + dp[m+1][r] + A[l-1]*A[m]*A[r]);
      }
}
  
  cout<<dp[1][n]<<endl; 
  return 0;
}

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

static const int N = 100;

int main(){
  int n,p[N+1],m[N+1][N+1];
  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]=(1<<21);
      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++: //矩阵连乘问题，递归方程采用课本P47的递归方程。
//与课本P47的程序相比，两者都是填m[][]的上三角，
//但本程序是横向填表，而课本程序是斜向填表。
//本程序按照 自底向上，自左向右 的顺序来计算m[][]的上三角。
//程序中有记录相应断开位置到s[][]。

#include <iostream>
using namespace std;

void MatrixChain(int, int[], int **, int **);
void print(int, int, int **);
void CalculateMin(int i, int j, int p[], int **m, int **s);

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

    MatrixChain(n, p, m, s);
    cout << m[1][n] << endl; //最优值存储在m[1][n]
    //print(1, n, s); //打印最优计算方式

    return 0;
}

void MatrixChain(int n, int p[], int **m, int **s)
{
    //以下双重循环对m[][]的上三角进行填表
    //填表顺序为自底向上，自左向右
    for (int i = n; i >= 1; i--) //i表示行
    {
        for (int j = i; j <= n; j++) //j表示列，因为只填上三角，所以j的初值为i
        {
            //以下按照矩阵连乘问题的递归公式来求每一个m[i][j]
            if (i == j)
            {
                m[i][j] = 0;
            }
            else
            {
                CalculateMin(i, j, p, m, s);
            }
        }
    }
}

void CalculateMin(int i, int j, int p[], int **m, int **s)
{
    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 (int k = i + 1; k < j; k++) //k用于尝试不同的断开位置
    {
        int curr = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        if (curr < m[i][j]) //当前k值作为Ai连乘到Aj的断开位置能获得更少计算量
        {
            m[i][j] = curr; //刷新最优值
            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 << ")";
    }
}