Official

D - 休暇の計画 / Vacation Plan Editorial by admin

Gemini 3.8 Flash (High)

Overview

This problem asks us to find the maximum total satisfaction obtained by choosing exactly \(K\) non-consecutive days to go out from an \(N\)-day vacation.

Analysis

Naive Approach and Its Limitations

If we consider an exhaustive search (brute force) trying all ways to choose the \(K\) days to go out, the number of choices is extremely large (there are roughly \(\binom{N-K+1}{K}\) ways, which easily exceeds \(10^{1000}\) at its peak, rather than something small like \(\binom{2501}{2500}\)). Therefore, brute force will far exceed the time limit (TLE).

Key Optimization (Applying Dynamic Programming)

Whether we can go out on day \(i\) depends solely on “whether we went out on the previous day (day \(i-1\))”. Specifically which days were chosen prior to that does not affect our choices for day \(i\).

For problems like this, where we can discard the “detailed past history” and only need to track the “immediate previous state” and the “number of days chosen so far”, Dynamic Programming (DP) is very effective.

Algorithm

We define the states as follows:

DP Table Definition

  • \(dp[i][j][0]\): The maximum satisfaction considering up to day \(i\), choosing \(j\) days to go out, and not going out on day \(i\)
  • \(dp[i][j][1]\): The maximum satisfaction considering up to day \(i\), choosing \(j\) days to go out, and going out on day \(i\)

State Transitions

For day \(i\), we update as follows:

  1. When not going out on day \(i\) (\(dp[i][j][0]\))
    • It does not matter whether we went out on the previous day (day \(i-1\)) or not.
    • \(dp[i][j][0] = \max(dp[i-1][j][0], dp[i-1][j][1])\)
  2. When going out on day \(i\) (\(dp[i][j][1]\))
    • Since we cannot go out on consecutive days, we must not have gone out on the previous day (day \(i-1\)).
    • \(dp[i][j][1] = dp[i-1][j-1][0] + A_i\)

Initial Conditions and Answer

  • Initial conditions: \(dp[0][0][0] = 0\), and all others are \(-\infty\)
  • Final answer: \(\max(dp[N][K][0], dp[N][K][1])\)

Complexity

  • Time Complexity: \(O(NK)\)
    • There are \(N \times K\) states, and each transition takes \(O(1)\) time.
    • Given \(N \le 5000, K \le 2500\), the number of operations is at most approximately \(1.25 \times 10^7\), which easily fits within the time limit.
  • Space Complexity: \(O(K)\)
    • Computing day \(i\) only requires the results from day \(i-1\). By maintaining only the immediate previous state, the array size can be compressed from \(O(NK)\) to \(O(K)\).

Implementation Notes

  • Array Reuse (1D DP Optimization): By maintaining arrays f (for not going out) and g (for going out) of size \(K+1\), and iterating the \(j\) loop in descending order (\(K \to 1\)), we can update in-place using only 1D arrays while preventing overwriting values from the same step.

  • Skipping Unnecessary Calculations (Pruning): The maximum number of days that can be chosen up to day \(i\) is at most \(\lceil i / 2 \rceil = \lfloor (i + 1) / 2 \rfloor\). By bounding the upper limit of \(j\) to \(\min(K, \lfloor (i + 1) / 2 \rfloor)\), we can eliminate redundant iterations and speed up execution by roughly a factor of two.

    Source Code

import sys


def main():
    input_data = sys.stdin.read().split()
    if not input_data:
        return

    N = int(input_data[0])
    K = int(input_data[1])
    A = [int(x) for x in input_data[2:]]

    INF = 10**18
    f = [-INF] * (K + 1)
    g = [-INF] * (K + 1)
    f[0] = 0

    for i in range(1, N + 1):
        a = A[i - 1]
        max_j = (i + 1) // 2
        if max_j > K:
            max_j = K

        for j in range(max_j, 0, -1):
            g_old = g[j]
            g[j] = f[j - 1] + a
            if g_old > f[j]:
                f[j] = g_old

    ans = f[K] if f[K] > g[K] else g[K]
    print(ans)


if __name__ == "__main__":
    main()

This editorial was generated by gemini-3.8-flash-high.