F - ダンジョン攻略 / Dungeon Conquest 解説 by admin
Claude Opus 5 (High)Summary
This problem can be solved using dynamic programming (DP) where the state is the remaining health points (HP), optimizing whether to choose “recover” (drink from the fountain) or “treasure chest” at each rest area. With \(O(M)\) states and \(N\) transitions, it runs in \(O(NM)\) time.
Analysis
Exhaustive Search Over Choices is Impossible
There are \(N-1\) rest areas, each offering 2 choices, leading to \(2^{N-1}\) total combinations. With \(N \le 3000\), this will definitely not run in time.
Greedy Approach Fails
A greedy approach such as “prioritize opening treasure chests at rest areas with more coins” fails. For example, consider:
- \(M = 10\), \(A = (5, 5, 5)\), \(B_1 = 10, C_1 = 100\), \(B_2 = 10, C_2 = 1\)
If we collect \(C_1 = 100\), the health remains at \(10 - 5 = 5\) and drops to \(0\) upon clearing Room 2. Then at Rest Area 2, we are forced to recover, making it impossible to clear Room 3 (a failure). Conversely, if we recover at Rest Area 1, we can collect \(C_2 = 1\). In other words, there is a trade-off: “taking coins now may prevent you from recovering later,” meaning local decisions will not work.
What is the State?
The only factor affecting Takahashi’s future actions is the remaining health at that point (how many coins have been collected so far does not affect whether future actions are possible). Furthermore, since health is an integer between \(0\) and \(M\) inclusive, there are only \(M+1\) possible states.
Therefore, by defining
\[\mathrm{dp}[h] = (\text{maximum number of coins obtained such that the remaining health is exactly } h \text{ immediately after clearing Room } i)\]
we can update the DP while advancing \(i\) from \(1\) to \(N\) (using a sentinel value like \(-1\) for unreachable states \(h\)).
Handling “Health Capped at \(M\)”
The transition for recovery is \(h \to \min(h + B_i,\, M)\). If \(h \le M - B_i\), the resulting health is \(h + B_i\), which is a one-to-one mapping. However, if \(h > M - B_i\), all of them collapse to \(M\). Therefore, for this range, we can take the “maximum value of \(\mathrm{dp}[h]\)” and transition it into the state with health \(M\). Handling this carefully is the key point of this problem.
Algorithm
Initialization: Since the health after clearing Room 1 is \(M - A_1\): $\(\mathrm{dp}[M - A_1] = 0,\quad \text{and } -1 \text{ for all other states (unreachable)}\)\( (From the constraints, \)A_1 \le M$, so Room 1 can always be cleared.)
For each \(i = 1, 2, \dots, N-1\), we treat “action at Rest Area \(i \to\) clearing Room \(i+1\)” as a single step and construct a new array \(\mathrm{new}\). Let \(a = A_{i+1}\), \(B = B_i\), and \(C = C_i\):
(1) Open the treasure chest
Health does not change at the rest area, so for \(h\) satisfying \(h \ge a\): $\(\mathrm{new}[h - a] \leftarrow \max(\mathrm{new}[h-a],\ \mathrm{dp}[h] + C)\)$
(2) Drink from the fountain (without reaching the cap)
When \(h \le M - B\), health becomes \(h + B\) after recovery. Since \(h + B \ge a\) (which means \(h \ge a - B\)) is required to clear the room, the valid range of \(h\) is \(\max(0,\, a-B) \le h \le M - B\). In this case: $\(\mathrm{new}[h + B - a] \leftarrow \max(\mathrm{new}[h+B-a],\ \mathrm{dp}[h])\)$
(3) Drink from the fountain (clamping at the cap \(M\))
When \(M - B < h \le M\), health becomes \(M\) after recovery and \(M - a\) after clearing the room (clearing is always possible because \(A_i \le M\)). We take the maximum value \(best\) of \(\mathrm{dp}[h]\) in this range, and if \(best \ge 0\): $\(\mathrm{new}[M - a] \leftarrow \max(\mathrm{new}[M-a],\ best)\)$
After processing all \(i\), \(\max_h \mathrm{dp}[h]\) is the answer (since any remaining health at the end is valid, we take the maximum over all states).
Concrete Example
For \(M = 10\), \(A = (5,5,5)\), \(B_1 = 10, C_1 = 100\), \(B_2 = 10, C_2 = 1\):
- Initial state: \(\mathrm{dp}[5] = 0\)
- Rest Area 1 + Room 2:
- Treasure chest \(\to\) health \(5 \to 0\), coins \(100\): \(\mathrm{new}[0] = 100\)
- Fountain (\(h = 5 > M - B = 0\), so capped at maximum) \(\to\) health \(10 \to 5\): \(\mathrm{new}[5] = 0\)
- Rest Area 2 + Room 3:
- From \(\mathrm{dp}[0] = 100\): Choosing the treasure chest fails because health is \(0 < 5\). Drinking from the fountain restores health to \(10\), which becomes \(5\) after clearing Room 3, so \(\mathrm{new}[5] = 100\).
- From \(\mathrm{dp}[5] = 0\): Opening the treasure chest changes health \(5 \to 0\) with \(1\) coin \(\to \mathrm{new}[0] = 1\).
- The answer is \(\max = 100\).
(In this example, it turns out that \(C_1\) can be obtained. For an example where greedy fails, consider a case where multiple rooms follow after taking the coins, as described earlier. In any case, DP handles all scenarios correctly.)
Complexity
- Time Complexity: \(O(NM)\) (each step merely scans an array of length \(M+1\) a few times) \(\to\) at most about \(3000 \times 3000 = 9 \times 10^6\) operations.
- Space Complexity: \(O(M)\) (using a rolling array to reuse \(\mathrm{dp}\). Note that maintaining a 2D array of size \(O(NM)\) may cause memory limits to be exceeded).
Implementation Notes
Sentinel Value: Since the number of coins is always at least \(0\), using \(-1\) to represent unreachable states allows a simple check: “reachable if \(\ge 0\)” (equivalent to \(-\infty\)).
Do Not Allocate a 2D Array: Use only two arrays, \(\mathrm{dp}\) and \(\mathrm{new}\), swapping them at each step.
Calculation of Loop Ranges:
- Treasure chest: \(h \ge a\) (fails if there is not enough health).
- Fountain (unsaturated): \(h\) ranges from \(\max(0, a - B)\) to \(M - B\). Note that when \(B \ge a\), the lower bound is \(0\).
- Fountain (saturated): \(h\) ranges from \(M - B + 1\) to \(M\). You only need to find the single maximum value over this interval.
Fast I/O: Although the input size is relatively small since \(N \le 3000\), in Python it is safer to read all input at once using
sys.stdin.buffer.read().split().Since it is guaranteed that “there is always a valid way to clear the dungeon,” there is no need to worry about the final \(\max(\mathrm{dp})\) being \(-1\).
In Python, \(9 \times 10^6\) loop iterations can be somewhat slow, so it is advisable to minimize operations inside the loop (avoiding redundant function calls, caching in local variables). If further speedup is desired, you can vectorize transitions using list slicing and element-wise
max(e.g.,list(map(max, a, b))), taking advantage of the fact that transitions are essentially shifts with an offsetmax.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
N = int(data[0]); M = int(data[1])
A = [int(x) for x in data[2:2+N]]
rest = data[2+N:]
NEG = -1
dp = [NEG]*(M+1)
dp[M - A[0]] = 0
for i in range(N-1):
B = int(rest[2*i]); C = int(rest[2*i+1])
a = A[i+1]
new = [NEG]*(M+1)
# 宝箱を開ける: 体力そのまま + C コイン
for h in range(a, M+1):
v = dp[h]
if v >= 0:
new[h-a] = v + C
# 泉の水を飲む
lim = M - B
d = B - a
start = 0
if d < 0:
start = -d
end = lim
if end > M:
end = M
for h in range(start, end+1):
v = dp[h]
if v >= 0:
t = h + d
if v > new[t]:
new[t] = v
# 上限に張り付くケース
best = NEG
for h in range(lim+1, M+1):
if dp[h] > best:
best = dp[h]
t = M - a
if best > new[t]:
new[t] = best
dp = new
print(max(dp))
main()
This editorial was generated by claude5opus-high.