Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(m[0][n-1])
}

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int main(){
    int n;
    cin >> n;

    int len[n+1];
    rep(i, n)cin >> len[i] >> len[i+1];

    ll dp[105][105] = {};//lからrまで行列積の最小の乗算回数

    //連鎖行列積を求める数i
    for(int i = 2; i <= n; ++i){
        for(int l = 1; l+i-1 <= n; ++l){
            int r = i+l-1;
            dp[l][r] = INF;
            //(M_l*...*M_c)*(M_c+1*...*M_r)
            for(ll c = l; c < r; ++c){
                dp[l][r] = min(dp[l][r], dp[l][c] + dp[c+1][r] + len[l-1]*len[c]*len[r]);
            }
        }
    }

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

C++: #include <bits/stdc++.h>
#define N 101
using namespace std;
int A[N];

int mem[N][N],used[N][N];
int dfs(int l,int r){
  if(l == r) return 0;
  if(r - l == 1) return A[l]*A[r]*A[r+1];
  if(used[l][r]++) return mem[l][r];

  int res = 1e9;  
  for(int i=l;i<r;i++){
    int a = dfs(l,i);
    int b = dfs(i+1,r);
    int c = A[l] * A[i+1] * A[r+1];
    res = min(res,a + b + c);
  }
  return mem[l][r] = res;
}

int main(){
  
  int n;
  cin>>n;
  for(int i=0,cnt=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    if(i == 0) A[cnt++] = a;
    A[cnt++] = b;
  }

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


  return 0;
}

C++: #include <cstdio>
int N, MEMO[100][100], V[100][100];
struct info {
    int r;
    int c;
};
struct info MAT[100];
int DP(int i, int j);
void input(void);
int main(void){
    freopen("in.txt", "r", stdin);
    input();
    printf("%d\n", DP(0, N - 1));
    return 0;
}
int DP(int i, int j){
    if (V[i][j] == 1) return MEMO[i][j];
    V[i][j] = 1;
    if (i >= j) return 0;
    int min = 987654321;
    for(int k = i; k < j; k++){
        int cnd = DP(i, k) + DP(k + 1, j) + MAT[i].r * MAT[k].c * MAT[j].c;
        if (cnd < min) min = cnd;
    }
    MEMO[i][j] = min;
    return min;
}
void input(void){
    scanf("%d", &N);
    for(int i = 0; i < N; i++) scanf("%d%d", &MAT[i].r, &MAT[i].c);
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define mp make_pair
#define fi first
#define se second

int main()
{
    int n;cin >> n;
    vector<pair<int,int>> matrix(n);
    int a,b;
    for (int i=0;i<n;i++)
    {
        cin >> a >> b;
        matrix[i]=mp(a,b);
    }
    vector<vector<pair<int,pair<int,int>>>> dp(n,vector<pair<int,pair<int,int>>> (n,mp(INF,mp(-1,-1))));
    for (int i=0;i<n;i++)
    {
        dp[i][i].fi=0;
        dp[i][i].se.fi=matrix[i].fi;
        dp[i][i].se.se=matrix[i].se;
    }
    for (int len=1;len<n;len++)
    {
        for (int l=0;l<n-len;l++)
        {
            int r=l+len;
            //cout << l << r << endl;
            for (int m=l;m<r;m++)
            {
                if (dp[l][r].fi>dp[l][m].fi+dp[m+1][r].fi+dp[l][m].se.fi*dp[l][m].se.se*dp[m+1][r].se.se)
                {
                    //cout << l <<" " << r << ":" ;
                    dp[l][r].fi=dp[l][m].fi+dp[m+1][r].fi+dp[l][m].se.fi*dp[l][m].se.se*dp[m+1][r].se.se;
                    dp[l][r].se.fi=dp[l][m].se.fi;
                    dp[l][r].se.se=dp[m+1][r].se.se;
                    //cout << dp[l][r].fi<< endl;
                }
            }
        }
    }
    cout << dp[0][n-1].fi << endl;
    return 0;
}

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

constexpr int INF = 1e9;

int dp[110][110];

int main(){
  int n; cin >> n;
  int p[110];
  for( int i = 0; i < n; i++ ) cin >> p[i] >> p[i + 1];
  for( int i = 0; i < 110; i++ ){
    for( int j = 0; j < 110; j++ ){
      if( i != j ) dp[i][j] = INF;
    }
  }
  
  for( int l = 2; l <= n; l++ ){
    for( int i = 0; i < n - l + 1; i++ ){
      int j = l + i - 1;
      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] );
      }
    }
  }

  cout << dp[0][n - 1] << endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define INF 1<<30

static const int N = 100;

int main() {
    int n;
    int 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] = INF;
            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;

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;
#define 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[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}
	cout << m[1][n] << endl;
	return 0;
}