F - ダンジョン攻略 / Dungeon Conquest Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
We solve this problem using Dynamic Programming (DP), where the state represents the remaining health immediately after clearing each room, and the value records the maximum number of coins collected up to that point. Noticing that the maximum health is capped at \(M\), we can design appropriate state transitions to find the optimal solution in \(O(NM)\) time complexity.
Analysis
1. Naive Approach and Its Limitations
At each rest area, there are \(2\) choices: “drink water” or “open a treasure chest”. Since there are \(N - 1\) rest areas, trying all possible action sequences results in \(2^{N-1}\) possibilities, which will not run within the time limit under the constraint \(N \le 3000\) (it will result in TLE).
2. Identifying Equivalent States (Focusing on DP)
When reaching rest area \(i\), regardless of the choices made up to that point, only the following \(2\) factors affect subsequent actions: 1. Current health 2. Total number of coins collected so far
For the same remaining health, having more coins collected is strictly better. Furthermore, health can only take integer values between \(0\) and \(M\), inclusive. Since \(M \le 3000\) is small, we can apply dynamic programming (DP) by maintaining a table of “the maximum number of coins collected when the health immediately after clearing room \(i\) is \(h\)”.
3. Formulating State Transitions
Let us consider the transition from the state immediately after clearing room \(i\), performing an action at rest area \(i\), to the state immediately after clearing room \(i+1\).
Let \(h\) be the health immediately after clearing room \(i+1\). Then, the health right before entering room \(i+1\) (after taking an action at rest area \(i\)) must be \(H = h + A_{i+1}\). Since the maximum health is \(M\), we must have \(H \le M\) (that is, \(h \le M - A_{i+1}\)).
Depending on the action taken at rest area \(i\), the corresponding health \(k\) immediately after clearing room \(i\) relates to \(H\) as follows:
If opening a treasure chest (collecting coins):
- Health does not change, so the health \(k\) at the end of room \(i\) must be equal to \(H\).
- The number of coins obtained increases by \(C_i\).
If drinking spring water (recovery):
- Health after recovery must become \(H\).
- When \(H < M\): Since the maximum health limit is not reached, the health before recovery must be exactly \(k = H - B_i\) (possible only if \(H \ge B_i\)).
- When \(H = M\): This is the case where health hits the upper limit. Any health \(k\) before recovery that satisfies \(k + B_i \ge M\) can transition to this state, so transitions can come from any \(k \ge M - B_i\). Among these values of \(k\), we choose the one that maximizes the number of coins at the end of room \(i\).
Algorithm
DP Table Definition:
- \(dp[h]\): The maximum number of coins collected when the health immediately after clearing the previous room is \(h\) (or \(-1\) if unreachable).
Initialization:
- The health immediately after clearing room \(1\) is \(M - A_1\).
- Set \(dp[M - A_1] = 0\) and initialize all other \(dp[h]\) to \(-1\).
Transitions (\(i = 1, 2, \dots, N-1\)):
- Initialize a new table \(next\_dp\) with \(-1\).
- Precompute the optimal value for recovering to \(H = M\): \(max\_rec = \max_{k \ge \max(0, M - B_i)} dp[k]\).
- For each \(h \in [0, M - A_{i+1}]\), compute the health upon entering room \(i+1\), \(H = h + A_{i+1}\), and record the maximum of the following \(2\) options into \(next\_dp[h]\):
- Collecting coins: \(dp[H] + C_i\) (if \(dp[H] \ge 0\))
- Recovery:
- If $H < M$: $dp[H - B_i]$ (if $H \ge B_i$ and $dp[H - B_i] \ge 0$) - If $H = M$: $max\_rec$
- Update \(dp\) with \(next\_dp\).
Computing the Answer:
- The answer is the maximum value in the \(dp\) array after clearing room \(N\): \(\max_{0 \le h \le M} dp[h]\).
Complexity
Time Complexity: \(O(NM)\)
- There are \(N - 1\) stages, and at each step, we update \(M\) states.
- Precomputing \(max\_rec\) takes \(O(M)\), and updating each \(h\) takes \(O(1)\), so each step takes \(O(M)\).
- In total, the loop runs \(O(NM) \approx 3000 \times 3000 = 9 \times 10^6\) times, easily fitting within the time limit.
Space Complexity: \(O(M)\)
- Since it is sufficient to maintain only two arrays—the current step’s \(dp\) array and the next step’s \(next\_dp\) array—the space complexity is \(O(M)\).
Implementation Notes
Precomputing recovery capped at \(M\): When finding the transition for recovery leading to \(H = M\), iterating over \(k\) for each \(h\) would take \(O(M^2)\). However, by finding the maximum over the range \(k \ge M - B_i\) once beforehand outside the loop, we can keep this within \(O(M)\).
Handling unreachable states: We represent unreachable health states with \(-1\) and guard against transitioning from impossible states. Since the number of coins obtained \(C_i\) is positive, we can clearly distinguish between the initial value \(0\) and unreachable states \(-1\).
Source Code
import sys
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
M = int(input_data[1])
A = [int(x) for x in input_data[2:2 + N]]
idx = 2 + N
B = [0] * (N - 1)
C = [0] * (N - 1)
for i in range(N - 1):
B[i] = int(input_data[idx])
C[i] = int(input_data[idx + 1])
idx += 2
dp = [-1] * (M + 1)
dp[M - A[0]] = 0
next_dp = [-1] * (M + 1)
for i in range(N - 1):
b = B[i]
c = C[i]
a = A[i + 1]
start_k = M - b
if start_k < 0:
start_k = 0
max_rec = -1
for k in range(start_k, M + 1):
if dp[k] > max_rec:
max_rec = dp[k]
limit = M - a
for h in range(limit + 1):
H = h + a
# コインを獲得する場合
val1 = dp[H]
if val1 != -1:
val1 += c
# 回復する場合
if H == M:
val2 = max_rec
else:
if H >= b:
val2 = dp[H - b]
else:
val2 = -1
if val1 > val2:
next_dp[h] = val1
else:
next_dp[h] = val2
for h in range(limit + 1, M + 1):
next_dp[h] = -1
dp, next_dp = next_dp, dp
ans = max(dp)
print(ans)
if __name__ == '__main__':
main()
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: