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 <cstdlib>
#include <cstdio>

using namespace std;

typedef struct matrix{
  int row;
  int column;
  struct matrix *next;
  struct matrix *back; 
}Matrix;

Matrix *push_back(Matrix *begin, int row, int column) {
  Matrix *p = (Matrix*)malloc(sizeof(Matrix));
  p->row = row;
  p->column = column;
  p->next = NULL;
  p->back = NULL;
  if(begin == NULL) return p;
  Matrix *p2 = begin;
  while(p2->next != NULL) p2 = p2->next;
  p2->next = p;
  p->back = p2;
  return begin;
}

Matrix *product_count(Matrix *x, int type, int *count) {
  Matrix *a,*b;
  if(type == 0) {
    a = x->back;
    b = x;
  }
  else {
    a = x;
    b = a->next;
  }
  *count = 0;
  if(a == NULL || b == NULL) return x;
  *count += a->row * a->column * b->column;
  Matrix *c = (Matrix*)malloc(sizeof(Matrix));
  c->row = a->row;
  c->column = b->column;
  c->next = b->next;
  c->back = a->back;
  if(a->back != NULL) a->back->next = c;
  if(b->next != NULL) b->next->back = c;
  free(a);
  free(b);
  return c;
}

Matrix *product_atob(Matrix *a, Matrix *b, int type,  long *sum) {
  int count;
  if(type == 0) {
    while(a->next != b) {
      b = product_count(b, 0, &count);
      *sum += count;
    }
  }
  else {
   while(a->next != b) {
      a = product_count(a, 1, &count);
      *sum += count;
    }
  }
  a = product_count(a, 1, &count);
  *sum += count;
  return a;
}

Matrix *solve(Matrix *x, long *sum) {
  if(x->next == NULL) return x;
  Matrix *min = x->next, *p = x->next;
  while (p->next != NULL) {
    if(min->column > p->column) min = p;
    p = p->next;
  }
  if(min->column < x->row) {
    x = product_atob(x, min, 0, sum);
  }
  else if(min->column > x->row) {
    x = product_atob(x, min, 1, sum);
  }
  else {
    Matrix *min2 = x->next;
    p = x->next;
    while (p != min) {
      if(min2->column > min->column && min2->column > p->column) min2 = p;
      p = p->next;
    }
    x = product_atob(x, min2, 1, sum);
  }
  if(x->next != NULL) {
    p = solve(x->next,sum);
    x = product_atob(x,p,0,sum);
  }
  return x;
}

int main()
{
  int i,n,r,c;
  Matrix *begin = NULL;
  scanf("%d",&n);
  for (i = 0; i < n; i++) {
    scanf("%d %d",&r,&c);
    begin = push_back(begin,r,c);

  }
  long sum = 0;
  begin = solve(begin, &sum);
  cout << sum << endl;
  
  return 0;
}

C++: #include<stdio.h>

#define N 101

int main(){
  int n;
  int p[N];
  int m[N][N];
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    scanf("%d%d",&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;
      /*2<<21*/
      m[i][j]=2097152;
      for(int k=i;k<=j-1;k++){
	/*さいしょう比較*/
	if(m[i][j]<m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j])
	
	  m[i][j]=m[i][j];
	else
	  m[i][j]=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
      }
    }
  }
  printf("%d\n",m[1][n]);
  
  return 0;


  }

C++: #include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define MAX_N 100
#define INF 1000000000


int N;
int r[MAX_N],c[MAX_N];
int memo[MAX_N][MAX_N];
int saiki(int i ,int j){
    if(memo[i][j] > -1)
        return memo[i][j];
    if(i == j)
        return 0;
    int mini = INF;
    FOR(k,i,j){
        mini = min(mini,saiki(i,k) + saiki(k + 1,j) + r[i] * c[k] * c[j]);
    }
    return memo[i][j] = mini;
}

int main(){
    scanf("%d",&N);
    REP(i,N){
        scanf("%d%d",&r[i],&c[i]);
    }
    REP(i,N)REP(j,N)memo[i][j] = -1;
    printf("%d\n",saiki(0,N-1));
    return 0;
}

C++: #include <iostream>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n;
  std::cin >> n;
  int p[n + 1];
  for (int i = 1; i <= n; ++i) { std::cin >> p[i - 1] >> p[i]; }

  int c[n + 1][n + 1];
  for (int i = 1; i <= n; ++i) c[i][i] = 0;

  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n - l + 1; ++i) {
      int j = i + l - 1;
      c[i][j] = 1 << 21;
      for (int k = i; k < j; ++k) {
        c[i][j] =
            std::min(c[i][j], c[i][k] + c[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  std::cout << c[1][n] << '\n';
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <iomanip>
  
using namespace std;

#define pi 3.141592653589793

int solve(int n, int p[])
{
	int result[101][101];
	for(int i = 0; i < n + 1; i++){
		for(int j = 0; j < n + 1; j++){
			if(i == j) result[i][j] = 0;
			else result[i][j] = 1000000;
		}
	}
	for(int i = 2; i < n + 1; i++){
		for(int j = 1; j < n - i + 2; j++){
			for(int k = j; k < j + i - 1; k++){
				result[j][j + i - 1] = min(result[j][j + i - 1], result[j][k] + result[k + 1][j + i - 1] + p[j - 1] * p[k] * p[j + i - 1]);
			}
		}
	}
	// i : 掛け合わせる行列の個数
	// j : 掛け合わせる行列のうち一番左のもの
	// k : 左から何番目で分けるか
	return result[1][n];
}

int main()
{
	int n;
	cin >> n;
	int p[101];
	for(int i = 1; i < n + 1; i++){
		cin >> p[i - 1] >> p[i];
	}
	cout << solve(n, p) << endl;
	return 0;
}

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

int row[110],column[110];

int dp[110][110];

const int inf = 1LL << 60;

int dfs(int left, int right){ 
  
  if( dp[left][right] != inf ){ return dp[left][right];} // メモ化再帰
  
  int res = inf;
  for(int i = left; i < right; i++){ 
    res = min(res, dfs(left,i)+dfs(i+1,right)+row[left]*column[i]*column[right]);
  }
  
  return dp[left][right] = res;
}

signed main(void){
  
  int n; cin >> n;
  for(int i = 1; i <= n; i++){ 
    cin >> row[i] >> column[i];
  }
  
  for(int i = 0; i < 110; i++){ 
    for(int j = 0; j < 110; j++){ 
      dp[i][j] = inf;
    }
  }

  for(int i = 1; i <= n; i++){
    dp[i][i] = 0;
  }
  
  for(int i = 1; i < n; i++){ 
    // Mi*M(i+1);
    dp[i][i+1] = row[i]*column[i]*column[i+1];
  }
  
  cout << dfs(1,n) << endl;
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;//行列のやつ
const long long  INF = 999999999;
vector<int> p;
vector<vector<int>> dp;
int matrix(){
    int n = p.size()-1;
    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] = INF;
            for(int k=i;k<=j-1;k++){
                int q = dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j];
                dp[i][j] = min(q,dp[i][j]);
            }
        }
    }
    
    return dp[1][n];
}
int main(){
    int n;cin >> n;
    p.resize(n+1);
    dp.assign(n+1,vector<int>(n+1));
    for(int i=0;i<n;i++){
        cin >> p[i] >> p[i+1];
    }
    
    cout << matrix() << endl;
}
/*
 *
 * 連鎖行列積
n 個の行列の連鎖 M1,M2,M3,...,Mn が与えられたとき、スカラー乗算の回数が最小になるように積 M1M2M3...Mn の計算順序を決定する問題を連鎖行列積問題(Matrix-Chain Multiplication problem)と言います。

n 個の行列について、行列 Mi の次元が与えられたとき、積 M1M2...Mn の計算に必要なスカラー乗算の最小の回数を求めるプログラムを作成してください。

入力
入力の最初の行に、行列の数 n が与えられます。続く n 行で行列 Mi(i=1...n) の次元が空白区切りの２つの整数 r、c で与えられます。r は行列の行の数、c は行列の列の数を表します。

出力
最小の回数を１行に出力してください。

制約
*/
/*
1≤n≤100
1≤r,c≤100
入力例 1
6
30 35
35 15
15 5
5 10
10 20
20 25
出力例 1
15125
*/