A - 経費の申請 / Expense Claims Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
This problem asks us to find the sum of \(N\) expenses, and output Yes if the total is at most the upper limit of \(B\) yen, or No if it exceeds the limit.
Analysis
Since we need to determine whether all expenses can be claimed, there is no need to select or reorder them. We can make the determination simply by calculating the total amount \(S = A_1 + A_2 + \cdots + A_N\) .
- If \(S \leq B\), all can be claimed, so output
Yes - If \(S > B\), it exceeds the upper limit, so output
No
For example, if the expenses are \(100, 200, 300\) yen, the sum is \(600\) yen. If the limit is \(600\) yen, the answer is Yes; if the limit is \(500\) yen, the answer is No.
In this problem, the straightforward approach of summing all the amounts in order easily runs in time. However, “up to \(B\) yen” includes the case where the total is exactly \(B\) yen, so comparing with \(S < B\) will result in a wrong answer.
Algorithm
- Read \(N\), \(B\), and the amounts of the \(N\) expenses.
- Use
sumto calculate the sum of the expenses,total. - Output
Yesiftotal <= b, andNootherwise.
The value total obtained in this process is the sum when claiming all expenses. By checking whether this value is less than or equal to the upper limit, we can correctly determine the result according to the problem statement.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Summing the \(N\) amounts takes \(O(N)\) time, so the time complexity is \(O(N)\). In the provided code, the space complexity is also \(O(N)\) because the read inputs are stored in a list.
Implementation Notes
data[0]corresponds to \(N\),data[1]corresponds to \(B\), anddata[2:2 + n]corresponds to the \(N\) expenses.Since expenses can still be claimed if the total is exactly at the limit, use
<=for the comparison.The total sum of the expenses can be up to \(100 \times 10^9 = 10^{11}\). Although this exceeds the range of a signed 32-bit integer, Python handles arbitrarily large integers automatically.
Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
n, b = data[0], data[1]
total = sum(data[2:2 + n])
print("Yes" if total <= b else "No")
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: