Official

F - ダンジョン攻略 / Dungeon Conquest Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

At each resting area, we choose either to “recover health” or “gain coins” to maximize the total number of coins collected while ensuring we can clear up to the final room. This problem can be solved using dynamic programming by maintaining the maximum number of coins collected for each possible health value immediately after clearing a room.

Analysis

Trying All Actions Will Exceed the Time Limit

There are \(N-1\) resting areas, and since there are \(2\) choices of actions at each, there are \(2^{N-1}\) possible combinations of actions. An approach that tries all of them will not finish in time for \(N \le 3000\).

Furthermore, if we prioritize opening treasure chests, we may run out of health in later rooms. Conversely, if we prioritize recovery too much, unnecessary healing will cause us to miss out on coins.

For the Same Room and Health, Keep Only the State with the Most Coins

Suppose that immediately after clearing room \(i\), we can reach the following \(2\) states:

  • Health of \(5\), with \(10\) coins
  • Health of \(5\), with \(20\) coins

From this point on, the available actions and clearable rooms are identical in both cases. Since there are no events that consume coins, there is no need to keep the state with \(10\) coins.

Therefore, if the health immediately after clearing the same room is the same, it is sufficient to keep only the state with the maximum number of coins.

Health is an integer from \(0\) to \(M\), where \(M \le 3000\). Thus, we can consider dynamic programming using health as an index.

Algorithm

1. DP Definition

Immediately after clearing room \(i\), define:

\(dp[h] = \text{maximum number of coins collected when health is } h\)

Unreachable health states are represented by \(-1\). Since the number of coins collected is always at least \(0\), they can be distinguished.

2. Initial State

The initial health is \(M\), and clearing room \(1\) consumes \(A_1\) health. Therefore, we set

\(dp[M-A_1] = 0\)

and set all other states to \(-1\).

3. Processing Resting Areas and the Next Room Together

From the DP state immediately after clearing room \(i\), we compute the DP state immediately after taking an action at resting area \(i\) and clearing room \(i+1\).

Let the array to be updated be \(ndp\), initialized entirely to \(-1\) each time. For each reachable health \(h\), we try the following \(2\) choices:

Opening the Treasure Chest

Health remains unchanged, and the number of coins increases by \(C_i\).

Clearing room \(i+1\) requires \(h \ge A_{i+1}\). If this condition is satisfied, we update:

\(ndp[h-A_{i+1}] \gets \max\left(ndp[h-A_{i+1}],\ dp[h]+C_i\right)\)

Drinking from the Spring

Let the health after recovery be

\(h' = \min(M,\ h+B_i)\)

Clearing room \(i+1\) requires \(h' \ge A_{i+1}\). If this condition is satisfied, we update:

\(ndp[h'-A_{i+1}] \gets \max\left(ndp[h'-A_{i+1}],\ dp[h]\right)\)

For example, when \(M=10\), current health is \(4\), recovery amount is \(8\), and the health consumed by the next room is \(6\), opening the chest makes it impossible to clear the next room. On the other hand, recovering brings the health to \(\min(10,4+8)=10\), allowing us to finish clearing the next room with a remaining health of \(4\).

After processing all states, we swap \(dp\) and \(ndp\).

4. Answer

After processing all resting areas, \(dp\) represents the states immediately after clearing room \(N\).

Since the remaining health at the end can be anything, the answer is:

\(\max_{0 \le h \le M} dp[h]\)

Why This Approach Is Correct

At each resting area, the only possible actions are “open the treasure chest” and “drink from the spring”, and the DP explores both. Furthermore, transitions that fail to clear the next room are excluded.

When reaching the same health value, we only keep the state with the maximum number of coins; since whether future actions are possible depends solely on health, we will never discard an optimal solution. Therefore, the maximum value in the final DP gives the required answer.

Complexity

  • Time complexity: \(O(NM)\)
    At each resting area, we examine \(M+1\) possible health values and perform \(O(1)\) operations for each.
  • Space complexity: \(O(N+M)\)
    The presented code uses \(O(N)\) space for the array \(A\) and \(O(M)\) space for the two DP arrays. The DP component alone uses \(O(M)\) space.

Implementation Notes

  • Use long long for the number of coins
    The total can be up to \((N-1)\times 10^9\) coins, which does not fit in a 32-bit integer.

  • Health immediately after clearing can be \(0\)
    Determine whether a room can be cleared using hp >= required_health.

  • Apply the recovery cap first
    Compute the health after recovery using min(M, hp + B) before subtracting the health consumed by the next room.

  • Use a separate array for updates
    Directly updating the same array could create states where multiple actions are performed at the same resting area. Initialize ndp to \(-1\) each time, update it, and swap them at the end.

  • Do not process unreachable states
    Skip transitions when dp[hp] < 0.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<int> A(N);
    for (int &a : A) cin >> a;

    vector<long long> dp(M + 1, -1), ndp(M + 1, -1);
    dp[M - A[0]] = 0;

    for (int i = 0; i < N - 1; ++i) {
        int B;
        long long C;
        cin >> B >> C;

        fill(ndp.begin(), ndp.end(), -1);
        for (int hp = 0; hp <= M; ++hp) {
            if (dp[hp] < 0) continue;

            if (hp >= A[i + 1]) {
                int nextHp = hp - A[i + 1];
                ndp[nextHp] = max(ndp[nextHp], dp[hp] + C);
            }

            int healedHp = min(M, hp + B);
            if (healedHp >= A[i + 1]) {
                int nextHp = healedHp - A[i + 1];
                ndp[nextHp] = max(ndp[nextHp], dp[hp]);
            }
        }
        dp.swap(ndp);
    }

    cout << *max_element(dp.begin(), dp.end()) << '\n';
    return 0;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: