Official

C - りんご狩りの最適プラン / Optimal Apple Picking Plan Editorial by admin Editorial for printing

GPT 5.4 High

Overview

If we consider the “effective profit” of harvesting each tree as \(A_i - C\), the problem reduces to finding the “maximum subarray sum” of a contiguous subsequence.
Since we can also choose an empty interval, the answer is the maximum subarray sum that is at least \(0\).

Analysis

The profit of a chosen interval \([l, r]\) is

\( \left(\sum_{i=l}^{r} A_i\right) - (r-l+1)C \)

Rearranging this, we get:

\( \sum_{i=l}^{r}(A_i - C) \)

In other words, if we define \(B_i = A_i - C\) for each tree, the problem becomes finding the “maximum sum of \(B_i\) over a contiguous interval.”


Naive Approach

If we try all intervals \([l, r]\) and compute their sums, there are \(O(N^2)\) intervals.
Since \(N \le 150000\), this will not be fast enough.

Even if we use prefix sums to compute each interval’s sum in \(O(1)\), the total number of intervals is too large, so brute force will result in TLE.


Key Insight

This problem is equivalent to the “maximum subarray problem.”
This can be solved in \(O(N)\) using the well-known Kadane’s algorithm.

The idea is as follows:

  • \(current\) := “the maximum profit among intervals ending at the current position”
  • Let the effective profit of the next tree be \(x = A_i - C\), then:
    • Extend the previous interval: \(current + x\)
    • Start a new interval here: \(x\)
    • However, since an empty interval is also allowed, we can reset to \(0\) if the value is negative

Therefore, the update formula is:

\( current = \max(0, current + (A_i - C)) \)

Then, taking the maximum of \(current\) at each step gives us the answer.


Concrete Example

For example, consider:

  • \(C = 5\)
  • \(A = [4, 10, 3, 8]\)

The effective profit for each tree is:

\( [-1, 5, -2, 3] \)

Considering the maximum sum of contiguous subsequences of this array:

  • \([-1] = -1\)
  • \([5] = 5\)
  • \([5, -2] = 3\)
  • \([5, -2, 3] = 6\)

The maximum is \(6\).

In terms of the original problem, this corresponds to the profit when selecting the 2nd through 4th trees:

\( (10 + 3 + 8) - 3 \times 5 = 21 - 15 = 6 \)

Algorithm

  1. Initialize \(best = 0\), \(current = 0\).
  2. For each tree, read the value \(a\).
  3. Add the effective profit \(a - C\) to the current interval.
  4. If the result becomes negative, that interval has no value going forward, so reset to \(0\).
    • \(current = \max(0, current + a - C)\)
  5. Update the maximum with \(best = \max(best, current)\).
  6. Output \(best\) at the end.

This is exactly Kadane’s algorithm.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\)

Implementation Notes

  • \(A_i\) and \(C\) can be large, but Python’s integers can handle them directly.

  • There is no need to store the entire array. It is sufficient to read and update one element at a time.

  • Since we can choose an empty interval, the initial value of the answer should be \(0\).

  • The single line current = max(0, current + a - C) is the essence of this problem.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N, C = map(int, input().split())
    
    best = 0
    current = 0
    
    for _ in range(N):
        a = int(input())
        current = max(0, current + a - C)
        if current > best:
            best = current
    
    print(best)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.4-high.

posted:
last update: