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"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.txt"
 
int memo[100][100];
vector<pair<int, int>>ps;
int getans(const int l, const int r) {
	if (memo[l][r] != -1)return memo[l][r];
	if (l + 1 == r) {
		return memo[l][r] = ps[l].first*ps[l].second*ps[r].second;
	}
	else if (l == r) {
		return memo[l][r] = 0;
	}
	else {
		int ans = 1e9;
		for (int t = l; t < r; ++t) {
			int len = getans(l, t) + ps[l].first*ps[t].second*ps[r].second + getans(t + 1, r);
			ans = min(ans, len);
		}
		return memo[l][r] = ans;
	}
}
int main() {
	for (int i = 0; i < 100; ++i) {
		for (int j = 0; j < 100; ++j) {
			memo[i][j] = -1;
		}
	}
	int N; cin >> N;
	ps.resize(N);
	for (int i = 0; i < N; ++i) {
		int a, b; cin >> a >> b;
		ps[i] = make_pair(a, b);
	}
	int ans=getans(0, N - 1);
	cout << ans << endl;
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int mat[110][2], dp[110][110];
const int INF = 1<<29;

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  REP(i, N) REP(j, 2) cin >> mat[i][j];
  REP(i, N) REP(j, N) dp[i][j] = INF;
  REP(i, N) dp[i][i] = 0;
  for (int w = 1; w < N; w++) {
    for (int L = 0; L + w < N; L++) {
      int R = L + w;
      for (int i = L; i < R; i++)
	dp[L][R] = min(dp[L][R], dp[L][i] + dp[i+1][R] + mat[L][0] * mat[i][1] * mat[R][1]);
    }
  }
  cout << dp[0][N-1] << endl;
  return 0;
}

C++: #include <iostream>

using namespace std;

typedef struct matrix{
  int row;
  int column;
  struct matrix *next;
  struct matrix *back; 
}Matrix;

Matrix *push_back(Matrix *begin, int row, int column) {
  Matrix *p = (Matrix*)malloc(sizeof(Matrix));
  p->row = row;
  p->column = column;
  p->next = NULL;
  p->back = NULL;
  if(begin == NULL) return p;
  Matrix *p2 = begin;
  while(p2->next != NULL) p2 = p2->next;
  p2->next = p;
  p->back = p2;
  return begin;
}

Matrix *product_count(Matrix *x, int type, long *count) {
  Matrix *a,*b;
  if(type == 0) {
    a = x->back;
    b = x;
  }
  else {
    a = x;
    b = a->next;
  }
  *count = 0;
  if(a == NULL || b == NULL) return x;
  *count += a->row * a->column * b->column;
  Matrix *c = (Matrix*)malloc(sizeof(Matrix));
  c->row = a->row;
  c->column = b->column;
  c->next = b->next;
  c->back = a->back;
  if(a->back != NULL) a->back->next = c;
  if(b->next != NULL) b->next->back = c;
  free(a);
  free(b);
  return c;
}

int main()
{
  int i,n,r,c;
  Matrix *begin = NULL;
  scanf("%d",&n);
  for (i = 0; i < n; i++) {
    scanf("%d %d",&r,&c);
    begin = push_back(begin,r,c);
  }
  Matrix *min = begin, *p = begin;
  while (p->next != NULL) {
    if(min->column > p->column) min = p;
    p = p->next;
  }
  long sum = 0,count;
  if(min->column <= begin->row) {
    begin = NULL;
    while(1) {
      min = product_count(min,0,&count);
      if(count != 0) sum += count;
      else break;
    }
    if(min->next != NULL) {
      min = min->next;
      while(1) {
	min = product_count(min,1,&count);
	if(count != 0) sum += count;
	else break;
      }
      min = product_count(min,0,&count);
      sum += count;
    }
  }
  else{
    min = begin;
    begin = NULL;
    while(1) {
      min = product_count(min,1,&count);
      if(count != 0) sum += count;
      else break;
    }
  }
  cout << sum << endl;
  
  return 0;
}

C++: #include<iostream>
using namespce std;

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

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

int main()
{
	int n, p = 0, m = 101, mini;
	int v[101];

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> v[i] >> v[i + 1];
		if (m > v[i]) {
			m = v[i];
			mini = i;
		}
	}

	if (n > 1) {
		for (int j = 0; j < n; j++)
			p += v[j] * v[j + 1];
		p += v[0] * v[n];

		if (mini == 0)
			p -= v[0] * v[1];
		else if (mini == n)
			p -= v[n - 1] * v[n];
		else
			p -= v[mini] * (v[mini + 1] + v[mini - 1]);

		p *= v[mini];
	}

	cout << p << endl;

	return 0;
}

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

#define REP(i, n) for(int i = 0; i < (n); i++)

typedef pair<int, int> pii;

const int N_SIZE = 100;

int dp[N_SIZE + 1][N_SIZE + 1];

void solve(int n, pii rc[])
{
  REP(i, n)
    dp[i][i] = rc[i].first * rc[i].second;
  REP(i, n - 1)
    dp[i][i + 1] = dp[i][i] * rc[i + 1].second;
  for(int i = 2; i < n; i++){
    for(int j = 0, k = i + j, m = 100000000; k < n; j++, k++){
      for(int l = j, x; l < k; l++){
	x = ((j == l) ? dp[j][l] * rc[k].second : dp[j][l]) + ((l + 1 == k) ? dp[l + 1][k] * rc[j].first : dp[l + 1][k]);
	if(j != l && l + 1 != k)
	  x += rc[j].first * rc[l].second * rc[k].second;
	m = min(m, x);
      }
      dp[j][k] = m;
      m = 100000000;
    }
  }
}
 
int main()
{
  int n;
  cin >> n;
  pii rc[n];
  REP(i, n)
    cin >> rc[i].first >> rc[i].second;
  if(n == 1){
    cout << "0" << endl;
  }else{
    solve(n, rc);
    cout << dp[0][n - 1] << endl;
  } 
}

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

int main() {
    int n, m[101][101], p[101];
    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] = 200000;
            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;
}