From a69d572d70aaf10c496b6f22d7df50dea546da33 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=EC=9D=B4=EB=AC=B8=EB=B9=88?= <3412mb@gmail.com>
Date: Thu, 14 Mar 2024 02:39:59 +0900
Subject: [PATCH] =?UTF-8?q?2024-03-14=20=EB=AC=BC=EC=95=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Munbin-Lee/README.md | 3 +-
.../39-\353\254\274\354\225\275.py" | 45 +++++++++++++++++++
2 files changed, 47 insertions(+), 1 deletion(-)
create mode 100644 "Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py"
diff --git a/Munbin-Lee/README.md b/Munbin-Lee/README.md
index 1be0e769..5221cadf 100644
--- a/Munbin-Lee/README.md
+++ b/Munbin-Lee/README.md
@@ -38,5 +38,6 @@
| 35차시 | 2024.02.18 | 백트래킹 | 단어 마방진 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 |
| 36차시 | 2024.02.21 | 문자열 | 회문은 회문아니야!! | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 |
| 37차시 | 2024.03.05 | 백트래킹 | 석유 시추 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 |
-| 37차시 | 2024.03.08 | 트라이 | 전화번호 목록 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 |
+| 38차시 | 2024.03.08 | 트라이 | 전화번호 목록 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 |
+| 39차시 | 2024.03.14 | 문자열 | 물약 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/160 |
---
diff --git "a/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py" "b/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py"
new file mode 100644
index 00000000..efcfe576
--- /dev/null
+++ "b/Munbin-Lee/\353\254\270\354\236\220\354\227\264/39-\353\254\274\354\225\275.py"
@@ -0,0 +1,45 @@
+stdin = open(0)
+
+def input():
+ return stdin.readline().rstrip()
+
+N, M = map(int, input().split())
+
+prices = {}
+
+for _ in range(N):
+ item, price = input().split()
+ prices[item] = int(price)
+
+recipes = []
+
+for _ in range(M):
+ target, formula = input().split('=')
+ terms = formula.split('+')
+ recipes.append([target, terms])
+
+def updatePrice(target, terms):
+ price = 0
+
+ for term in terms:
+ count = int(term[0])
+ item = term[1:]
+
+ if item not in prices:
+ return
+
+ price += count * prices[item]
+
+ if target not in prices or prices[target] > price:
+ prices[target] = price
+
+for _ in range(M):
+ for recipe in recipes:
+ updatePrice(recipe[0], recipe[1])
+
+if 'LOVE' not in prices:
+ print('-1')
+ exit()
+
+answer = min(prices['LOVE'], 1000000001)
+print(answer)