公式

C - 畑の均し作業 / Leveling the Field 解説 by admin

GPT 5.2 High

Overview

We need to find the minimum number of operations to make all sections the same height, where the only allowed operation is adding height \(1\) to a contiguous interval. The conclusion is that “it is optimal to match the final height to the maximum value,” and the answer can be computed in \(O(N)\) using only adjacent differences.

Analysis

1. The final height should be \(\max(A)\)

Since we can only “add” soil, the final height \(H\) must satisfy \(H \ge \max(A)\).
Furthermore, if we make \(H\) larger than \(\max(A)\), we would need to raise all sections even more (for example, additional operations of adding \(1\) to the entire interval would be needed), so the number of operations would never decrease.
Therefore, the minimum number of operations is achieved when we set \(H = \max(A)\).

2. Convert to “how many more times do we need to add”

Let \(H = \max(A) = M\), and define the number of times each section still needs to be raised as [ D_i = M - A_i ] Then the problem becomes equivalent to the following:

  • The initial state is an array of all \(0\)s
  • In one operation, we can do “\(+1\) to a contiguous interval”
  • We want the final array to be \(D\)
    → Find the minimum number of operations

3. A naive approach is too slow

For example, if we try to “add \(1\) one at a time to sections that are too low,” we could need up to \(10^9\) operations, which obviously results in TLE.
What we need is a method that “counts only the number of operations without actually simulating them.”

4. Key observation: operations increase at points where the required amount increases from left to right

Looking from left to right, the value changes from \(D_{i-1}\) to \(D_i\).

  • If \(D_i > D_{i-1}\), then we must start new operations for the increased amount
    (because operations started from the left side alone cannot satisfy the required amount at \(i\))
  • If \(D_i \le D_{i-1}\), the excess can be handled by ending some operations midway, so no new operations are needed

Therefore, the minimum number of operations is [ D1 + \sum{i=2}^{N} \max(0, Di - D{i-1}) ] (using \(1\)-indexed notation).

Furthermore, since [ Di - D{i-1} = (M-Ai) - (M-A{i-1}) = A_{i-1} - A_i ] the answer can be written using only the original array \(A\) as [ (M-A1) + \sum{i=2}^{N} \max(0, A_{i-1} - A_i) ] This is exactly the computation in the provided code.

Concrete example

When \(A=[2,1,3,1]\), \(M=3\), \(D=[1,2,0,2]\). - Initially, \(D_1=1\) operations are needed - \(1 \to 2\): increases by \(+1\), so +1 operation - \(2 \to 0\): decreases, so +0 operations - \(0 \to 2\): increases by \(+2\), so +2 operations
Total: \(1+1+0+2=4\) operations.

Algorithm

  1. Compute \(M = \max(A)\)
  2. Initialize the answer to \(M - A_1\)
  3. For \(i = 2..N\), if \(A_{i-1} > A_i\), add the difference \(A_{i-1} - A_i\) to the answer
  4. Output the result

This computes [ (M-A1) + \sum{i=2}^{N} \max(0, A_{i-1} - A_i) ]

Complexity

  • Time complexity: \(O(N)\) (a single pass for finding the maximum and comparing adjacent elements)
  • Space complexity: \(O(1)\) (only constants beyond the input array)

Implementation Notes

  • Since \(A_i\) can be up to \(10^9\) and \(N\) can be up to \(2\times10^5\), the answer can be on the order of \(10^{14}\). This is not an issue in Python, but in other languages, long long is required.

  • Since the input can be large, using sys.stdin.readline is recommended for safety.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N = int(input().strip())
    A = list(map(int, input().split()))
    mx = max(A)

    ans = mx - A[0]
    for i in range(1, N):
        if A[i - 1] > A[i]:
            ans += A[i - 1] - A[i]

    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.