Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	mm := make([]int, n+1)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		mm[i] = getNextInt(scanner)
		mm[i+1] = getNextInt(scanner)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			for k := i; k <= j-1; k++ {
				d := dp[i][k] + dp[k+1][j] + mm[i-1]*mm[k]*mm[j]
				if dp[i][j] == 0 || dp[i][j] > d {
					dp[i][j] = d
				}
			}
		}
	}

	fmt.Fprintln(writer, dp[1][n])

	writer.Flush()
}

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

static const int N=100;

//求解矩阵链乘法的最优解
//p 用于存储矩阵的行和列的
//dp[i][j] 表示从矩阵i乘到矩阵j的最小乘法次数

int main(){
    int n;
    int p[N+1],dp[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++){
        dp[i][i]=0;
    }
    for(int l=2;l<=n;l++){
        for(int i=1;i<=n-l+1;i++){
            int j=i+l-1;
            dp[i][j]=(1<<21);//取一个很大的值
            for(int 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]);
            }
        }
    }

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

    return 0;
}

C++: #include <stdio.h>

#define MAX_N 100
#define INF 1e8

int main(){
  int N,i,j,k,l,q,r[MAX_N],c[MAX_N],p[MAX_N+1];
  int dp[MAX_N+1][MAX_N+1];
  scanf("%d",&N);
  for(i = 0 ; i < N ; i++){
    scanf("%d%d",&r[i],&c[i]);
  }
  p[0] = r[0];
  for(i = 1 ; i <= N ; i++){
    p[i] = c[i-1];
  }
  N++;
  for(i = 0 ; 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] = INF;
      for(k = i ; k <= j-1 ; k++){
        q = dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j];
        if(q < dp[i][j]){
          dp[i][j] = q;
        }
      }
    }
  }
  printf("%d\n",dp[1][N-1]);
  return 0;
}

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

int n, p = 0, min = 101, mini;
int v[101];

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> v[i] >> v[i + 1];
		if (min > v[i]) {
			min = v[i];
			mini = i;
		}
	}

	for (int j = 0; j < n; j++)
		p += v[j] * v[j + 1];
	p += v[0] * v[n];

	if (mini == 0)
		p -= v[0] * v[1];
	else if (mini == n)
		p -= v[n - 1] * v[n];
	else
		p -= v[mini] * (v[mini + 1] + v[mini - 1]);

	p *= v[mini];

	cout << p << endl;

	return 0;
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

using ll = long long int;
using P = pair<int,int>;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

//const ll INF=(ll)1e19;
const int INF=(1<<30);

vector<int> r;
vector<int> c;
int dp[105][105];


int main(){
  int n;
  cin >> n;
  REP(i,n){
    int rr,cc;
    cin >> rr >> cc;
    r.emplace_back(rr);
    c.emplace_back(cc);
  }

  REP(i,n)REP(j,n)dp[i][j]=INF;

  REP(i,n)REP(j,n){
    if(j+i >=n)continue;
    if(i==0){
      dp[j][j] = 0;
    }else{

      FOR(k,j,j+i){
        chmin(dp[j][j+i], dp[j][k] + r[j]*c[k]*c[j+i] + dp[k+1][j+i]);
      }
    }
  }

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

C++: #include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int INT_INF = 1LL << 30;

int N;
vll m(110), n(110);
vvll dp(110, vll(110, INF));

ll res(int l, int r){ //[l, r)での最小回数
  if(dp.at(l).at(r) != INF) return dp.at(l).at(r);
  if(r-l == 1) return dp.at(l).at(r) = 0;
  if(r-l == 2) return dp.at(l).at(r) = m.at(l) * n.at(l) * n.at(r-1);

  ll result = INF;
  for(int i = l+1; i <= r-1; i++){ //[l, i), [i, r)に分割
    chmin(result, res(l, i) + res(i, r) + m.at(l) * m.at(i) * n.at(r-1));
  }
  return dp.at(l).at(r) = result;
}

int main(){
  cin >> N;
  for(int i = 0; i < N; i++){
    cin >> m.at(i) >> n.at(i);
  }

  cout << res(0, N) << endl;
}

C++: // 連鎖行列積 問題
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

#define MAX_N 100
#define ELE_NUM 2
// 無効な値の定義
const int INV = -1;

const int colNo = 0;
const int rowNo = 1;
const int IntMAX = 	2147483647;

int ChainedMatrixtMultiplication(int n, int m, int matrixData[][ELE_NUM]);

int main(void)
{

    int n, count;
    int matrix_data[MAX_N + 1][ELE_NUM];

    cin >> n;

    for(int i = 1; i <= n; i++)
    {
        // 行
        cin >> matrix_data[i][colNo];
        // 列
        cin >> matrix_data[i][rowNo];
    }

    count = ChainedMatrixtMultiplication(n, ELE_NUM, matrix_data);
    printf("%d\n", count);

    return 0;
}

// 連鎖行列積の最小の乗算回数を演算する関数
// n : 積を求める行列の数
// m : 行列の情報 : 0 - 行の情報, 1 - 列の情報
// matrixData[][] : 各行列の行、列情報を保持する配列
int ChainedMatrixtMultiplication(int n, int m, int matrixData[][ELE_NUM])
{

    // 第1要素 : 演算開始のNo., 第2要素 : 演算終了のNo.
    int dp[MAX_N + 1][MAX_N + 1];
    int calcResult = 0;

    // 配列の初期化
    for(int i = 0; i <= n; i++)
    {
        for(int j = 0; j <= n; j++)
        {
            dp[i][j] = INV;

            if( i == j )
            {
                // 同じ要素同士の積は演算することが不可能
                dp[i][j] = 0;
            }
        }
    }

    // 行列の演算回数の計算
    // i : 行列の演算をする先頭の要素No.
    // j : 行列の演算をする最後の要素No.
    // k : i 〜 jまでの範囲を移動する変数
    for(int l = 2; l <= n; l++)
    {
        for(int i = 1; i <= n - l + 1; i++)
        {
            int j = i + l - 1;

            dp[i][j] = IntMAX;

            printf("i_s = %d i_e = %d j = %d\n", i, n - l + 1, j);

            for(int k = i; k <= j - 1; k++)
            {
                calcResult = dp[i][k] + dp[k + 1][j]
                            + ( matrixData[i][colNo] * matrixData[k][rowNo] * matrixData[j][rowNo]);

                dp[i][j] = min( dp[i][j], calcResult );
            }
        }
    }

    return dp[1][n];
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
const int MAX_V = 100;
vector<pair<int,int> > pairs;
int main(){
  int n; scanf("%d",&n);
  for(int i=0;i<n;i++){
    pair<int,int> p;
    scanf("%d %d",&p.first,&p.second);
    pairs.push_back(p);
  }
  long long int sum=0;
  while(pairs.size()>1){
    vector<pair<int,int> >::iterator min_ptr;
    int min=0x7fffffff;
    int mulMin=0x7fffffff;
    for(auto iter=pairs.begin()+1;iter<pairs.end();iter++){
      int localMin = (*(iter-1)).first;
      if(localMin > (*iter).second) localMin = (*iter).second;
      int mul=(*(iter-1)).first*(*iter).second;
      if(min>=localMin){
	if(mulMin>mul){
	  min=localMin;
	  mulMin=mul;
	  min_ptr=iter;
	}
      }
    }
    sum+=(*(min_ptr-1)).first*(*min_ptr).first*(*min_ptr).second;
    (*(min_ptr-1)).second=(*min_ptr).second;
    pairs.erase(min_ptr);
  }
  printf("%lld\n",sum);
}