Official

D - 夏祭りの出店計画 / Summer Festival Booth Planning Editorial by admin

Qwen3-Coder-480B

Overview

In an \(N\)-day summer festival, we need to select two booth periods of exactly \(K\) consecutive days each and maximize the total sales. The two periods must not overlap and must have at least 1 day gap between them.

Analysis

First, each booth period consists of “exactly \(K\) consecutive days,” and the sales equal the sum over that interval. Therefore, it is efficient to precompute the total sales for all possible intervals.

Naive Approach and Its Issues

A straightforward approach would be to try all possible combinations of two intervals. However, since the number of intervals is approximately \(N - K + 1\), the number of combinations becomes quadratic, which is infeasible given the constraint that \(N\) can be up to \(10^6\) (TLE).

Key Insight for Improvement

What we want to find is the maximum total sales of a “left interval” and a “right interval sufficiently far from it.” The key observation is that when we fix the right interval, we need to efficiently find which left interval (among those that can be used before it) has the largest sales.

By using prefix sums to precompute the total sales for each interval, and additionally precomputing and storing the maximum value among left intervals, we can update the answer while scanning through right intervals.

Algorithm

  1. Compute prefix sums
    Compute the prefix sum array \(prefix\_sum\) of array \(A\). This allows us to compute the sum of any interval \([l, r]\) in \(O(1)\).

  2. Precompute total sales for each interval
    For each starting position \(i\), store the sum of the interval \([i, i+K-1]\) as \(interval\_sums[i]\).

  3. Precompute maximum values on the left
    Compute \(max\_left[i]\) as “the maximum total sales among intervals from the \(0\)-th to the \(i\)-th.”

  4. Scan right intervals and update the maximum
    Scan the starting position \(j\) of the right interval from \(K+1\) onward, and add the corresponding left maximum (\(max\_left[j-K-1]\)) to update the overall maximum.

Complexity

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

Implementation Notes

  • Use prefix sums to efficiently compute interval sums.

  • By precomputing the left maximum values, the problem can be solved with a linear scan instead of an exhaustive search.

  • Be careful with index ranges (in particular, ensure that \(j - K - 1\) does not become negative).

    Source Code

import sys
from itertools import accumulate

def main():
    input = sys.stdin.read
    data = input().split()
    
    N = int(data[0])
    K = int(data[1])
    A = list(map(int, data[2:]))

    # 累積和を計算
    prefix_sum = [0] + list(accumulate(A))
    
    # 各開始位置におけるK日間の合計を計算
    interval_sums = [prefix_sum[i + K] - prefix_sum[i] for i in range(N - K + 1)]
    
    # 左側の最大値を保持しながら右側を探す
    max_left = [0] * (N - K + 1)
    max_left[0] = interval_sums[0]
    for i in range(1, N - K + 1):
        max_left[i] = max(max_left[i - 1], interval_sums[i])
    
    # 最大の合計を求める
    max_total = 0
    for j in range(K + 1, N - K + 1):  # 右側の開始位置
        right_sum = interval_sums[j]
        left_max = max_left[j - K - 1]
        total = left_max + right_sum
        if total > max_total:
            max_total = total
    
    print(max_total)

if __name__ == "__main__":
    main()

This editorial was generated by qwen3-coder-480b.