Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(m[0][n-1])
}

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