Official

A - 予算オーバー / Over Budget Editorial by admin

Qwen3-Coder-480B

Overview

Given the current budget and appropriate budget for each project, find the number of over-budget projects and the total refund amount.

Analysis

For this problem, we need to determine the following two pieces of information for each project:

  1. Whether it is over budget: that is, whether \(A_i > B_i\).
  2. The refund amount: when \(A_i > B_i\), a refund of \(A_i - B_i\) (in ten-thousand yen) is required.

A straightforward approach is sufficient: simply perform the above check and calculation for every project, keeping a count and a running total. Looking at the constraints, \(N\) is at most \(2 \times 10^5\), so as long as we can process each project in constant time, the overall solution will run in time.

Additionally, by processing each project as we read the input, there is no need to store all the data in an array. This also keeps memory usage low.

Algorithm

  1. Read the number of projects \(N\).
  2. For each project:
    • Read \(A_i\) and \(B_i\).
    • If \(A_i > B_i\):
      • Increment the over-budget counter by 1.
      • Add \(A_i - B_i\) to the total refund amount.
  3. Output the counter and the total refund amount.

This process completes by scanning through all projects exactly once.

Example

Sample input:

3
10 5
8 8
12 7

Processing flow:

  • Project 1: \(A_1 = 10\), \(B_1 = 5\) → Over budget, refund \(5\) → Count=1, Total=5
  • Project 2: \(A_2 = 8\), \(B_2 = 8\) → Normal → No change
  • Project 3: \(A_3 = 12\), \(B_3 = 7\) → Over budget, refund \(5\) → Count=2, Total=10

Output: 2 10

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\)

Implementation Notes

  • By processing each project’s input on the fly, there is no need to store data in an array.
  • Use a conditional branch if A > B: to only count and add when necessary.
  • Using meaningful variable names makes the code more readable (e.g., count, total_refund).
## Source Code

```python
N = int(input())
count = 0
total_refund = 0

for _ in range(N):
    A, B = map(int, input().split())
    if A > B:
        count += 1
        total_refund += A - B

print(count, total_refund)

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

posted:
last update: