Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	for i := 1; i <= n; i++ {
		m[i][i] = 0
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			m[i][j] = 1 << 21
			//kまでで区切る
			for 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])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[1][n])
}

C++: #include<iostream>
#include<cstdio>
#define INT_MAX 100000000
using namespace std;
int index;
int mm[1000][1000];
int mine = INT_MAX;

void solve(int p[]){
  int n = index-1,j,q;
  for(int i=1;i<=n;i++)mm[i][i]=0;
  for(int l=2;l<=n;l++){
    for(int i=1;i<=n-l+1;i++){
      j = i+l-1;
      mm[i][j] = INT_MAX;
      for(int k=i;k<=j-1;k++){
	q = mm[i][k] + mm[k+1][j] + p[i-1]*p[k]*p[j];
	if(q<mm[i][j]){
	  mm[i][j] = q;
       
	}

      }
    }
  }




}

int main(){
  int p[1000],a,b;
  scanf("%d",&index);
  //for(int i=0;i<=index;i++)for(int j=0;j<=n;j++)mm[i][j] = INT_MAX;
  for(int i=0;i<index;i++){
    scanf("%d %d",&a,&b);
    p[i] = a;
    if(i == index-1)p[i+1] = b;
  }

  index = index+1;
  solve(p);
 
  printf("%d\n",mm[1][index]);
  return 0;
}

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