Official

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

gemini-3-flash-thinking

Overview

This problem is a maximum subarray sum problem, where we select a contiguous interval from a sequence of elements and maximize the total value. By subtracting a fixed cost from each element’s value, we can transform it into a form where standard algorithms can be applied.

Analysis

1. Transforming the Formula

First, let’s focus on the formula for calculating profit. The profit when selecting trees from the \(l\)-th to the \(r\)-th is as follows: $\( \left(\sum_{i=l}^{r} A_i\right) - (r - l + 1) \times C \)\( Here, \)(r - l + 1)\( is the number of selected trees. This formula can be rewritten by subtracting \)C\( individually from each \)A_i\(: \)\( (A_l - C) + (A_{l+1} - C) + \dots + (A_r - C) = \sum_{i=l}^{r} (A_i - C) \)\( In other words, if we precompute "the number of apples obtained minus the cost" \)B_i = A_i - C\( for every tree, the problem reduces to the simple form of **"finding the maximum sum of a contiguous subarray of array \)B$“**.

2. Naive Approach and Its Limitations

If we try all combinations of intervals \((l, r)\), there are \(O(N^2)\) possible intervals. Given the constraint \(N \leq 150,000\) in this problem, \(N^2 \approx 2.25 \times 10^{10}\), which will not finish within the time limit. Therefore, a more efficient \(O(N)\) or \(O(N \log N)\) algorithm is needed.

3. Optimal Solution

While keeping in mind the option of “selecting no trees (profit 0)”, we scan the array from the beginning and update the current sum. If the sum becomes negative at any point, it is more profitable to discard the interval up to that point (i.e., start a new interval from the next element). This is a technique known as Kadane’s Algorithm.

Algorithm

Kadane’s Algorithm

  1. Initialize a variable max_profit to hold the maximum profit to 0.
  2. Initialize a variable current_profit to hold the sum of the current interval to 0.
  3. For each element \(A_i\) in the array, repeat the following operations:
    • Add \((A_i - C)\) to current_profit.
    • If current_profit becomes negative, reset current_profit to 0 (deciding to discard the interval up to this point).
    • Update max_profit with max(max_profit, current_profit).
  4. The final max_profit is the answer.

Complexity

  • Time Complexity: \(O(N)\)
    • Since we only scan the array once, it runs in linear time with respect to the number of elements \(N\).
  • Space Complexity: \(O(N)\)
    • Depends on the array size needed to store the input values.

Implementation Notes

  • Cost Subtraction: When subtracting \(C\) from each \(A_i\), note that \(A_i\) and \(C\) can be as large as \(10^9\), so the result of the computation may be negative, and the final total may exceed a 32-bit integer. Be careful of this (in Python, integers are automatically handled as arbitrary-precision integers, so there is no concern about overflow).

  • Initial Value: Since the profit of selecting no trees is 0, the initial value of max_profit should be set to 0, ensuring that a value of 0 or greater is always maintained.

    Source Code

import sys

def solve():
    # 入力を一度に読み込む
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    n = int(input_data[0])
    c = int(input_data[1])
    a = list(map(int, input_data[2:]))
    
    # 各要素からコストCを引いた配列を作成
    # 利益 = sum(A_i) - (r - l + 1) * C = sum(A_i - C)
    # これは最大部分配列和問題に帰着される
    
    max_profit = 0
    current_profit = 0
    
    # カデインのアルゴリズム (Kadane's Algorithm) を使用して
    # 最大部分配列和を O(N) で求める
    for val in a:
        current_profit += (val - c)
        if current_profit < 0:
            current_profit = 0
        if current_profit > max_profit:
            max_profit = current_profit
            
    print(max_profit)

if __name__ == "__main__":
    solve()

This editorial was generated by gemini-3-flash-thinking.

posted:
last update: