Go: package main

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

const N = 100

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

func main() {
	var n int
	fmt.Scan(&n)
	p := make([]int, N+1)
	m := make([][]int, N+1)
	for i := range m {
		m[i] = make([]int, N+1)
	}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		scanner.Scan()
		p[i-1], _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		p[i], _ = strconv.Atoi(scanner.Text())
	}
	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
			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])
			}
		}
	}
	fmt.Println(m[1][n])
}

Python: n = int(input())
p = list(range(n+1))

for i in range(n):
    p[i], p[n] = map(int, input().split())

# table[i][j] : M_i??????M_j??????????????????????°?????????°
table = [[0] * n for i in range(n)]

# ??£????????£???2????????????????????????
for i in range(n-1):
    table[i][i+1] = p[i]*p[i+1]*p[i+2]


for k in range(3, n+1):    # ?????????k???????????????????????????
    for i in range(n+1-k): # M_i?????????
        j = i + k - 1      # M_j?????§???k??????

        table[i][j] = float('inf')

        for x in range(i, j+1): # M_i~M_x??¨M_(x+1)~M_j??§????????????(i<=x<=k)
            cost = table[i][x] + table[x+1][j] + p[i]*p[x+1]*p[j+1]
            table[i][j] = min(table[i][j], cost)
            

print(table[1][n])

Python: n = int(input())
p = []
s = input().split()
p.append(int(s[0]))
p.append(int(s[1]))
for _ in range(n - 1):
    s = input().split()
    p.append(int(s[1]))

m = [[-1 for _ in range(n)] for _ in range(n)]
for i in range(n):
    m[i][i] = 0

for l in range(2, n + 1):
    for i in range(0, n - l + 1):
        j = i + l - 1
        for k in range(i, j):
            x = m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1]
            if m[i][j] < 0:
                m[i][j] = x
            elif m[i][j] > x:
                m[i][j] = x

print(m[0][n - 1])

Python: #!/usr/bin/python3
n = int(input())
M = [tuple(list(map(int, input().split()))) for i in range(n)]

# best[i][i + j] : M_i * M_i+1 * ... * M_i+j?????????cost??¨??????
best = [[float('inf') for j in range(n)] for i in range(n)]
for j in range(0, n):
    for i in range(n - j):
        l = []
        for k in range(j):
            c1 = best[i][i + k]
            c2 = best[i + k + 1][i + j]
            c3 = M[i][0] * M[i + k][1]  * M[i + j][1]
            l.append(c1 + c2 + c3)
        best[i][i + j] = 0 if len(l) == 0 else min(l)
print(best[0][n-1])

Python: n = int(input())
m = [list(map(int, input().split())) for _ in range(n)]

p = [m[0][0]]
for i in range(n):
    p.append(m[i][1])

INF = 1 << 60
dp = [[INF] * n for _ in range(n)]

for i in range(n):
    dp[i][i] = 0

for x in range(1, n):
    for i in range(n - x):
        j = i + x
        for k in range(i, j):
            dp[i][j] = min((dp[i][j], dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1]))

print(dp[0][-1])

Python: def solve(a, b):
    global dp
    if a == b:
        return 0
    elif dp[a][b]:
        return dp[a][b]
    ret = INF
    for i in range(a, b):
        tmp = solve(a, i) + solve(i + 1, b) + c[i] * r[a] * c[b]
        ret = min(ret, tmp)
    dp[a][b] = ret
    return ret

n = int(input())
r = [0] * n;  c = [0] * n;
for i in range(n):
    r[i], c[i] = [int(x) for x in input().split()]

INF = float('inf')
dp = [[0 for _ in range(n)] for _ in range(n)]
print(solve(0, n - 1))

Python: import sys

n = int(input())

c = [0]*(n+1)
c[0],c[1] = map(int,input().split())

for i in range(2,n+1):
    c[i] = int(input().split()[1])

dp = [[sys.maxsize]*(n+1) for i in range(n+1)]

for i in range(1,n+1):
    dp[i][i] = 0

for l in range(2,n+1) :#length of chain j-i+1
    for i in range(1,n-l+2):
        j = i +l - 1
        for k in range(i,j):
            dp[i][j] = min(dp[i][j],dp[i][k]+ dp[k+1][j]+c[i-1]*c[k]*c[j])


print(dp[1][n])

Python: #区間[l,r]での最小値は?
#区間[l,l+mid]の最小値+[l+mid+1,r]の最小値+midに依存した計算回数
#(Mi Mj)*(Mj Mk)の計算回数はijk

def kuDP(l,r):
  global dp
  
  if dp[l][r] != -1:
    return dp[l][r]
  
  if l == r:
    dp[l][r] = 0
    return 0
  
  if l+1 == r:
    c = M[l][0]*M[l][1]*M[r][1]
    dp[l][r] = c
    return c
  
  midl = [10**10]*(r-l)
  for mid in range(r-l):
    midl[mid] = kuDP(l,l+mid) + kuDP(l+mid+1,r) + M[l][0]*M[l+mid][1]*M[r][1]
    
  c = min(midl)
  
  dp[l][r] = c
  
  return c

N = int(input())
M = [list(map(int,input().split())) for _ in range(N)]
dp = [[-1]*N for _ in range(N)]
print(kuDP(0,N-1))

Python: #!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def main():
    num_mat = int(stdin.readline())
    data = [int(s) for s in stdin.readline().split()]
    for _ in range(num_mat-1):
        _, row = (int(s) for s in stdin.readline().split())
        data.append(row)
    num_dat = len(data)

    bin_op = tuple(data[i]*data[i+1]*data[i+2] for i in range(num_dat-2))
    parts = [(data[0]*data[1], data[-2]*data[-1]), bin_op]

    for j in range(3, num_mat):
        part = parts[-1]
        new_part = []
        for i in range(num_dat-j):
            a = data[i]
            b = data[i+1]
            c = data[i+j-1]
            d = data[i+j]
            new_part.append(min(part[i] + a*c*d, part[i+1] + a*b*d))
        parts.append(new_part)

    L = data
    if num_mat < 2:
        print(0)
    elif num_mat < 3:
        print(data[0]*data[1]*data[2])
    else:
        print(min(parts[i][0] + parts[-(i+1)][-1] +
                  L[0]*L[i+1]*L[-1] for i in range(len(parts))))


main()

Python: n = int(input())
p0, p1 = list(map(int, input().split()))
p = [p0, p1]
for i in range(n-1):
    pi = list(map(int, input().split()))
    p.append(pi[1])
    
dp = [[10**10]*(n+2) for _ in range(n+2)]

for i in range(0, n+2):
    dp[i][i] = 0

for l in range(2,n+1):
    for i in range(1, n-l+2):
        j = i + l - 1
        for k in range(i, j):
            dp[i][j] = min([dp[i][j], 
              dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j]])

print(dp[1][n])

Python: #!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import re
import math
import collections
import itertools
import functools
import logging

logging.basicConfig(level=logging.DEBUG, format="DBG: %(message)s")

def dbg(msg, *args, **kwargs):
    logging.debug(msg, *args, **kwargs)

Mat = collections.namedtuple("Mat", "r c")

def mat_mul(ms):
    return Mat(ms[0].r, ms[-1].c)

def cost(m1, m2):
    return m1.r * m1.c * m2.c

def solve(n, ms):
    if n == 1: return 0

    dp = {}
    for i in range(n):
        dp[(i,i)] = 0

    for l in range(2, n+1):
        for i in range(n-l+1):
            cost_min = math.inf
            for j in range(l-1):
                dbg("l, i, j: %d %d %d", l, i, j)
                m_lhs = mat_mul(ms[i:i+j+1])
                m_rhs = mat_mul(ms[i+j+1:i+l])
                dbg("m_lhs, m_rhs: %r %r", m_lhs, m_rhs)
                cost_lhs = dp[(i,i+j)]
                cost_rhs = dp[(i+j+1,i+l-1)]
                dbg("cost_lhs, cost_rhs: %d %d", cost_lhs, cost_rhs)
                cost_mul = cost(m_lhs, m_rhs)
                dbg("cost_mul: %d", cost_mul)
                cost_now = cost_mul + cost_lhs + cost_rhs
                dbg("cost_now: %d", cost_now)
                if cost_min > cost_now:
                    cost_min = cost_now
            dp[(i,i+l-1)] = cost_min

    dbg(dp)
    return dp[(0,n-1)]

def main():
    n = int(input())

    ms = []
    for _ in range(n):
        r,c = map(int, input().split())
        ms.append(Mat(r,c))

    print(solve(n, ms))

if __name__ == "__main__": main()