Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	for i := 1; i <= n; i++ {
		m[i][i] = 0
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			m[i][j] = 1 << 21
			//kまでで区切る
			for 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])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[1][n])
}

C++: #include<stdio.h>

#define N 106
#define INF 100000000

int P[N];
int m[N][N];


int matrixchainorder(int*,int);

int main(){
  int i;
  int num;

  scanf("%d",&num);
  

  for(i=1;i<=num;i++){
    //printf("%d\n",i);
    scanf("%d%d",&P[i-1],&P[i]);
  }
  
  printf("%d\n",matrixchainorder(P,num));

  return 0;
}


int matrixchainorder(int *P,int length){
  int n,i,j,k;
  int q;
  int l;

  n = length;

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

  for(l = 2;l <= n;l++){
    for(i=1;i<=n-l+1;i++){
      j = i+l-1;
      m[i][j] = INF;
      for(k=i;k<=j-1;k++){
	q = m[i][k] + m[k+1][j] + P[i-1]*P[k]*P[j];
	if(m[i][j] > q){
	  m[i][j] = q;
	  //printf("q = %d\n",q);
	}
      }
    }
  }

  /*printf("DEBUG\n");
  for(int a = 7; a >= 1; a--){
    for(int b = 0; b <= 6; b++){
      printf("%d ", m[b][a]);

    }
    printf("\n");
  }
  */

  return m[1][n];
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

ll dp[110][110];

int main() {
    ll N;
    cin >> N;
    vll p;
    rep(i, N) {
        ll a, b;
        cin >> a >> b;
        if(i == 0) p.emplace_back(a);
        p.emplace_back(b);
    }

    rep(i, 110) rep(j, 110) dp[i][j] = INF;
    rep(i, 100) dp[i][i] = 0;

    for(ll l = 2; l <= N; l++) rep(i, N-l+1) {
        ll j = i + l - 1;
        for(ll k = i; k < j; k++) chmin(dp[i][j], dp[i][k] + dp[k+1][j] + p[i] * p[k+1] * p[j+1]);
    }
    cout << dp[0][N-1] << endl;
}

C++: #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
ll n;
vector<ll> r(100), c(100);
vector<vector<ll> > dp(101, vector<ll>(101, -1));
int dfs(int v, int w) {
    // cout << v << ":" << w << endl;
    if(dp[v][w] != -1) {
        return dp[v][w];
    } else {
        ll tmp = INT_MAX / 3;
        for(int i = v + 1; i < w; i++) {
            tmp = min(tmp, dfs(v, i) + dfs(i, w) + r[v] * r[i] * c[w - 1]);
        }
        dp[v][w] = tmp;
        return tmp;
    }
}
int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> r[i] >> c[i];
    }
    for(int i = 0; i < n; i++) {
        dp[i][i] = 0;
        dp[i][i + 1] = 0;
    }
    dfs(0, n);
    cout << dp[0][n] << endl;
}

C++: #include <iostream>

using namespace std;

const int N = 100;

int min(int a, int b){
    return a > b ? b : a;
}

int main()
{
    int n;
    int rc[N+1];
    int mat[N + 1][N] = {};
    cin >> n;

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

    // 行列の長さl
    for (int l = 2; l < n + 1; l++)
    {
        // i番目の積 積の数は : n - l + 1
        for (int i = 1; i < n - l + 2; i++)
        {
            mat[l][i] = (1 << 21);
            for (int k = 1; k < l; k++)
            {
                mat[l][i] = min(mat[l][i], mat[k][i] + mat[l - k][i + k] + rc[i-1] * rc[i+k-1] * rc[i+l-1]);
            }
        }
    }

    cout << mat[n][1] << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

ll dp[100][100];
vector<pair<int,int>> matrix(100);


ll kukandp(int i, int j){
    if(dp[i][j]!=INF)return dp[i][j];
    if(j-i==0)dp[i][j]=0;
    if(j-i==1)dp[i][j]=matrix[i].first*matrix[i].second*matrix[j].second;

    if(j-i>1){
        for(int k=i; k<j; k++){
            dp[i][j]=min(dp[i][j], kukandp(i,k)+kukandp(k+1,j)+matrix[i].first*matrix[k].second*matrix[j].second);
        }
    }

    return dp[i][j];
}

int main(){
    int N; cin>>N;
    rep(i,N){
        cin>>matrix[i].first>>matrix[i].second;
    }
    rep(i,100)rep(j,100)dp[i][j]=INF;
    cout<<kukandp(0,N-1)<<endl;
    return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define LL long long
using namespace std;
int main()
{
    int a[200],n,m[200][200],i;
    cin>>n;
    for (i=1; i<=n; i++)
    {
        cin>>a[i-1]>>a[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]+a[i-1]*a[k]*a[j]);
            }
        }
    }
    cout<<m[1][n]<<endl;
    return 0;
}

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>

long long int INF = 1e18;
using namespace std;

typedef pair<long long int, long long int> P;

int main(){
	
	int n;
	cin >> n;
	
	int A[200], B[200];
	
	for(int i = 0; i < n; i++){
		cin >> A[i] >> B[i];
	}
	
	long long int DP[101][101] = {};
	for(int i = 1; i < n; i++){
		for(int j = 0; j + i < n; j++){
			DP[j][j + i] = INF;
			for(int k = 0; k < i; k++){
				DP[j][j + i] = min(DP[j][j + i], DP[j][j + k] + DP[j + k + 1][j + i] + A[j] * B[j + k] * B[j + i]);
				//cout << DP[j][j + k] + DP[j + k + 1][j + i] + A[j + k] * B[j + k] * B[j + k + 1] << " ";
			}
			//cout << DP[j][j + i] << " ";
		}
		//cout << endl;
	}
	
	cout << DP[0][n - 1] << endl;
	
	return 0;
}