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 "bits/stdc++.h"
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define IOS std::cin.tie(0),std::ios::sync_with_stdio(false)

namespace idaten {

	using namespace std;
	using ll = int_fast64_t;
	using ull = uint_fast64_t;
	using P = std::pair<int64_t, int64_t>;
	template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
	template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
	template<class Type>struct is_vector : std::false_type {};
	template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
	template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
		if (v.empty())return out;
		constexpr bool is_vector_v = is_vector<T>::value;
		if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
		else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
		return out;
	}
	inline void put() {}
	template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
	template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
	inline void putn() {}
	template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

	void solve() {
		IOS;
		int n;
		cin >> n;
		vector<int> a(n + 1);
		int b;
		REP(i, n) {
			cin >> a[i] >> b;
		}
		a[n] = b;
		vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
		RREP(i, n + 1) {
			FOR(j, i + 2, n + 1) {
				int tmp = INT_MAX;
				FOR(k, i + 1, j) {
					chmin(tmp, dp[i][k] + dp[k][j] + a[i] * a[k] * a[j]);
				}
				dp[i][j] = tmp;
					
			}
		}
		put(dp[0][n]);
	}
		
	
} // namespace idaten

int main() {
	idaten::solve();
	return 0;
}

C++: #include <iostream>
using namespace std;
static const int INFTY=(1<<21);

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin>>n;
  int p[n+1];
  for (int i=1; i<=n; i++) {
    cin>>p[i-1]>>p[i];
  }
  int m[n+1][n+1];
  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;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
	int n;
  	cin >>n;
  	long int p[n+1];
  	rep(i,n){
    	cin >>p[i]>>p[i+1];    
    }
  	long int dp[n+1][n+1];
  	for(int i=1;i<=n;i++){
    	dp[i][i]=0;
    }
  	
  	for(int l=1;l<=n-1;l++){
    	for(int i=1;i<=n-l;i++){
        	int j=i+l;
          	dp[i][j]=1000000000;
            for(int k=i;k<=j;k++){
              long int x=dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j];
             dp[i][j]=min(dp[i][j],x);    	
        	}             
            
        }
    }
  	cout<<dp[1][n]<<endl;
  
}

C++: #include<stdio.h>
#define INFTY 1000000

int min(int,int);

int main(){
  int l,i,j,k,n,p[100],m[101][101],q;
  scanf("%d",&n);

  for(i=1;i<=n;i++){
    scanf("%d%d",&p[i-1],&p[i]);
  }

  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]=INFTY;
      for(k=i;k<=j-1;k++){
	q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
	m[i][j]=min(m[i][j],q);
      }
    }
  }

  printf("%d\n",m[1][n]);
  return 0;
}

int min(int a,int b){
  if(a<b)return a;
  else return b;
}

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<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define INFTY (1<<30)

int m[101][101];
int p[101];
int n;

void multiplyMatrix(){
	int i, l, j, k;
	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] = INFTY;
			for(k = i;k < j;k++)
				m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
		}
	}
}

int main(void){
	int i;
	scanf("%d", &n);
	for(i = 0;i < n;i++)
		scanf("%d %d", &p[i], &p[i + 1]);
	multiplyMatrix();
	printf("%d\n", m[1][n]);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

#define MAX 100

int dp[MAX+1][MAX+1]; //乗算回数
int p[MAX+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++) dp[i][i] = 0;

    for(int l = 2; l <= n; l++){  //lは対象とする行列の数
        for(int i = 1; i + l - 1 <= n; i++){
            int j = i + l - 1; //iからjまでの最小乗算回数を見ていく
            dp[i][j] = (1 << 21); //INFTYで埋める
            for(int k = i; k <= j-1; k++){
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j]);
            }
        }
    }

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

    return 0;
}

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

static const int N=100;
int dp[N+1][N+1];
int p[N+1];
int main(){
    int n;
    cin>>n;
    int h,w;//h:num of gyo,w:num of retu
    for(int i=1;i<=n;i++){
        cin>>p[i-1]>>p[i];
    }
    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=l+i-1;
            dp[i][j]=(1<<21);
        for(int k=i;k<=j-1;k++){
            dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j]);
        
        }
        }
        
    }
    cout<<dp[1][n]<<endl;
    return 0;
}