Official

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

gpt-6-astra-medium

Overview

This problem asks us to maximize the total effectiveness over \(N\) days such that we do not choose hard training on consecutive days. We can solve this using dynamic programming (DP), taking “whether the training menu for the day is normal or hard” as the state.

Analysis

Since there are \(2\) choices for each day, enumerating all possible schedules would result in \(2^N\) possibilities. Since \(N\) can be up to \(2 \times 10^5\), this approach will exceed the time limit.

Also, a greedy approach that chooses the menu with the greater effectiveness on each day is not correct. For example, consider the following case:

Day Normal effectiveness Hard effectiveness
\(1\) \(1\) \(2\)
\(2\) \(1\) \(100\)

If we choose hard on Day \(1\), we can only choose normal on Day \(2\), resulting in a total of \(2+1=3\). However, if we choose normal on Day \(1\) and hard on Day \(2\), the total becomes \(1+100=101\).

The key observation here is that whether we can choose hard today depends only on yesterday’s menu. We do not need to keep track of the menus chosen prior to yesterday.

Therefore, we can maintain the optimal total up to the previous day by dividing it into \(2\) cases based on whether yesterday’s menu was “normal” or “hard”.

Algorithm

We define the maximum total effectiveness up to Day \(i\) as follows:

  • \(\mathrm{normal}_i\): the maximum value when choosing normal on Day \(i\)
  • \(\mathrm{hard}_i\): the maximum value when choosing hard on Day \(i\)

1. Choosing Normal

If we choose normal today, yesterday’s menu can be either normal or hard. Therefore, we add \(A_i\) to the larger value from yesterday:

\(\mathrm{normal}_i=\max(\mathrm{normal}_{i-1},\mathrm{hard}_{i-1})+A_i\)

2. Choosing Hard

If we choose hard today, yesterday must have been normal. Therefore, the transition is:

\(\mathrm{hard}_i=\mathrm{normal}_{i-1}+B_i\)

Both transitions consider all valid states of the previous day for today’s chosen menu. Since we take the maximum among them, computing these iteratively day by day yields the optimal value for each state.

3. Base Cases and the Final Answer

On Day \(1\), we do not need to consider any previous day, so:

  • \(\mathrm{normal}_1=A_1\)
  • \(\mathrm{hard}_1=B_1\)

In the code, by setting normal = 0 and hard = 0 before the loop, Day \(1\) can be processed with the exact same transition equations. This initial value is used for convenience; the first update will correctly yield \(A_1\) and \(B_1\).

On the final day, either menu is allowed, so the answer is:

\(\max(\mathrm{normal}_N,\mathrm{hard}_N)\)

Furthermore, since the transitions only require the values from the previous day, we do not need to store the results for each day in an array; we can compute everything using just \(2\) variables, normal and hard.

Complexity

  • Time Complexity: \(O(N)\)
    For each day, we perform a constant number of comparisons and additions.
  • Space Complexity: \(O(1)\)
    We can process the input line by line and only use a constant number of variables.

Implementation Notes

Using Values Before Updating

Today’s hard value must be computed using yesterday’s normal. If we update normal first, we would mistakenly use today’s value.

Therefore, the updates are performed in the following order in the code:

next_normal = (normal if normal > hard else hard) + a
hard = normal + b
normal = next_normal

First, we temporarily save today’s normal value in next_normal, compute today’s hard value using yesterday’s normal, and then update normal.

Beware of the Size of the Total Value

The answer can be at most \(N \times 10^9 \leq 2 \times 10^{14}\), which does not fit in a 32-bit integer. This is not an issue with Python’s integers, but in other languages, you must use a 64-bit integer type.

Source Code

import sys


def main():
    readline = sys.stdin.buffer.readline
    n = int(readline())

    normal = 0
    hard = 0

    for _ in range(n):
        a, b = map(int, readline().split())
        next_normal = (normal if normal > hard else hard) + a
        hard = normal + b
        normal = next_normal

    print(normal if normal > hard else hard)


if __name__ == "__main__":
    main()

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

posted:
last update: