A - 水の補充 / Refilling Water Editorial by admin
GPT 5.2 HighOverview
Compute the total amount of water needed for all flower pots \(T=\sum_{i=1}^{N} R_i\), then calculate the cost by multiplying the shortfall (the amount that must be purchased from the water supply beyond what the watering can holds \(S\)) by the unit price \(C\).
Analysis
The key insight is that the “amount to purchase from the water supply” is determined solely by the difference between the total requirement \(T\) and the available water \(S\).
- If \(T \le S\), the watering can alone is sufficient (it’s fine to have leftovers), so the purchase amount is \(0\) liters and the cost is \(0\) yen.
- If \(T > S\), the shortfall is \(T-S\) liters, so the cost is \((T-S)\times C\) yen.
In other words, there is no need to simulate watering each flower pot in order. As long as we know the total, we can compute the answer in one step.
A naive implementation that “decreases water for each flower pot” would still have \(O(N)\) time complexity, but since all we need is the total, it is most concise and least error-prone to organize the solution as “total → difference → cost”.
Also, since \(N \le 10^6\) can be large, in Python it is effective to read input efficiently (using sys.stdin.buffer.read()).
Examples: - If \(R=[2,3,4]\), then \(T=9\) - When \(S=7\): shortfall \(=9-7=2\), cost \(=2C\) - When \(S=10\): shortfall \(=-1\), so no purchase needed, cost \(0\)
Algorithm
- Read \(N,S,C\) from input.
- Compute the total \(T=\sum R_i\) of \(R_1 \ldots R_N\).
- Compute the shortfall \(D = T - S\).
- If \(D>0\), output \(D \times C\); otherwise, output \(0\).
Complexity
- Time complexity: \(O(N)\) (just summing the \(R_i\))
- Space complexity: \(O(1)\) (only variables like the total. ※In practice, the implementation may create an array for fast input reading, so it actually uses space proportional to the input size)
Implementation Notes
Fast input: Since \(N\) can be up to \(10^6\), using
sys.stdin.buffer.read().split()is consistently faster than repeatedinput()calls.Overflow considerations: \(T\) can be at most \(10^6 \times 10^4 = 10^{10}\), and the cost \((T-S)\times C\) can be at most \(10^{10}\times 10^4=10^{14}\). Python’s integers have arbitrary precision, so this is not a problem (in other languages, 64-bit integers would be required).
Watch for negative shortfall: Even if \(T-S\) is negative, the purchase amount is \(0\), so we only charge when \(D>0\).
Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
if not data:
return
N, S, C = data[0], data[1], data[2]
total = sum(data[3:3+N])
deficit = total - S
if deficit > 0:
print(deficit * C)
else:
print(0)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.