๋ ์ ์์ฉ๋ฌธ์ .
์์ด๋์ด
๋ ์ ์๊ณ ๋ฆฌ์ฆ์ ๊ธฐ๋ณธ ์๋ฆฌ๋ฅผ ์ด์ฉํด ํด๊ฒฐํ ์ ์๋ค.
์ด์ฐจ์ ๋ฆฌ์คํธ backpack[x][y]๋ x๋ฒ์งธ ์ฑ๊น์ง y๋น์ฉ์ผ๋ก ์ป์ ์ ์๋ ์ต๋ ๋ฉ๋ชจ๋ฆฌ๊ฐ์ด๋ค.
1. ์ด์ ํฌ๊ธฐ๋ ์ ์๋ ๋ชจ๋ ๋น์ฉ์ ์ดํฉ์ผ๋ก ํ๋ค.
2 - 1. ํ์ฌ ์ฑ์ ๋น์ฉ costList [i]๊ฐ j๋ณด๋ค ํฌ๋ฉด ๊ทธ๋๋ก ๋๋ค.
backpack[i][j] = backpack[i - 1][j]
2 - 2. j ๊ฐ ๋ ํฌ๋ฉด ํ์ฌ ์ฑ์ ์ ๋ฌด๋ฅผ ๋น๊ตํด ๊ฐ์ ์ ๋ฐ์ดํธํด์ค๋ค.
backpack[i][j] = max(backpack[i - 1][j], backpack[i - 1][j - costList[i]] + memoryList[i])
3. ํ์ฌ backpack[x][y]์ ๊ฐ์ด M ์ด์์ด๋ผ๋ฉด ์ ๋ต์ ์ต์๊ฐ ์ ๋ฐ์ดํธ ํด์ค๋ค.
answer = min(answer, j)
์ ์ฒด ์ฝ๋
N, M = map(int, input().split())
memoryList = [0] + [int(x) for x in input().split()]
costList = [0] + [int(x) for x in input().split()]
backpack = [[0] * (sum(costList) + 1) for _ in range(N + 1)]
answer = sum(costList)
for i in range(1, N + 1):
for j in range(1, sum(costList) + 1):
if j < costList[i]:
backpack[i][j] = backpack[i - 1][j]
else:
backpack[i][j] = max(backpack[i - 1][j], backpack[i - 1][j - costList[i]] + memoryList[i])
if backpack[i][j] >= M:
answer = min(answer, j)
print(answer)
'๐งฉ Problem Solving > [๋ฐฑ์ค]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2563 ์์ข ์ด (python ํ์ด์ฌ) (0) | 2024.04.01 |
---|---|
[๋ฐฑ์ค] 4179 ๋ถ! (python ํ์ด์ฌ) (0) | 2024.03.28 |
[๋ฐฑ์ค] 2212 ์ผ์ (python ํ์ด์ฌ) (0) | 2024.03.27 |
[๋ฐฑ์ค] 16918 ๋ด๋ฒ๋งจ (python ํ์ด์ฌ) (0) | 2024.03.23 |
[๋ฐฑ์ค] 1697 ์จ๋ฐ๊ผญ์ง (python ํ์ด์ฌ) (0) | 2024.03.22 |