Official

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

Claude 4.6 Opus (Thinking)

Overview

This is a problem where you select a contiguous interval of trees and maximize the profit, defined as the apple harvest minus the cost. This can be reduced to the Maximum Subarray Problem and solved efficiently using Kadane’s algorithm.

Analysis

Reformulating the Problem

The profit when selecting trees from the \(l\)-th to the \(r\)-th is:

\[\sum_{i=l}^{r} A_i - (r - l + 1) \times C = \sum_{i=l}^{r} (A_i - C)\]

This can be transformed as shown above. In other words, if we define a new value \(B_i = A_i - C\) for each tree \(i\), the problem reduces to: “Find the maximum sum of a contiguous subsequence of the sequence \(B_1, B_2, \ldots, B_N\) (where the empty interval is also allowed, in which case the sum is \(0\)).”

Naive Approach

Trying all intervals \((l, r)\) requires \(O(N^2)\) combinations, and for \(N \leq 150000\), this means up to approximately \(1.125 \times 10^{10}\) computations, which will result in TLE.

Efficient Solution

A well-known technique for finding the maximum sum of a contiguous subarray is Kadane’s algorithm, which solves the problem in \(O(N)\).

Algorithm

Kadane’s algorithm is based on the following idea:

  • current: a variable that tracks “the maximum sum of a contiguous subsequence ending at the current position”
  • For each element \(B_i = A_i - C\) processed in order, compute current += B_i
  • If current becomes negative, discard the current interval and reset to \(0\) (it is better to start a new interval from here)
  • At each step, check whether current is the largest seen so far and update max_profit

Concrete example: When \(N = 5\), \(C = 3\), \(A = [1, 5, 4, 2, 8]\)

\(i\) \(A_i\) \(B_i = A_i - C\) current max_profit
1 1 \(-2\) \(0\) (reset) 0
2 5 \(2\) \(2\) 2
3 4 \(1\) \(3\) 3
4 2 \(-1\) \(2\) 3
5 8 \(5\) \(7\) 7

The answer is \(7\) (selecting trees 2 through 5: \((5+4+2+8) - 4 \times 3 = 19 - 12 = 7\)).

Complexity

  • Time complexity: \(O(N)\) — only a single pass through the array
  • Space complexity: \(O(1)\) — excluding input reading, only a few variables are used (the input reading part is \(O(N)\))

Implementation Notes

  • There is no need to explicitly store \(B_i = A_i - C\) as an array; at each step, simply compute a - C and add it to current

  • By initializing max_profit to \(0\), the option of “selecting no trees (profit \(0\))” is naturally handled

  • Since \(A_i\) and \(C\) can be up to \(10^9\), products and sums can become large, but Python has no integer overflow, so computations can be done safely

  • By reading all input at once with sys.stdin.read(), even large inputs can be processed efficiently

    Source Code

import sys

def main():
    input_data = sys.stdin.read().split()
    N = int(input_data[0])
    C = int(input_data[1])
    
    max_profit = 0
    current = 0
    
    for i in range(N):
        a = int(input_data[2 + i])
        current += a - C
        if current > max_profit:
            max_profit = current
        if current < 0:
            current = 0
    
    print(max_profit)

main()

This editorial was generated by claude4.6opus-thinking.