Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Matrix-chain Multiplication

  The goal of the matrix-chain multiplication problem is to find the most efficient way to multiply given $n$ matrices $M_1, M_2, M_3,...,M_n$.

  Write a program which reads dimensions of $M_i$, and finds the minimum number of scalar multiplications to compute the maxrix-chain multiplication $M_1M_2...M_n$.

Input

  In the first line, an integer $n$ is given. In the following $n$ lines, the dimension of matrix $M_i$ ($i = 1...n$) is given by two integers $r$ and $c$ which respectively represents the number of rows and columns of $M_i$.

Output

  Print the minimum number of scalar multiplication in a line.

Constraints

$1 \leq n \leq 100$

$1 \leq r, c \leq 100$

Sample Input 1

6
30 35
35 15
15 5
5 10
10 20
20 25

Sample Output 1

15125



Sample input:
6
30 35
35 15
15 5
5 10
10 20
20 25


Sample output:
15125

Submission status: Accepted

Solution code:
class Mychain:
    # 名前はこれでいいんだろうか・・・

    def __init__(self, n):
        self.n = n
        self.lst = [0 for _ in range(n * (n + 1) // 2)]

    def get_index(self, i, j):
        return i - 1 + ((j - i) * (2*self.n - j + i + 1) // 2)

    def get_value(self, i, j):
        return self.lst[self.get_index(i, j)]

    def set_value(self, i, j, value):
        index = self.get_index(i, j)
        self.lst[self.get_index(i, j)] = value


n = int(input())
res = Mychain(n)
matrix = [int(i) for i in input().split()]
matrix.extend([int(input().split()[1]) for i in range(n - 1)])


for n1 in range(1, n + 1):
    for i in range(1, n - n1 + 1):
        j = n1 + i
        value = min(res.get_value(i, i + k) + res.get_value(i + k + 1, j)
                    + matrix[i - 1] * matrix[i + k] * matrix[j]
                    for k in range(n1))
        res.set_value(i, j, value)

print(res.get_value(1, n))

Submission status: Accepted

Solution code:
def matrix_chain_order(p):
    n = len(p)
    m = {}
    for length in range(1, n + 1):
        for i in range(n - length + 1):
            j = i + length - 1
            if i == j:
                m[(i, j)] = 0
            else:
                q = None
                for k in range(i, j):
                    c = m[(i, k)] + m[(k+1, j)] + p[i][0] * p[k][1] * p[j][1]
                    if q is None or c < q:
                        q = c
                m[(i, j)] = q
    return m[(0, n-1)]

if __name__ == '__main__':
    import sys

    p = []
    n = int(input())
    for _ in range(n):
        p.append(tuple(map(int, sys.stdin.readline().split())))
    print(matrix_chain_order(p))

Submission status: Accepted

Solution code:
N = int(input())

for i in range(N):
    if i == 0:
        P = list(map(int, input().split()))  # Pは各行列の次元
        continue
    P.append(int(input().split()[1]))

# dpテーブルの用意 minを取るときはinf埋めしておくと処理が楽になるしバグが見つかりやすい
#dp[i][j]は(Mi...Mj)のminコストを入れる
INF = 2**32-1
dp = [[INF]*N for _ in range(N)]

#dpテーブルの対角成分を0で初期化
#dp[s][s]はMsのminコストを表す（Msは単独の行列）
for ij in range(N):
    dp[ij][ij] = 0

# 最終的に求めたいのはdp[0][N]だが、計算途中で繰り返すそれぞれの行列積をdpに入れておくことで効率化
for l in range(1, N):  # l は対象とする行列の個数を表す
    for i in range(N):
        j = i+l   # Mj はMiから何個の行列を考えるかで決まる
        if j >= N:  # 計算するのはインデックスはN-1まで
            continue
        for k in range(i, j):  # (Mi...Mk)(Mk+1...Mj) のようにどこでMkで区切るかを動かす
            dp[i][j] = min(dp[i][k] + dp[k+1][j] + P[i] * P[j+1]*P[k+1], dp[i][j]) # 
print(dp[0][-1])

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    m1 = input().split()
    ps = [int(m1[0]), int(m1[1])]
    for _ in range(n - 1):
        ps.append(int(input().split()[1]))
    ms = [[float("inf") for _ in range(n)] for __ in range(n)]
    # 範囲 0 - n
    for i in range(n):
        ms[i][i] = 0

    for length in range(1, n):
        # 00 11 22 33 44 ...         nn
        # 01 12 23 34 45 ...      n-1,n
        # 02 13 24 35 46 ... n-2,n
        # ...
        # 0n
        for start in range(0, n - length):
            end = start + length
            # 仕切りpartition
            m_start_end = float("inf")
            for partition in range(start, end):
                m_start_end = min(m_start_end, ms[start][partition] + ms[partition + 1]
                                  [end] + ps[start] * ps[partition + 1] * ps[end + 1])
            ms[start][end] = m_start_end
    print(ms[0][n - 1])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
INF = 2000000000

def matrixChainMultiplication(m):
    global n
    for l in range(2, n+1):
        for i in range(1, n-l + 2):
            j = i + l - 1
            m[i][j] = INF
            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])
    return m
 
    
n = int(input())

m = [[0 for i in range(n + 1)] for j in range(n + 1)]
p = []


for i in range(n):
    input_command = input()
    num1,num2 = input_command.split()
    p.append(int(num1))
    if i == n-1:
        p.append(int(num2))
        
m = matrixChainMultiplication(m)
print(m[1][n])

Submission status: Accepted

Solution code:
import sys


class Mat(object):

  def __init__(self, row, col):
    self.row = row
    self.col = col
    
def get_min_mult_cnt_dp(i, j, mat_list):

  multp = [[sys.maxsize for i in range(len(mat_list))] for i in range(len(mat_list))]

  

  # 長さ0連鎖の演算量
  for i in range(len(mat_list)):
    multp[i][i] = 0

  for le in range(1, len(mat_list)):
    for i in range(len(mat_list)-le):
      j = i + le

      tmp = sys.maxsize
      for k in range(i, j):
        p = mat_list[i].row
        n = mat_list[k].col
        q = mat_list[j].col
        tmp = multp[i][k] + multp[k+1][j] + p*n*q

        if (tmp < multp[i][j]):
          multp[i][j] = tmp
    
  
  return multp[i][j]
mat_list = []
num = int(input())
for i in range(num):
    a,b = map(int,input().split())
    mat_list.append(Mat(a,b))



print(int(get_min_mult_cnt_dp(0, num-1, mat_list)))

Submission status: Accepted

Solution code:
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))))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    def get_cost(l,r):
        if r-l==1:return 0
        if r-l==2:return rc[l][0]*rc[r-1][0]*rc[r-1][1]
        if cost[l][r]!=-1:
            return cost[l][r]
        res=cost[l][r]=min(get_cost(l,m)+get_cost(m,r)
                           +rc[l][0]*rc[m][0]*rc[r-1][1] for m in range(l+1,r))
        return res

    n=int(input())
    rc=[list(map(int,input().split())) for _ in range(n)]
    cost=[[-1]*(n+1) for _ in range(n)]
    print(get_cost(0,n))

main()

Submission status: Accepted

Solution code:

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])

Submission status: Accepted

Solution code:
def min_cal(A):
    n=len(A)
    ls=[[0 for i in range(n)] for i in range(n)]
    for i in range(n):
        ls[i][i]=0
    for i in range(1,n):
        for j in range(n-i):
            ls[j][i+j]=min([ls[j][k]+ls[k+1][i+j]+A[k][1]*A[j][0]*A[i+j][1] for k in range(j,i+j)])
    return ls[0][n-1]

n=int(input())
Mats=[]
for i in range(n):
    Mats.append([int(i) for i in input().split()])

print(min_cal(Mats))

Submission status: Accepted

Solution code:
def matrixChainMultiplication(n):
    # for i in range(1, n+1):
    #     m[i][i] = 0
    for l in range(2, n+1):
        for i in range(1, n-l+1+1):
            j = i + l - 1
            m[i][j] = 999999999
            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])
    return m[1][n]

n = int(input())
squareM = [input().split(" ") for _ in range(n)]
p = [int(squareM[i][0]) for i in range(n)]
p.append(int(squareM[n-1][1]))
m = [[0]*(n+1) for _ in range(n+1)]
print(matrixChainMultiplication(n))

Submission status: Accepted

Solution code:
INF = float('inf')


def main():
    n = int(input())
    p = [0 for _ in range(n+1)]

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

    dp = [[0 for _ in range(n+1)] for _ in range(n+1)]

    for l in range(2, n+1):
        for i in range(1, n-l+2):
            j = i + l - 1
            dp[i][j] = INF
            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])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import itertools as ite
import math
import random
import sys
import codecs
from collections import defaultdict

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

n = input()
A = map(int, raw_input().split())
for i in range(n - 1):
    n1, n2 = map(int, raw_input().split())
    A += [n2]
DP = [[INF] * n for i in range(n)]
for i in range(n):
    DP[i][i] = 0

for L in range(1, n):
    for i in range(n - L):
        for j in range(L):
            DP[i][i + L] = min(DP[i][i + L], DP[i][i + j] + DP[i + j + 1][i + L] + A[i] * A[i + j + 1] * A[i + L + 1])
print DP[0][n - 1]

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)




n=int(input())
M=[0]*(n+1)
for i in range(n):
    M[i],M[i+1]=map(int,input().split())
dp=[[float('inf')]*n for i in range(n)]
for i in range(n):
    dp[i][i]=0
for k in range(1,n):
    for i in range(n-k):
        j=i+k
        for l in range(i,j):
            dp[i][j]=min(dp[i][j],dp[i][l]+dp[l+1][j]+M[i]*M[l+1]*M[j+1])
print(dp[0][n-1])
# print(dp)

Submission status: Accepted

Solution code:
# https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/10/ALDS1_10_B
# 普通に難しい
# アイデアは写真の解説を参照

# データの入力
N = int(input())

for i in range(N):
    if i == 0:
        P = list(map(int, input().split()))
        continue
    P.append(int(input().split()[1]))

# dpテーブルの用意 minを取るときはinf埋めしておくと処理が楽になるしバグが見つかりやすい
INF = 2**32-1
dp = [[INF]*N for _ in range(N)]

# dpテーブルの初期化
# 対角成分0
for ij in range(N):
    dp[ij][ij] = 0

# dpの更新 どう更新しているのか、なんでこう更新するのかは写真を参照
for l in range(1, N):  # lは対角成分からいくつ離れているか (なん個行列をかけるかという解釈も可)
    for i in range(N):
        j = i+l
        if j >= N:
            continue
        for k in range(i, j):
            dp[i][j] = min(dp[i][k] + dp[k+1][j] + P[i]
                           * P[j+1]*P[k+1], dp[i][j])
# from pprint import pprint
# pprint(dp)
print(dp[0][-1])

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    p = []
    for i in range(N):
        r, c = (int(i) for i in input().split())
        p.append(r)
        if i == N - 1:
            p.append(c)
    memo = [[float('inf') for j in range(N + 1)] for i in range(N + 1)]

    def matrix_chain_order():
        for i in range(1,N+1):
            memo[i][i] = 0

        for l in range(2,N+1):
            for i in range(1, N - l + 1 + 1):
                j = i + l - 1
                for k in range(i, j):
                    q = memo[i][k] + memo[k + 1][j] + p[i-1]*p[k]*p[j]
                    if q < memo[i][j]:
                        memo[i][j] = q

    matrix_chain_order()
    print(memo[1][N])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def matrixChainMultiplication():
	INF = 100 * 100 * 100 + 1
	m = [[INF for _ in range(n + 1)] for _ in range(n + 1)]
	for i in range(1, n + 1):
		m[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):
				q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]
				if q < m[i][j]:
					m[i][j] = q
	return m[1][n]

if __name__ == "__main__":
	n = int(input())
	p = list(map(int, input().split()))
	for i in range(1, n):
		p.append(list(map(int, input().split()))[1])
	print(matrixChainMultiplication())

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
INF = 1001001001001001
def main():
    n = int(input())
    p = [0] * (n+1)
    for i in range(n):
        p[i], p[i+1] = map(int, input().split())
    dp = [[INF]*(n+1) for _ in range(n+1)]
    for i in range(n+1):
        dp[i][i] = 0
    for l in range(2, n+1):
        for i in range(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])
if __name__ == '__main__': main()

Submission status: Accepted

Solution code:
import sys

n = int(input())
m = []
for i in range(n):
    inpt = list(map(int, input().split( )))
    m.append(inpt)

lrg = 1000000
DP = [[lrg for i in range(n)] for j in range(n)]


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

if n >= 1:
    for i in range(n-1):
        DP[i][i+1] = m[i][0]*m[i][1]*m[i+1][1]

for d in range(2,n):
    for i in range(n-d):
        for j in range(i+1,i+d):
            DP[i][i+d] = min(DP[i][i+d],DP[i][j]+DP[j+1][i+d]+m[i][0]*m[j][1]*m[i+d][1])

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

Submission status: Accepted

Solution code:
import sys


def getmin(i, j, mlist, memo={}):
    if i == j:
        return 0
    elif (i, j) in memo:
        return memo[(i, j)]
    else:
        ret = sys.maxsize
        for k in range(i, j):
            p = mlist[i][0]
            n = mlist[k][1]
            q = mlist[j][1]
            tmp = getmin(i, k, mlist, memo) + \
                getmin(k+1, j, mlist, memo) + p*n*q

            if tmp < ret:
                ret = tmp

        memo[(i, j)] = ret
        return memo[(i, j)]


def main():
    n = int(input())
    M = [[0, 0] for i in range(n)]
    for i in range(n):
        M[i] = list(map(int, input().split()))

    print(getmin(0, n-1, M))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    p = [0] * (n + 1)
    m = [[0] * (n + 1) for _ in range(n + 1)]
    for i in range(n):
        p[i], p[i + 1] = map(int, input().split())
    for l in range(2, n + 1):
        for i in range(1, n - l + 2):
            j = i + l - 1
            m[i][j] = float('inf')
            for k in range(i, j):
                p_prod = p[i - 1] * p[k] * p[j]
                m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p_prod)
    print(m[1][n])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
r=[0]*(n+1)
c=[0]*(n+1)
for i in range(n):
    r[i+1], c[i+1] = map(int, input().split())

dp = [[float('inf') for i in range(n+1)] for j in range(n+1)]

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

# print("")
for l in range(2, n+1):
    for i in range(1, n-l+2):
        # print("l=", l, "i=", i)
        tmp_list = [r[i] * c[i+l-1] * r[j+1] + dp[i][j] + dp[j+1][i+l-1] for j in range(i, i+l-1)]
        dp[i][i+l-1] = min(tmp_list)

print(dp[1][n])

Submission status: Accepted

Solution code:
import sys
from typing import List


if __name__ == "__main__":
    num_matrix = int(input())
    matrices: List[List[int]] = []
    for _ in range(num_matrix):
        matrices.append(list(map(lambda x: int(x), input().split())))
    dp = [[sys.maxsize] * num_matrix for i in range(num_matrix)]
    for i in range(num_matrix):
        dp[i][i] = 0

    for l in range(1, num_matrix):
        for i in range(num_matrix - l):
            row = matrices[i][0]
            col = matrices[i + l][1]
            dp[i][i + l] = min(row * matrices[j][1] * col + dp[i][j] + dp[j + 1][i + l]
                               for j in range(i, i + l))

    print(f"{dp[0][num_matrix - 1]}")

Submission status: Accepted

Solution code:

INF = 10**9

if __name__ == '__main__':
    n = int(raw_input())

    # dp[i][j]: minimal cost of M[i]...M[j]
    # for example, the minimal cost of M[1]M[2]M[3]M[4] is the smallest of the 3 cases below:
    # (M[1])(M[2]M[3]M[4])
    # (M[1]M[2])(M[3]M[4])
    # (M[1]M[2]M[3])(M[4])
    # where M[i] represents a (p[i-1] x p[i]) matrix, 1<=i<=n
    # clearly the cost of multiplying only one matrix (M[i]) is zero, so dp[i][i] = 0
    dp = [[0 for j in range(n+1)] for i in range(n+1)]  # 1-origin
    p = [0 for i in range(n+1)]  # 0-origin
    for i in range(1, n+1):
        p[i-1], p[i] = map(int, raw_input().split())

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

    # l: multiply l consecutive matrices
    for l in range(2, n+1):
        # each group of l matrices are M[i]...M[j], where j-i+1=l, j=l+i-1
        # j<=n, l+i-1<=n, i<=n-l+1
        for i in range(1, n-l+1+1):
            j = l+i-1
            minv = INF
            for k in range(i, j):
                # divide into M[i]...M[k], which is a (p[i-1] x p[k]) matrix
                # and M[k+1]...M[j], which is a (p[k] x p[j]) matrix
                # so multiply the two costs p[i-1]*p[k]*p[j]
                minv = min(minv, dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j])
            dp[i][j] = minv
    print dp[1][n]

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
a = []
dp = []

#l <= x < rまでの行列せきの最小値
def rec(l, r):
	if dp[l][r] != -1:
		return dp[l][r]
	if l+1 == r:
		dp[l][r] = 0
		return dp[l][r]
	if l+2 == r:
		dp[l][r] = a[l][0] * a[l][1] * a[l+1][1]
		return dp[l][r]

	m = 100000000
	for i in range(l+1, r):
		m = min(m, rec(l, i) + rec(i, r) + a[l][0] * a[i][0] * a[r-1][1])
	dp[l][r] = m
	return dp[l][r]

def main():
	global a, dp
	n = int(input().strip())
	for i in range(n):
		a.append(tuple(map(int, input().strip().split())))
	dp = [[-1] * (n+1) for _ in range(n)]
	print(rec(0, n))

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
INFTY = 100 * 100 * 100 + 1  # 1 ≤ r,c ≤ 100


n = int(input())

p = [None for _ in range(n + 1)]
m = [[None] * (n + 1) for _ in range(n + 1)]

for i in range(n):
    r, c = map(int, input().split())

    if i == 0:
        p[0] = r

    p[i + 1] = c

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

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


matrix_chain_multiplication()
print(m[1][n])

Submission status: Accepted

Solution code:
n = int(input())
M = []
for _ in range(n):
    M.append([int(i) for i in input().split()])

def martixChainMultipication(M):
    n = len(M)
    m = [[0 for _ in range(n)] for _ in range(n)]
    for l in range(2, n+1):
        for i in range(1, n-l+2):
            j = i + l - 1
            m[i-1][j-1] = 1000000
            for k in range(i, j):
                cost = m[i-1][k-1]+m[k][j-1]+M[i-1][0]*M[k-1][1]*M[j-1][1]
                m[i-1][j-1] = min(m[i-1][j-1], cost)
    return m

print(martixChainMultipication(M)[0][n-1])

Submission status: Accepted

Solution code:
import sys


input = sys.stdin.readline
n = int(input())
m_list = []
for _ in range(n):
    m_list.append(list(map(int, input().split())))

p_list = [m_list[0][0]]
for i in range(n):
    p_list.append(m_list[i][1])

m = [[10**9]*(n+1) for i in range(n+1)]

for i in range(1, n+1):
    m[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):
            m[i][j] = min(m[i][j], m[i][k]+m[k+1][j]+p_list[i-1]*p_list[k]*p_list[j])

print(m[1][n])

Submission status: Accepted

Solution code:
n = int(input())
A = [list(map(int, input().split())) for i in range(n)]

ans = [[None for i in range(n)] for j in range(n)]


# for文で動的計画法
def dp1(i, j):
    if ans[i][j] is not None:
        return ans[i][j]
    else:
        if i == j:
            ans[i][j] = 0
        else:
            ret = float('INF')
            for k in range(i, j):
                tmp = dp1(i, k) + A[i][0]*A[k][1]*A[j][1] + dp1(k+1, j)
                ret = min(ret, tmp)
            ans[i][j] = ret
        return ans[i][j]


print(dp1(0, n-1))

Submission status: Accepted

Solution code:
n = int(input())
rc = [[0, 0]]+[list(map(int, input().split())) for i in range(n)]
m = [[0]*(n+1) for _ in range(n+1)]

for l in range(2, n+1):
    for i in range(1, n-l+2):
        j = i+l-1
        m[i][j] = 2**64
        for k in range(i, j):
            m[i][j] = min([
                m[i][j],
                m[i][k]+m[k+1][j]+rc[i][0]*rc[k+1][0]*rc[j][1]
            ])
print(m[1][n])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 22 14:15:50 2019

@author: Yamazaki Kenichi
"""

n = int(input())
A = [list(map(int,input().split())) for i in range(n)]
B = [A[0][0]]
for c in A:
    B += [c[1]]

T = [[0 for i in range(n)] for i in range(n)]
for i in range(1,n):
    for j in range(n-i):
        if i == 1:
            T[i][j] = B[j]*B[j+1]*B[j+2]
            continue
        temp = 10**6
        for k in range(1,i+1):
             temp = min(temp,
                        T[i-k][j] + T[k-1][j+(i-k)+1] + B[j]*B[j+i-k+1]*B[j+i+1])
        T[i][j] = temp
print(T[n-1][0])

Submission status: Accepted

Solution code:
# coding: utf-8

INF = 1 << 60
n = int(input().rstrip())
r = [0 for _ in range(n+10)]
c = [0 for _ in range(n+10)]
# i番目の行列からj番目の行列までの最小値
dp = [[INF for _ in range(n+10)] for _ in range(n+10)]
for i in range(n):
    line = input().rstrip().split()
    r[i] = int(line[0])
    c[i] = int(line[1])
    
for i in range(n):
    dp[i][i] = 0

# 行列の距離が近い方から計算しなくてはいけない！
for l in range(1,n):
    for i in range(n - l):
        j = i + l
        for k in range(i,j):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + r[i]*c[k]*c[j])
            
print(dp[0][n-1])

Submission status: Accepted

Solution code:
import math

n = int(input())
dp = [[0 for j in range(n+1)] for i in range(n+1)]
p = []

def init(p):
    global dp
    for i in range(1, n):
        dp[i][i+1] = p[i-1]*p[i]*p[i+1]

def solve(r, c):
    global dp
    smallest = math.inf
    for i in range(r, c):
        temp = p[r-1]*p[i]*p[c] + dp[r][i] + dp[i+1][c]
        if temp < smallest:
            smallest = temp
    return smallest

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

init(p)
for j in range(3, n+1):
    for i in range(j-2, 0, -1):
        dp[i][j] = solve(i, j)
print(str(dp[1][n]))

Submission status: Accepted

Solution code:
n = int(input())
M = []
dp = [[None for i in range(100)] for i in range(100)]

def calcMatrix(i,j):
    #print(i,j)
    if i == j:
        dp[i][j] = 0
    elif i == j-1:
        dp[i][j] = M[i]*M[i+1]*M[i+2]
    else:
        for k in range(j-i):
            #print(i,j,k)
            #print(dp[i][i+k])
            #print(dp[i+k+1][j])
            #print(M[i]*M[k+1]*M[j])
            current = dp[i][i+k] + dp[i+k+1][j] + M[i]*M[i+k+1]*M[j+1]
            if k==0:
                dp[i][j] = current
            if current < dp[i][j]:
                dp[i][j] = current 

for i in range(n-1):
    Q = list(map(int,input().split()))
    M.append(Q[0])
Q = list(map(int,input().split()))
M.append(Q[0])
M.append(Q[1])
#print(M)

for i in range(n):
    for j in range(n-i):
        calcMatrix(j,i+j)
        #for k in range(10):
        # print(dp[k])
        # print()
print(dp[0][n-1])

Submission status: Accepted

Solution code:
n = int(input())
data = list(list(map(int, input().split())) for _ in range(n))
m = [[None for _ in range(n+1)]for _ in range(n+1)]
p = []
INF = 1000000000

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

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

for l in range(2, n+1):
    for i in range(1, n - l + 2):
        j = i + l - 1
        m[i][j] = INF
        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(p)
#print(data)
print(m[1][n])

Submission status: Accepted

Solution code:
def matrix_chmul(matrices):
    length = len(matrices)
    dp = [[0 for n in range(length)] for m in range(length)]
    rowcol = []
    for i in range(length):
        rowcol.extend(matrices[i])
    for i in range(1, length):
        for j in range(length - i):
            if i == 1:
                dp[j][i + j] = rowcol[j * 2] * rowcol[(i + j) * 2] * rowcol[(i + j) * 2 + 1]
            else:
                temp_min = float("inf")
                for k in range(0, i):
                    # print(i, j, k, dp[j][j + k], dp[j + k + 1][i + j])
                    dp[j][i + j] = min(temp_min, (dp[j][j + k] + dp[j + k + 1][i + j] + rowcol[j * 2] * rowcol[(j + k + 1) * 2] * rowcol[(i + j) * 2 + 1] ))
                    temp_min = dp[j][i + j]
        # print(dp)
    return dp[0][length - 1]

length = int(input())
matrices = [[0 for n in range(2)] for m in range(length)]
for _ in range(length):
    x, y = [int(n) for n in input().split(" ")]
    matrices[_] = x, y
print(matrix_chmul(matrices))

Submission status: Accepted

Solution code:

def mcm(p,n):
	m=[[0 for x in range(n+1)] for x in range(n+1)]
	for s in range(n):
		for i,j in zip(range(1,n+1),range(s+2,n+1)):
			m[i][j] = 1 << 30
			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])
	return m[1][n]

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

ans=mcm(p,n)
print(ans)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_B&lang=jp
#  Matrix Chain Multiplication : python3
#  2018.12.03 yonezawa

from collections import deque
import sys
input = sys.stdin.readline

def main():
    nmax = 1000000
    n = int(input())
    p = []
    calc = [[nmax for i in range(n + 1)] for i in range(n + 1)]

    for i in range(n):
        (a,b) = map(int,input().split())
        p.append(a)
    else:
        p.append(b)

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

    for i in range(1,n):
        calc[i][i+1] = p[i-1] * p[i] * p[i +1] 
    
    for s in range(2,n):
        for i in range(1,n + 1 - s ):
#            print (i,i+s)
            for j in range(s):
                m = calc[i][i+j] + calc[i+j+1][i+s] + p[i-1]*p[i+j]* p[i+s]
#                print ("m=",m,"calc:",i,i+j,"+calc",i+j+1,i+s,"p:",i-1,i+j,i+s)
                if calc[i][i+s] > m:
                    calc[i][i+s] = m
    print (calc[1][n])
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def matrix_chain_multiplication(n, m, p):
    for l in range(2, n+1):
        for i in range(1, n-l+2):
            j = i + l - 1
            m[i][j] = float("inf")
            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])
    return m


n = int(input())
N = 100
p = [0 for i in range(N+1)]
m = [[0 for a in range(N+1)] for b in range(N+1)]

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

Submission status: Accepted

Solution code:
INF = 10**20
n = int(input())
nlist = []
all_mat = []
for i in range(n):
    a,b = map(int, input().split())
    all_mat.append((a,b))
    if (i == 0):
        nlist = [a,b]
    else:
        nlist.append(b)
        
def cost1(nlist):
    if len(nlist) == 0:
        return 0
    m = min(nlist)
    cost = 0
    mid = nlist.index(m)
    a = nlist[:mid]
    b = nlist[mid+1:]
    while (len(a)>1 and len(a) !=0):
        for i in range(len(a)-1):
            cost += a.pop()*a[-1]
            #print(a)
    while (len(b)>1 and len(b) !=0):
        b.reverse()
        for i in range(len(b)-1):
            cost += b.pop()*b[-1]
            #print(b)
    if (len(a) == 1 and len(b) == 1):
        cost += a.pop()*b.pop()
        #print(a,b)
    cost *= m
    return cost
#[34, 44, 13, 30]
def cost2(nlist, mm):
    if len(nlist) == 0:
        return 0
    m = min(nlist)
    cost = 0
    mid = nlist.index(m)
    a = nlist[:mid+1]
    #print(a)
    b = nlist[mid:]
    #print(b)
    while (len(a)>1 and len(a) !=0):
        a.reverse()
        for i in range(len(a)-1):
            cost += a.pop()*a[-1]
            #print(a)
    while (len(b)>1 and len(b) !=0):
        for i in range(len(b)-1):
            cost += b.pop()*b[-1]
            #print(b)
    if (len(a) == 1 and len(b) == 1):
        cost += m*mm
        #print(a,b)
    cost *= mm
    return cost

copy = nlist[:]
m = min(nlist)
#print(nlist)
c = nlist.count(m)
cost = 0
if (c == 1):
    cost = cost1(nlist)
elif (c >= 2):
    cc = 1
    cut_index = [0]
    mcount = nlist.count(m)
    for i in range(mcount):
        cut_index.append(nlist.index(m))
        nlist.remove(m)
    cut = []
    for i in range(mcount):
        cut.append(nlist[cut_index[i]:cut_index[i+1]])
    cut.append(nlist[cut_index[-1]:])
    #print(cut)
    co1 = cut[0] +[m]+ cut[-1]
    cost += cost1(co1)
    for co2 in cut[1:-1]:
        #print(co2)
        cost += cost2(co2,m)
    co3 = cut[0]+cut[-1]
    if (len(co3)>1):
        cost += (len(cut)-2)*m*min(co3)
print(cost)

Submission status: Accepted

Solution code:
def least_multiplication(mtx):
    N = len(mtx)
    mul = [[None] * N for x in range(N)]

    for i in range(N):
        mul[i][i] = 0
    #pprint.pprint(mul)

    for chain in range(1, N):
        for begin in range(N-chain):
            end = begin + chain
            cand = []
            for j in range(begin, end):
                left = mul[begin][j]
                down = mul[j+1][end]
                this = mtx[begin][0] * mtx[j][1] * mtx[end][1]
                cand.append(left + down + this)
            mul[begin][begin+chain] = min(cand)
        #pprint.pprint(mul)
    return mul[0][N-1]


def main():
    n = int(input())
    matrices = []
    for i in range(n):
        col, row = map(int, input().split())
        matrices .append((col, row))
    ans = least_multiplication(matrices)
    print(ans)


main()