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<iostream>
#include<cmath>
#include<vector>

#define INF 1000000000

struct matrix {
	int row;
	int column;

};

using namespace std;

//leftはindexの添え字
int partprob_matchain(vector<vector<int>>& table, matrix* mat, int left, int right) {
	if (left == right) return 0;
	if (table.at(left).at(right) != 0) return table.at(left).at(right);
	int temp, min = INF;
	for (int i = left, j = left + 1; j <= right; i++, j++) {
		temp = partprob_matchain(table, mat, left, i);
		temp += partprob_matchain(table, mat, j, right);
		temp += mat[left].row * mat[i].column * mat[right].column;
		if (temp < min) min = temp;
	}
	table.at(left).at(right) = min;
	return min;
}


int matrix_chain(vector<vector<int>>& table, matrix* mat) {
	return partprob_matchain(table, mat, 0, table.at(0).size() - 1);
}


int main(void) {
	int size;
	cin >> size;
	matrix* mat = new matrix[size];
	vector<vector<int>> table(size, vector<int>(size));

	for (int i = 0; i < size; i++) cin >> mat[i].row >> mat[i].column;

	cout << matrix_chain(table, mat) << endl;
	delete[] mat;
	return 0;

}

C++: #include <cstdio>
#include <vector>
#include <climits>
using namespace std;

inline int MIN(int a,int b){
	return ((a)<(b)?(a):(b));
}

int minimumProduct(int left, int right, vector<pair<int,int> >& mat, vector<vector<int> >& product) {
	if(left==right) return 0;
	if(product[left][right]!=-1) {
		return product[left][right];
	}
	int i,min=INT_MAX;
	for(i=left; i<right; ++i) {
		min=MIN(min,minimumProduct(left,i,mat,product)
				+minimumProduct(i+1,right,mat,product)
				+mat[left].first*mat[right].second*((i==right-1)?mat[right].first:mat[i].second));
	}
	return product[left][right]=min;
}

int main() {
	int n;
	int i,j;
	vector<pair<int,int> > mat;
	vector<vector<int> > product;

	scanf("%d",&n);
	mat.resize(n);
	product.resize(n);
	for(i=0; i<n; ++i) {
		product[i].resize(n);
		scanf("%d%d",&mat[i].first,&mat[i].second);
		for(j=0; j<n; ++j) {
			product[i][j]=-1;
		}
	}
	printf("%d\n",minimumProduct(0,n-1,mat,product));
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAXN = 100+7;

int p[MAXN];
int dp[MAXN][MAXN];
int n;
void generate_dp(){
    for(int i = 1; i <= n; i++){
        dp[i][i] = 0;
    }
    for(int b = 1; b < n; b++){
        for(int i = 1; i <= n-b; i++){
            int j = i+b;
            int k; k = i;
            int min_x = dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j];
            for(k = i+1; k < j; k++){
                if(dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j] < min_x){
                    min_x = dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j];
                }
            }
            dp[i][j] = min_x;
        }
    }
}
int main(){

    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d %d", &p[i], &p[i+1]);
    }
    generate_dp();
    printf("%d\n", dp[1][n]);

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0);

	int n;
	cin >> n;
	ll a, b;
	vector<pair<ll, ll>> v;
	for (int i = 0; i < n; ++i)
	{
		cin >> a >> b;
		v.push_back({a, b});
	}

	vector<vector<ll>> dp(n, vector<ll>(n, -1));
	function<ll(int, int)> f = [&](int i, int j)
	{
		if (i == j)
			return 0LL;

		if (dp[i][j] != -1)
			return dp[i][j];

		ll r = 1LL<<62;
		for (int k = i; k < j; ++k)
			r = min(r, f(i, k)+f(k+1, j)+v[i].first*v[j].second*v[k].second);

		return dp[i][j]=r;
	};

	cout << f(0, n-1) << "\n";

	return 0;
}

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

int n,m[101],cm[101][101];

void calc(){
  int i,j,l,p,k;
  for (i=0;i<n;i++)cm[i][i]=0;
  for (l=1;l<n;l++){
    for(i=0;i<n-l;i++){
      j=i+l;
      cm[i][j]=INF;
      //cout << i << " "<< j << endl;
      for (k=i;k<j;k++){
	//cout << i << " "<< j <<" "<< k<<" "<<cm[i][k]<<" "<<cm[k+1][j] <<  endl;
	p=cm[i][k]+cm[k+1][j]+m[i]*m[k+1]*m[j+1];
	cm[i][j]=min(cm[i][j],p);
      }
    }
  }
}

int main(){
  cin >> n;
  for (int i=0;i<n;i++) cin >> m[i]>>m[i+1];

  calc();

  cout << cm[0][n-1]<<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[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}
	cout << m[1][n] << endl;
	return 0;
}

C++: #include <iostream>
#include <vector>

int64_t calculateMultiply(std::vector<std::pair<int, int>>& v) {
  int n = v.size();
  std::vector<std::vector<int64_t>> m(n, std::vector<int64_t>(n, 0));
  for (int l = 1; l < n; ++l) {
    for (int i = 0; i < n-l; ++i) {
      int j = i+l;
      m[i][j] = INT64_MAX;
      for (int k = i; k < j; ++k) {
        int q = m[i][k] + m[k+1][j] + (v[i].first)*(v[k].second)*(v[j].second);
        if (q < m[i][j]) {
          m[i][j] = q;
        }
      }
    }
  }
  return m[0][n-1];
}


int main() {
  int n;
  std::cin >> n;
  std::vector<std::pair<int, int>> v;
  for (int i = 0; i < n; ++i) {
    int a, b;
    std::cin >> a >> b;
    v.push_back({a, b});
   }
  std::cout << calculateMultiply(v) << std::endl;
}

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

static const int N = 100;

int main(int argc, char const *argv[])
{
    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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<vector<int> > vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<string, int> psi;
typedef pair<int, int> pii;
#define rep(i,s,n) for(int i = (s); i < (n); i++)
#define loop(i) for(int i = 0;true; i++)
#define all(x) (x).begin(), (x).end()
#define mn 1000000007
int main() {
	int N;
	cin >> N;
	vi A(N+1);
	vvi cost(N, vi(N,0));
	for (int i = 0; i < N; i++)
	{
		cin >> A[i] >> A[i + 1];
	}
	for (int i = 2; i <= N; i++)
	{
		for (int left = 0; left <= N-i; left++)
		{
			int right = left + i - 1;
			cost[left][right] =1e9;
			for (int k = left; k < right; k++)
			{
				cost[left][right] = min(cost[left][right], cost[left][k] +cost[k+1][right]+ A[left] * A[k+1] * A[right+1]);
			}
		}
	}
	cout << cost[0][N - 1] << endl;
};

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<vector>
#include<math.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
const long long mod = 1e9 + 7;

int main(){
    int n;
    cin >> n;
    int p[n+1], m[n+1][n+1];
    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 i = 2; i <= n; i++) for(int j = 1; j <= n - i + 1; j++){
        int k = j + i - 1;
        m[j][k] = (1 << 21);
        for(int l = j; l <= k - 1; l++) m[j][k] = min(m[j][k], m[j][l] + m[l + 1][k] + p[j - 1] * p[l] * p[k]);
    }
    cout << m[1][n] << endl;
}