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<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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(b);i>=(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
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;}

ll n;
ll r[105],c[105];
ll dp[105][105];
ll f(ll lx,ll rx){
    if(rx-lx==1)return 0;
    if(dp[lx][rx]!=-1)return dp[lx][rx];
    ll ret=LINF;
    fore(i,1,rx-lx-1){
        chmin(ret,f(lx,lx+i)+f(lx+i,rx)+r[lx]*r[lx+i]*c[rx-1]);
    }
    return dp[lx][rx]=ret;
}
void solve(){
    cin>>n;
    rep(i,n)cin>>r[i]>>c[i];

    rep(i,n+1)rep(j,n+1)dp[i][j]=-1;
    cout<<f(0,n)<<endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    solve();
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;


ll dp[110][110];
bool flag[110][110];
int n;
vector<ll>p;

ll rec(ll left,ll right){
    if(flag[left][right])return dp[left][right];
    if(left == right){
        flag[left][right]=true;
        return dp[left][right]=0;
    }
    if(left>right){
        return 0;
    }
    if(right -left ==1){
        flag[left][right]=true;
        return dp[left][right] = p[left]*p[left+1]*p[left+2];
    }
    ll Min = INF;
    for(ll l=left;l<right;l++){
        Min =min(Min,rec(left,l)+rec(l+1,right)+p[left]*p[l+1]*p[right+1]);
    }
    flag[left][right]=true;
    return dp[left][right]=Min;
}

int main(void){
    cin>>n;
    vector<int>r(n),c(n);
    for(int i=0;i<n;i++)cin>>r[i]>>c[i];
    p.resize(n+2);
    p[1]=r[0];
    for(int i=2;i<=n+1;i++)p[i]=(c[i-2]);
    //dp[l][r]をl,l+1,...,r-1と定める
    cout<<rec(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<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int n, r[105], c[105];
int dp[105][105];


int main() {
    cin >> n;
    rep(i, n) cin >> r[i] >> c[i];

    rep(i, n) rep(j, n) dp[i][j] = i == j ? 0 : INF;

    for (int i = 1; i < n; i++) {
        for (int j = i-1; j >= 0; j--) {
            for (int k = 0; k < i; k++) {
                int cost = dp[i][k+1] + dp[k][j] + r[j]*c[k]*c[i];
                chmin(dp[i][j], cost);
            }
        }
    }
    cout << dp[n-1][0] << endl;
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, r[100], c[100], dp[100][101];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) scanf("%d%d", &r[i], &c[i]), dp[i][i] = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 0; i + j <= n; j++) {
			dp[j][i + j] = min(dp[j][i + j - 1], dp[j + 1][i + j]) + c[j] * c[i + j - 1];
		}
	}
	printf("%d\n", dp[0][n]);
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <queue>
#include <list>
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,a,b) for(int i=int(a);i<int(b);++i)
#define crep(i) for(char i='a';i<='z';i++)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define psortfirst(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.first<b.first;});
#define All(x) (x).begin(),(x).end()
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
using namespace std;
using pii  = pair<int,int>;
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
typedef long long ll;
#define MOD 1000000007
#define EPS 10e-8

static const int N = 100;

int main(){
    ll N,P[107],m[107][107];
    cin >> N;
    REP(i,1,N+1){
        cin >> P[i-1] >> P[i];
    }
    REP(i,1,N+1){
        m[i][i]=0;
    }
    REP(l,2,N+1){
        REP(i,1,N-l+2){
            int j=i+l-1;
            m[i][j]=MOD;
            REP(k,i,j+1){
                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;
}