Go: package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)

	P := make([]int, n+1)
	for i := 1; i <= n; i++ {
		l := nextInt(sc)
		P[i-1] = l
		m := nextInt(sc)
		P[i] = m
	}

	matrix := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		matrix[i] = make([]int, n+1)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n - l + 1; i++ {
			j := i + l - 1
			matrix[i][j] = (1 << 21)
			for k := i; k <= j-1; k++ {
				matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k+1][j] + P[i-1] * P[k] * P[j])
			}
		}
	}
	fmt.Println(matrix[1][n])
}

Python: def divide_list(l):
    divided_list = [[None, None] for _ in range(len(l) - 1)]
    for i, dl in enumerate(divided_list):
        dl[0], dl[1] = l[0:(i+1)], l[(i+1):]
    return divided_list


def mat_chain_mult(idx_list):
    global a
    global c

    start, end = idx_list[0], idx_list[-1]


    if c[start][end] is not None:
        return c[start][end]

    if len(idx_list) == 1:
        c[start][end] = 0
    elif len(idx_list) == 2:        
        c[start][end] = a[start] * a[start+1] * a[start+2]
    else:
        divided_list = divide_list(idx_list)
        cc = a[start] * a[end+1]
        
        c[start][end] = min([mat_chain_mult(dl0) + mat_chain_mult(dl1) + cc * a[len(dl0) + start] for dl0, dl1 in divided_list])
    
    return c[start][end]
    

if __name__ == "__main__":
    n = int(input())
    a = [0] * (n + 1)
    c = [[None] * n for _ in range(n)]

    for i in range(n-1):
        a[i] = list(map(int, input().split()))[0]
    a[n-1], a[n] = list(map(int, input().split()))

    print(mat_chain_mult(list(range(n))))

Python: INF = float("inf")
n = int(input())
A = [0]
for i, a in enumerate(sys.stdin):
    a = list(map, a.split())
    if i == 0:
        A.extend(a)
    else:
        A.append(a[1])
C = [[INF] * (n + 1) for _ in range(n + 1)]
for i in range(n + 1):
    C[i][i] = 0
for j in range(1, n):
    for i in range(1, n - j + 1):
        for k in range(i + 1, i + j + 1):
            cost = C[i][k - 1] + C[k][i + j] + A[i] * A[k] * A[i + j + 1]
            if cost < C[i][i + j]:
                C[i][i + j] = cost
print(C[1][n])

Python: 
n = int(input())
p = [None] * (n + 1)
m = [[None for i in range(n+1)] for j in range(n+1)]

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

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

# print(p)
# print(m)

for l in range(2, n+1):  # lはchainの長さ。最終的にl = n。
    for i in range(1, n-l+2):  # i = チェーンの左側
        j = i + l - 1 # j = チェーンの右端
        m[i][j] = 1000000000

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

print(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()