-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11049.py
More file actions
38 lines (28 loc) · 708 Bytes
/
11049.py
File metadata and controls
38 lines (28 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 행렬 곱셈 순서
N = int(input())
R = [0]*N
C = [0]*N
for n in range(N):
r, c = map(int, input().split())
R[n] = r
C[n] = c
# def solution(X, Y):
# if Y-X <= 0:
# return 0
# minimum = 1e9
# for k in range(X, Y):
# minimum = min(minimum, solution(X, k)+solution(k+1, Y)+R[X]*C[k]*C[Y])
# return minimum
# dp 추가
dp = [[0]*N for _ in range(N)]
def solution(X, Y):
if dp[X][Y] > 0:
return dp[X][Y]
if Y-X <= 0:
return 0
dp[X][Y] = float('inf')
for k in range(X, Y):
dp[X][Y] = min(dp[X][Y], solution(X, k) +
solution(k+1, Y)+R[X]*C[k]*C[Y])
return dp[X][Y]
print(solution(0, N-1))