公式

A - 水の補充 / Refilling Water 解説 by admin

Qwen3-Coder-480B

Overview

A problem where you compare the total amount of water needed with the amount of water you have, and calculate the cost of purchasing the shortfall from the water supply.

Analysis

In this problem, the amount of water \(R_i\) required for each of the \(N\) flower pots is given, and their sum represents the total water requirement. That is, $\( \text{Total water required} = R_1 + R_2 + \cdots + R_N \)\( By comparing this with the amount of water \)S$ that Takahashi has, we determine how much additional water needs to be purchased.

If \(S\) is greater than or equal to the total, there is no need to buy water from the supply, and the cost is \(0\) yen. Conversely, if \(S\) is insufficient, the shortfall is purchased from the water supply at \(C\) yen per liter.

The key point is that instead of considering each flower pot individually, the decision is based on the “overall total.” This is clear from the problem statement’s description that “the total amount of water needed is \(R_1 + R_2 + \cdots + R_N\) liters.”

Also, given the constraints that \(N\) can be up to \(10^6\) and each \(R_i\) can be up to \(10^4\), the total can reach approximately \(10^{10}\). However, this is within the range handled by Python’s standard integer type, so there is no issue.

Algorithm

  1. Sum up the water required for each flower pot \(R_i\) to obtain the total water requirement total_required.
  2. Calculate the difference from the available water \(S\) to find the shortfall shortage. Use max(0, total_required - S) to ensure it does not become negative.
  3. If there is a shortfall, purchase it from the water supply at \(C\) yen per liter, so the cost is shortage * C.
  4. Output the result.

Complexity

  • Time complexity: \(O(N)\) (the bottleneck is the operation of summing the array)
  • Space complexity: \(O(N)\) (for storing the input list)

Implementation Notes

  • Using the sum() function to calculate the total allows for concise code.

  • The key point is using max(0, ...) to ensure the shortfall does not become negative.

  • Input can be efficiently read using map(int, input().split()).

    Source Code

# 入力の読み込み
N, S, C = map(int, input().split())
R = list(map(int, input().split()))

# 必要な水の総量を計算
total_required = sum(R)

# 不足分を計算(ただし最低でも0)
shortage = max(0, total_required - S)

# 水道から買う必要がある場合、その費用を計算
cost = shortage * C

# 結果の出力
print(cost)

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