From 6d89520f24792bb592f7a8a61612b489525840ef Mon Sep 17 00:00:00 2001 From: yenru0 Date: Fri, 26 Jul 2024 17:02:23 +0900 Subject: [PATCH] complete 16508.py --- zeta_python/completed/16508.py | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 zeta_python/completed/16508.py diff --git a/zeta_python/completed/16508.py b/zeta_python/completed/16508.py new file mode 100644 index 0000000..2d912df --- /dev/null +++ b/zeta_python/completed/16508.py @@ -0,0 +1,43 @@ +import sys + +input = sys.stdin.readline + +INF_MAX = 1000000 * 50 + + +def transpose(word: str): + dictionary = [0] * 26 + for c in word: + dictionary[ord(c) - 65] += 1 + return dictionary + + +def check(T: list, word: list): + return all([word[i] >= T[i] for i in range(26)]) + + +def merge(a: list, b: list): + return [a[i] + b[i] for i in range(26)] + + +if __name__ == "__main__": + T = transpose(input().rstrip()) + N = int(input()) + P = [ + (int(c), transpose(w)) for c, w in (input().rstrip().split() for _ in range(N)) + ] + D = [] + min_price = INF_MAX + + D.append((0, 0, [0] * 26)) + while D: + index, cost, word = D.pop() + word: list + if index >= N: + if check(T, word): + min_price = min(min_price, cost) + continue + else: + D.append((index + 1, cost + P[index][0], merge(word, P[index][1]))) + D.append((index + 1, cost, word.copy())) + print(min_price) if min_price < INF_MAX else print(-1)