Official

D - トレーニング計画 / Training Plan Editorial by admin

Gemini 3.8 Flash (High)

Overview

This problem asks us to find the maximum possible total effect obtained by choosing either “regular training (effect \(A_i\))” or “hard training (effect \(B_i\))” on each of the \(N\) days, subject to the constraint that “hard training cannot be performed for \(2\) consecutive days”.

Analysis

Naive Approach and its Limitations

For each day, there are \(2\) options: “regular” or “hard”. Thus, there are \(2^N\) possible ways to make the choices in total. However, the constraint in this problem is \(N \leq 2 \times 10^5\). \(2^{200000}\) is an astronomical number, so a brute-force search that examines all possible choices will result in a Time Limit Exceeded (TLE).

Key Insight to Solve the Problem

Whether hard training can be performed on day \(i\) depends solely on whether hard training was performed on the previous day (day \(i-1\)). Which training was chosen on earlier days from day \(1\) to day \(i-2\) does not affect whether the choices on day \(i\) are valid.

Therefore, it is not necessary to remember the entire history of past choices for each day. It suffices to record only the maximum total effect for each immediate state (whether the previous day was regular or hard) and carry it over to the next day. Since we can construct the solution using optimal solutions of subproblems in this manner, we can apply Dynamic Programming (DP).

Algorithm

We perform DP by dividing the state based on the action taken on the previous day.

DP State Definition

At the end of day \(i\), we maintain the following \(2\) values: - \(dp_0\): The maximum total effect when performing “regular training” on day \(i\) - \(dp_1\): The maximum total effect when performing “hard training” on day \(i\)

Transitions

Using the effects \(A_i, B_i\) of day \(i\), we compute the new state for day \(i\) from the state of day \(i-1\).

  1. When performing regular training on day \(i\): It does not matter whether regular or hard training was chosen on the previous day. Therefore, we add \(A_i\) to the larger value from the previous day. $\((\text{new } dp_0) = \max(dp_0, dp_1) + A_i\)$

  2. When performing hard training on day \(i\): Since hard training cannot be performed consecutively, the previous day must have been “regular training”. Therefore, we add \(B_i\) to the previous day’s \(dp_0\). $\((\text{new } dp_1) = dp_0 + B_i\)$

Concrete Example (when \(N = 3\))

  • Day \(1\): \(A_1 = 3, B_1 = 5\)
  • Day \(2\): \(A_2 = 4, B_2 = 6\)
  • Day \(3\): \(A_3 = 2, B_3 = 7\)

As an initial state, we set \(dp_0 = 0, dp_1 = -\infty\) for day \(0\) (to prevent violating the consecutive constraint right away on day \(1\)). - End of Day \(1\): - \(dp_0 = \max(0, -\infty) + 3 = 3\) (Regular) - \(dp_1 = 0 + 5 = 5\) (Hard) - End of Day \(2\): - \(dp_0 = \max(3, 5) + 4 = 9\) (Hard \(\to\) Regular) - \(dp_1 = 3 + 6 = 9\) (Regular \(\to\) Hard) - End of Day \(3\): - \(dp_0 = \max(9, 9) + 2 = 11\) - \(dp_1 = 9 + 7 = 16\) (Day \(2\) Regular \(\to\) Day \(3\) Hard)

The final answer is found as \(\max(dp_0, dp_1) = \max(11, 16) = 16\).

Complexity

  • Time Complexity: \(O(N)\) Since the computation for each day (state transitions) requires only \(O(1)\) arithmetic operations and comparisons, the entire process across all \(N\) days takes \(O(N)\) time. Even for \(N = 2 \times 10^5\), it runs sufficiently fast (within a few tens of milliseconds).
  • Space Complexity: \(O(1)\) (excluding the space needed to store the input) Since the computation for day \(i\) only uses the values from day \(i-1\), there is no need to maintain an array of length \(N\). The state can be continuously updated using only \(2\) variables.

Implementation Notes

  • Setting Initial Values: To allow choosing hard training on day \(1\), the initial value of \(dp_1\) should be set to a sufficiently small value (-float("inf")). This ensures that the transition for choosing hard training on day \(1\) correctly computes to \(0 + B_1 = B_1\).

  • Fast I/O: In Python, when \(N\) is large (\(2 \times 10^5\)), repeatedly calling input() can consume significant time for input processing. Reading all input at once using sys.stdin.read().split() can significantly reduce the execution time.

  • Handling Integer Overflow: The total effect can reach at most \(N \times \max(B_i) = 2 \times 10^5 \times 10^9 = 2 \times 10^{14}\). While Python handles arbitrarily large integers automatically, when implementing in languages such as C++, this value exceeds the range of a 32-bit signed integer (int), so a 64-bit signed integer (long long) must be used.

    Source Code

import sys


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

    n = int(input_data[0])

    dp0 = 0
    dp1 = -float("inf")

    idx = 1
    for _ in range(n):
        a = int(input_data[idx])
        b = int(input_data[idx + 1])
        idx += 2

        new_dp0 = (dp0 if dp0 > dp1 else dp1) + a
        new_dp1 = dp0 + b

        dp0 = new_dp0
        dp1 = new_dp1

    print(dp0 if dp0 > dp1 else dp1)


if __name__ == "__main__":
    main()

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