From 9c7400f2c11efd25b548afebd7fd0bfb605e5da1 Mon Sep 17 00:00:00 2001 From: SSUM <116950962+ssum21@users.noreply.github.com> Date: Wed, 21 May 2025 17:32:43 +0900 Subject: [PATCH] [Gold III] Title: Spell Cards, Time: 64 ms, Memory: 32412 KB -BaekjoonHub --- 백준/Gold/28467. Spell Cards/README.md | 34 ++++++++++++++++++ 백준/Gold/28467. Spell Cards/Spell Cards.py | 38 +++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 백준/Gold/28467. Spell Cards/README.md create mode 100644 백준/Gold/28467. Spell Cards/Spell Cards.py diff --git a/백준/Gold/28467. Spell Cards/README.md b/백준/Gold/28467. Spell Cards/README.md new file mode 100644 index 0000000..714a523 --- /dev/null +++ b/백준/Gold/28467. Spell Cards/README.md @@ -0,0 +1,34 @@ +# [Gold III] Spell Cards - 28467 + +[문제 링크](https://www.acmicpc.net/problem/28467) + +### 성능 요약 + +메모리: 32412 KB, 시간: 64 ms + +### 분류 + +다이나믹 프로그래밍, 그리디 알고리즘 + +### 제출 일자 + +2025년 5월 21일 17:32:24 + +### 문제 설명 + +

마리사는 바닥에 일렬로 늘어놓은 N장의 카드로 마법을 연습하고 있다. 왼쪽에서부터 i번째에 있는 i번 카드의 마력 소모량은 ai로 표현된다. 마리사가 i번 카드에 마법을 사용하기 위해서는 ai만큼의 마력을 소모해야 한다.

+ +

마리사는 인접한 두 카드를 골라서 마법을 시전한다. 이때 마리사는 고른 두 카드의 마력 소모량의 합만큼 마력을 소모한다. 이후 두 카드는 그 자리에서 하나로 합쳐지며, 새로 만들어진 카드의 마력 소모량은 이전 두 카드의 마력 소모량 중 최댓값이 된다. 이 과정은 카드가 단 하나만 남을 때까지 반복한다.

+ +

마리사는 소모하는 마력을 최소화하려 한다. 이때 마리사가 소모할 마력의 양을 구해주자!

+ +### 입력 + +

첫 번째 줄에 카드의 개수 N이 주어진다. (1N400)

+ +

두 번째 줄에 카드의 마력 소모량을 나타내는 정수 a1,a2,,aN가 공백으로 구분되어 주어진다. (1ai109)

+ +### 출력 + +

마리사가 소모하는 마력의 최솟값을 출력한다.

+ diff --git a/백준/Gold/28467. Spell Cards/Spell Cards.py b/백준/Gold/28467. Spell Cards/Spell Cards.py new file mode 100644 index 0000000..1ade775 --- /dev/null +++ b/백준/Gold/28467. Spell Cards/Spell Cards.py @@ -0,0 +1,38 @@ +import sys + +input = sys.stdin.readline + +n = int(input()) +arr = list(map(int, input().split())) + +# N=1인 경우는 합칠 필요가 없으므로 비용 0 +if n <= 1: + print(0) +else: + total_mana = 0 + current_cards = list(arr) # 원본 리스트를 복사하여 사용 + + # 카드가 하나 남을 때까지 반복 + while len(current_cards) > 1: + min_sum = sys.maxsize + merge_index = -1 + + # 인접한 두 카드의 합 중 최소값 찾기 + for i in range(len(current_cards) - 1): + current_sum = current_cards[i] + current_cards[i+1] + if current_sum < min_sum: + min_sum = current_sum + merge_index = i # 합칠 왼쪽 카드 인덱스 + + # 최소 합을 갖는 두 카드 합치기 + total_mana += min_sum # 합치는 비용 누적 + + # 새로운 카드 생성 (두 카드 중 최대값) + max_value = max(current_cards[merge_index], current_cards[merge_index+1]) + + # 리스트 업데이트: 왼쪽 카드를 새로운 카드로 바꾸고 오른쪽 카드 삭제 + current_cards[merge_index] = max_value + del current_cards[merge_index + 1] + + # 최종 누적된 마력 소모량 출력 + print(total_mana) \ No newline at end of file