๋ฌธ์
Day Of Mourning์ ๊ธฐํ๋ฆฌ์คํธ ๊ฐํ ๋ ๋ค๊ฐ์ค๋ ๊ณต์ฐ์์ ์ฐ์ฃผํ N๊ฐ์ ๊ณก์ ์ฐ์ฃผํ๊ณ ์๋ค. ์ง๊ธ๊น์ง ๊ณต์ฐ๊ณผ๋ ๋ค๋ฅธ ๊ณต์ฐ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด์ ์ด๋ฒ ๊ณต์ฐ์์๋ ๋งค๋ฒ ๊ณก์ด ์์ํ๊ธฐ ์ ์ ๋ณผ๋ฅจ์ ๋ฐ๊พธ๊ณ ์ฐ์ฃผํ๋ ค๊ณ ํ๋ค.
๋จผ์ , ๊ณต์ฐ์ด ์์ํ๊ธฐ ์ ์ ๊ฐ๊ฐ์ ๊ณก์ด ์์ํ๊ธฐ ์ ์ ๋ฐ๊ฟ ์ ์๋ ๋ณผ๋ฅจ์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์๋ค. ์ด ๋ฆฌ์คํธ๋ฅผ V๋ผ๊ณ ํ์ ๋, V[i]๋ i๋ฒ์งธ ๊ณก์ ์ฐ์ฃผํ๊ธฐ ์ ์ ๋ฐ๊ฟ ์ ์๋ ๋ณผ๋ฅจ์ ์๋ฏธํ๋ค. ํญ์ ๋ฆฌ์คํธ์ ์ ํ ์ฐจ์ด๋ก๋ง ๋ณผ๋ฅจ์ ๋ฐ๊ฟ ์ ์๋ค. ์ฆ, ํ์ฌ ๋ณผ๋ฅจ์ด P์ด๊ณ ์ง๊ธ i๋ฒ์งธ ๊ณก์ ์ฐ์ฃผํ๊ธฐ ์ ์ด๋ผ๋ฉด, i๋ฒ ๊ณก์ P+V[i]๋ P-V[i] ๋ก ์ฐ์ฃผํด์ผ ํ๋ค. ํ์ง๋ง, 0๋ณด๋ค ์์ ๊ฐ์ผ๋ก ๋ณผ๋ฅจ์ ๋ฐ๊พธ๊ฑฐ๋, M๋ณด๋ค ํฐ ๊ฐ์ผ๋ก ๋ณผ๋ฅจ์ ๋ฐ๊ฟ ์ ์๋ค.
๊ณก์ ๊ฐ์ N๊ณผ ์์ ๋ณผ๋ฅจ S, ๊ทธ๋ฆฌ๊ณ M์ด ์ฃผ์ด์ก์ ๋, ๋ง์ง๋ง ๊ณก์ ์ฐ์ฃผํ ์ ์๋ ๋ณผ๋ฅจ ์ค ์ต๋๊ฐ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ๋ชจ๋ ๊ณก์ ๋ฆฌ์คํธ์ ์ ํ ์์๋๋ก ์ฐ์ฃผํด์ผ ํ๋ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ N, S, M์ด ์ฃผ์ด์ง๋ค. (1 ≤ N ≤ 50, 1 ≤ M ≤ 1,000, 0 ≤ S ≤ M) ๋์งธ ์ค์๋ ๊ฐ ๊ณก์ด ์์ํ๊ธฐ ์ ์ ์ค ์ ์๋ ๋ณผ๋ฅจ์ ์ฐจ์ด๊ฐ ์ฃผ์ด์ง๋ค. ์ด ๊ฐ์ 1๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , M๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ๊ฐ๋ฅํ ๋ง์ง๋ง ๊ณก์ ๋ณผ๋ฅจ ์ค ์ต๋๊ฐ์ ์ถ๋ ฅํ๋ค. ๋ง์ฝ ๋ง์ง๋ง ๊ณก์ ์ฐ์ฃผํ ์ ์๋ค๋ฉด (์ค๊ฐ์ ๋ณผ๋ฅจ ์กฐ์ ์ ํ ์ ์๋ค๋ฉด) -1์ ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ 1 ๋ณต์ฌ
3 5 10
5 3 7
์์ ์ถ๋ ฅ 1 ๋ณต์ฌ
10
์์ ์ ๋ ฅ 2 ๋ณต์ฌ
4 8 20
15 2 9 10
์์ ์ถ๋ ฅ 2 ๋ณต์ฌ
-1
์์ ์ ๋ ฅ 3 ๋ณต์ฌ
14 40 243
74 39 127 95 63 140 99 96 154 18 137 162 14 88
์์ ์ถ๋ ฅ 3 ๋ณต์ฌ
238
# ํ์ด ๋ฐฉ๋ฒ
i๋ฒ์งธ ๊ณก๊น์ง ๋์ฌ ์ ์๋ ๋ณผ๋ฅจ์ ์๋ฅผ ๋ค ์ ์ฅํ๋ ๋ฐฐ์ด์ ๋ง๋ค์ด ๋์ ํด๋๊ฐ๋ ํ์์ด๋ค.
๊ตฌํ์ ํฌ๊ฒ ์ด๋ ต์ง ์์๋ค.
# ์ฝ๋
# 2025-05-11 10:28-11:08
import sys
sys.stdin = open("input.txt","r")
V = []
n,s,m = map(int,sys.stdin.readline().split()) # ๊ณก์ ๊ฐ์ N, ์์ ๋ณผ๋ฅจ S, ๊ทธ๋ฆฌ๊ณ M
V = [0] + list(map(int,sys.stdin.readline().split()
vol_list = [set() for _ in range(n+1)]
vol_list[0].add(s)
for i in range(1, n+1): # ๋ชจ๋ ๋
ธ๋
for v in vol_list[i-1]:
for next_v in (v-V[i],v+V[i]):
if 0 <= next_v <= m:
vol_list[i].add(next_v)
if len(vol_list[i]) == 0:
break
if len(vol_list[-1]) == 0: print(-1)
else: print(max(vol_list[-1]))
# ์ฐธ๊ณ
์ฒซ ์ฝ๋๋ ๋ถํ์ํ ์ค๋ณต์ด ๋ง์๋ค. list๋ณด๋ค set์ผ๋ก ์ฒ๋ฆฌํ๋๊ฒ ํจ์จ์
# 2025-05-11 10:28-11:08
import sys
sys.stdin = open("input.txt","r")
V = []
n,s,m = map(int,sys.stdin.readline().split()) # ๊ณก์ ๊ฐ์ N, ์์ ๋ณผ๋ฅจ S, ๊ทธ๋ฆฌ๊ณ M
V = [0] + list(map(int,sys.stdin.readline().split()))
vol_list = [[] for _ in range(n+1)]
vol_list[0].append(s)
for i in range(1, n+1): # ๋ชจ๋ ๋
ธ๋
for j in range(len(vol_list[i-1])):
next_v1, next_v2 = vol_list[i-1][j] - V[i], vol_list[i-1][j] + V[i]
if 0 <= next_v1 <= m and next_v1 not in vol_list[i]: vol_list[i].append(next_v1)
if 0 <= next_v2 <= m and next_v2 not in vol_list[i]: vol_list[i].append(next_v2)
if len(vol_list[i]) == 0:
break
if len(vol_list[-1]) == 0: print(-1)
else: print(max(vol_list[-1]))
'๐ Study > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Gold V] 5430 - AC (0) | 2025.05.15 |
---|---|
[Gold V] 16719 - ZOAC (0) | 2025.05.12 |
[Gold IV] 32187 - ๊ธ์ ๋ฐฐ์ (0) | 2025.05.10 |
[Gold V] 1931 - ํ์์ค ๋ฐฐ์ (0) | 2025.05.10 |
[Gold V] 20207 - ๋ฌ๋ ฅ (0) | 2025.05.09 |