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;

#define MAX_N 10000
#define INF 10000000LL

long long n, j, x[MAX_N][MAX_N], p[MAX_N + 2], sum;

int matrix() {
	for (int i = 0; i < n; i++) { x[i][i] = 0; }
	for (int h = 2; h <= n; h++) {
		for (int i = 1; i <= n - h + 1; i++) {
			j = i + h - 1;
			x[i][j] = INF;
			for (int k = i; k < j; k++) {
				x[i][j] = min(x[i][j], x[i][k] + x[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}
	sum = x[1][n];
	return sum;
}

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

C++: #include<bits/stdc++.h>
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;
const int N=105;
const int INF=0x3f3f3f3f;
int n,r[N],c[N],f[N][N];
int dp(int i,int j){
	if(f[i][j])return f[i][j];
	if(i==j)return f[i][j]=0;
	if(i+1==j)return f[i][j]=r[i]*c[i]*c[j];
	int ans=INF;
	for(int k=i;k<j;k++){
		ans=min(ans,dp(i,k)+dp(k+1,j)+r[i]*c[k]*c[j]);
	}
	return f[i][j]=ans;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&r[i],&c[i]);
	}
	printf("%d\n",dp(1,n));
	return 0;
}

C++: #include<bits/stdc++.h>
#define pb push_back
#define ll long long

using namespace std;

ll dp[105][105];
vector<pair<ll,ll> >v;


ll mcm(int left,int right)
{

    if(left>=right) return 0;
    if(dp[left][right]!=INT_MAX) return dp[left][right];
//cout<<"ok"<<endl;
    ll ans=INT_MAX;

    for(int i=left;i<right;i++)
    {
        ll res_left= mcm(left,i);
        ll res_right= mcm(i+1,right);
        ll res=v[left].first*v[i].second*v[right].second;
        ans=min(ans, res+res_left+res_right);
        //cout<<ans<<endl;
    }
    return dp[left][right]=ans;
}



int main()
{
    for(int i=0;i<104;i++) for(int j=0;j<104;j++) dp[i][j]=INT_MAX;

    int n,temp; cin>>n;
    temp=n;
    while(temp--)
    {
        int a,b; cin>>a>>b;
        v.pb({a,b});
    }

    cout<<mcm(0,n-1)<<endl;

}

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

int MaxCM(vector<int> A){
	int n = A.size() - 1, min, val;
	vector< vector<int> > table(n, vector<int>(n, 0));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n - i; j++){
			for (int k = 0; k < i; k++){
				val = table[j][j + k] + table[j + k + 1][j + i] + A[j] * A[j + k + 1] * A[j + i + 1];
				if (k == 0 || val < min) min = val;
			}
			if (i != 0) {
				table[j][j + i] = min;
			}
			else{
				table[j][j] = 0;
			}
		}
	}
	return table[0][n - 1];
}

int main(){
	int n, dum;
	scanf("%d", &n);
	vector<int> A(n + 1);
	for (int i = 0; i < n; i++){
		if (i == 0){
			scanf("%d%d", &A[0], &A[1]);
		}
		else{
			scanf("%d%d", &dum, &A[i + 1]);
		}
	}
	printf("%d\n", MaxCM(A));

	return 0;
}

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

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 ( 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;

int main() {
   int n;
   cin >> n;
   int r[n+1],c[n+1];
   for (int i=1; i<n+1; i++) {
       cin >> r[i] >> c[i];
   }
   int dp[n+1][n+1];
   memset(dp,0,sizeof(dp));
   for (int i=1; i<=n; i++) {
       for (int j=1; j<=i; j++) {
           if (j==1) {
               continue;
           }
           if (j==2) {
               dp[i][j]=r[i-1]*r[i]*c[i];continue;
           }
           int m;
           for (int k=1; k<j; k++) {
               if (k==1) {
                   m=dp[i][j-1]+r[i-j+1]*c[i-j+1]*c[i];
               }
               else m=min(m,dp[i-j+k][k]+dp[i][j-k]+r[i-j+1]*c[i-j+k]*c[i]);
           }
           dp[i][j]=m;
       }
   }
   cout << dp[n][n] << endl;
}

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 <iostream>
#include <algorithm>
using namespace std;

static const int N = 1000;

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

using namespace std;
const int maxn=100+10;
const int INF=(1<<21);
int n;
int p[maxn];
int dp[maxn][maxn];
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d%d",&p[i],&p[i+1]);
    }

    for(int i=0; i<n; i++)
    {
        dp[i][i]=0;

    }
    for(int len=2;len<=n;len++)
    {
        for(int i=0;i<n-len+1;i++)
        {
            int j=i+len-1;
            dp[i][j]=INF;
            for(int k=i;k<j;k++)
            {
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i]*p[k+1]*p[j+1]);
            }
        }
    }
    printf("%d\n",dp[0][n-1]);

    return 0;
}