A - 経費の申請 / Expense Claims Editorial by admin
gpt-6-astra-mediumOverview
Calculate the sum of \(N\) expenses, and output Yes if it is less than or equal to the upper limit of \(B\) yen, or No if it exceeds it.
Analysis
Whether all expenses can be claimed depends solely on the total sum of the expenses. There is no need to consider the order in which each expense is submitted.
Letting the total sum be \(S\), \(S = A_1 + A_2 + \cdots + A_N\) Since the company rule is “up to a total of \(B\) yen,” the condition to check is as follows:
- If \(S \leq B\), all expenses can be claimed, so output
Yes. - If \(S > B\), the limit is exceeded, so output
No.
For example, if the expenses are \(200, 300, 500\) yen and the upper limit is \(1000\) yen, the sum is \(1000\) yen. Since expenses can also be claimed when the sum is equal to the upper limit, the answer is Yes.
In this problem, the naive approach of sequentially adding up all amounts is well within the time limit. However, simply checking whether each individual expense is at most \(B\) yen will fail to detect cases where the total sum exceeds the limit.
Algorithm
- Read \(N\), \(B\), and the list \(A\) containing the expense amounts.
- Compute the total sum of the expenses using
sum(a). - If the sum is less than or equal to \(B\), output
Yes; otherwise, outputNo.
Complexity
- Time Complexity: \(O(N)\)
Reading the \(N\) amounts and computing their sum takes linear time. - Space Complexity: \(O(N)\)
Stores the expense amounts in a list.
Implementation Notes
Since expenses can still be claimed when the total is equal to the upper limit, use
<=rather than<for the comparison.The total can be up to \(100 \times 10^9 = 10^{11}\). While Python’s integer type can handle this directly, other languages will require a 64-bit integer type (such as
long longin C++).Ensure that the output strictly matches the specified case:
YesorNo.Source Code
# 問題文から独立に実装
n, b = map(int, input().split())
a = list(map(int, input().split()))
print("Yes" if sum(a) <= b else "No")
This editorial was generated by gpt-6-astra-medium.