Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&a[i+1])
	}

	m := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		m[i] = make([]int, n+1)
	}

	for d := 2; d <= n; d++ {
		for s := 0; s+d <= n; s++ {
			min := 1 << 30
			for j := s+1; j < s+d; j++ {
				val := m[s][j] + m[j][s+d] + a[s]*a[j]*a[s+d]
				if val < min {
					min = val
				}
			}
			m[s][s+d] = min
		}
	}
	fmt.Println(m[0][n])
}

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

typedef long long ll;

vector<ll> r(100);
vector<ll> c(100);
vector<vector<ll>> dp(101, vector<ll>(101,-1));

ll solve(ll a, ll b)
{
    if(dp.at(a).at(b) != -1){
        return dp.at(a).at(b);
    }
    if(b-a == 1){
        return dp.at(a).at(b) = 0;
    }else if(b-a == 2){
        return dp.at(a).at(b) = r.at(a)*c.at(a)*c.at(b-1);
    }else{
        vector<ll> tmp;
        ll i;
        for(i = a+1;i <= b-1;++i){
            ll tmp1 = solve(a, i);
            ll tmp2 = solve(i, b);
            tmp.push_back(tmp1 + tmp2 + r.at(a)*r.at(i)*c.at(b-1));
        }
        sort(tmp.begin(), tmp.end());
        return dp.at(a).at(b) = tmp.front();
    }
}

int main()
{
    ll i,j;
    ll n;
    cin >> n;
    for(i = 0;i < n;++i){
        cin >> r.at(i) >> c.at(i);
    }
    ll ans = solve(0,n);
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
const int INF=1<<21;
int p[105];
int m[105][105];
int main()
{
    int n;
    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;

        /* l==1时，是每两个相乘，l=3个时，是三个与剩下的几个相乘，l=4，是四个矩阵与剩下的
        相乘的最小*/
    for(int l=2;l<=n;l++)       //对于每个l都是用来变更  矩阵相乘大小的
    {
        for(int i=1;i<=n-l+1;i++)
        {
            int j=i+l-1;
            m[i][j]=INF;
            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>
using namespace std;

const int MAX_SIZE = 101;

int dp[MAX_SIZE][MAX_SIZE];

int solve(int p[], int begin,int end) {
	if (dp[begin][end] != -1)return dp[begin][end];
	if (begin >= end - 1)return 0;
	int res = -1;
	for (int i = begin + 1; i < end; i++) {
		int temp = solve(p, begin, i) + solve(p, i, end) + (p[begin] * p[i] * p[end]);
		if (res != -1) {
			res = min(temp, res);
		}
		else {
			res = temp;
		}
	}
	return dp[begin][end] = res;
}

int main() {
	int N;
	int p[MAX_SIZE + 1];

	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> p[i] >> p[i + 1];
	}

	for (int i = 0; i < MAX_SIZE; i++) {
		for (int j = 0; j < MAX_SIZE; j++) {
			dp[i][j] = -1;
		}
	}

	cout << solve(p, 0, N) << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define INFTY (1<<21)

static const int N = 100;

int p[N + 1];
int m[N + 1][N + 1];

int main() {
	int n;
	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] = INFTY;
			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 <bits/stdc++.h>
using namespace std;

// (n, m) @ (m, k) = (n, k)のとき、スカラー乗算の回数はn*m*k回
// 最適な分割を探していく

struct matrix
{
    int r, c;
};

int min_chain_mul(vector<matrix> M) {
    int c[M.size() + 1][M.size() + 1];
    for (int i = 1; i <= M.size(); i++) {
        c[i][i] = 0;
    }

    for (int l = 2; l <= M.size(); l++) { // span
        for (int i = 1; i <= M.size() - l + 1; i++) {
            int j = i + l -1;
            c[i][j] = (1 << 21);
            for (int k = i; k <= j - 1; k++) {
                c[i][j] = min(c[i][j], c[i][k]+c[k+1][j] + M.at(i-1).r * M.at(k-1).c * M.at(j-1).c);
            }
        }
    }
    // for (int i = 1; i <= M.size(); i++)
    //     cout << c[i][i+1] << endl;
    return c[1][M.size()];
}

int main() {
    int n;
    cin >> n;

    vector<matrix> M;
    for (int i = 0; i < n; i++)
    {
        matrix m;
        cin >> m.r >> m.c;
        M.push_back(m);
    }

    cout<< min_chain_mul(M) << endl;
}

C++: ///
// File:  alds1_10_b.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 21:37:26 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

static vector<uint32_t> matrix(101, 0);
static uint64_t dp[101][101];

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 1; i <= n; i++) {
    cin >> matrix[i - 1] >> matrix[i];
  }

  for (uint32_t l = 2; l <= n; l++) {
    for (uint32_t i = 1; i <= n - l + 1; i++) {
      uint32_t j = i + l - 1;
      uint64_t min_val = UINT64_MAX;
      for (uint32_t k = i; k <= j - 1; k++) {
        min_val = min(min_val, dp[i][k] + dp[k + 1][j] + matrix[i - 1] * matrix[k] * matrix[j]);
      }
      dp[i][j] = min_val;
    }
  }

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

  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace 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++: #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[2][index-2]);
  return 0;
}

C++: // http://algorithms.blog55.fc2.com/blog-entry-66.html

#include <iostream>
using namespace std;
#include <stdio.h>

#define MAX 100
#define INFTY (1 << 21)

int SIZE;
int R[MAX + 2];
int C[MAX+1][MAX+1]; // cost table
int B[MAX+1][MAX+1]; // best table

void compute(){
    for ( int i = 0; i <= SIZE; i++ )
    for ( int j = 0; j <= SIZE; j++ ) C[i][j] = INFTY;
    
    for ( int i = 0; i <= SIZE; i++ ) C[i][i] = 0;
    
    for ( int j = 1; j <= SIZE - 1; j++ ){
        for ( int i = 0; i < SIZE - j; i++ ){
            for ( int k = i; k < i + j; k++ ){
                // C[i][i+j] = min( C[i][i+j], 
                //                  C[i][k-1] + C[k][i+j] + R[i]*R[k]*R[i+j+1]);
                int cost = C[i][k] + C[k+1][i+j] + R[i]*R[k+1]*R[i+j+1];
                if ( cost < C[i][i+j] ){
                    C[i][i+j] =  cost;
                    B[i][i+j] = k;
                }
                
            }
        }
    }
}

void order( int i, int j ){
    if ( i == j ) cout << "M" << i;
    else{
        cout << "(";
        order( i, B[i][j]-1 ); order(B[i][j], j);
        cout << ")";
    }
}

void input(){
    cin >> SIZE;
	int z,i=0;
    for (; i < SIZE; i++ ) cin >> R[i]>>z;
	R[i]=z;
	//SIZE++;
}

void output(){
    cout <<  C[0][SIZE-1] << endl;
    //order(1, SIZE);
    //cout << endl;
}

main(){
    input();
    compute();
    output();
}