Official

A - 受付窓口の終了時刻 / Closing Time of the Reception Window Editorial by admin

gpt-5.3-codex

Overview

This is a “single queue simulation” where visitors are processed one by one in order.
By continuously updating the time when the counter next becomes free, we can directly obtain the finish time of the last visitor.

Analysis

The key point is that the start time of each visitor \(i\) is determined by
\(S_i = \max(\text{finish time of the previous visitor}, A_i)\)

In other words, the only information we need to maintain at any point is “when the counter becomes free.”
If we call this end_time, then for visitor \((A_i, B_i)\):

  1. Wait if the visitor hasn’t arrived yet: end_time = max(end_time, A_i)
  2. Advance by the service time \(B_i\): end_time += B_i

This gives the finish time of visitor \(i\).


A naive simulation that “advances time one unit at a time” would be far too slow, since times can be on the order of \(10^9\).
Also, there is no need to search through all past results each time to determine “when can this visitor start” — the result up to the previous visitor (i.e., when the counter becomes free) is sufficient.
By reducing it to this single-variable update, the problem can be solved with just \(N\) iterations of a loop.


For example:

  • Visitor 1: \((A_1,B_1)=(2,3)\)
  • Visitor 2: \((A_2,B_2)=(2,4)\)
  • Visitor 3: \((A_3,B_3)=(10,1)\)

Then:

  • Initial end_time=0
  • Visitor 1: max(0,2)=2, then +3 gives 5
  • Visitor 2: max(5,2)=5, then +4 gives 9
  • Visitor 3: max(9,10)=10, then +1 gives 11

Therefore the answer is 11.

Algorithm

  1. Initialize end_time = 0 (the time when the counter is free).
  2. Read visitors from the \(1\)st to the \(N\)th in order.
  3. For each visitor \((A, B)\):
    • end_time = max(end_time, A)
      (wait if the visitor has not yet arrived)
    • end_time += B
      (process the visitor and advance to the finish time)
  4. Output end_time after the loop ends. This is the finish time of the last visitor.

Complexity

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

Implementation Notes

  • Since the constraints are large, using sys.stdin.readline in Python ensures stable and fast input.

  • The time value can grow up to approximately \(10^9 + N \cdot 10^9\), so depending on the language, 64-bit integers may be required (Python has arbitrary-precision integers, so this is not an issue).

  • \(A_i\) is monotonically non-decreasing, but this solution does not overly rely on that property — it works correctly simply by processing visitors in order.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    end_time = 0  # current time when counter becomes free

    for _ in range(N):
        A, B = map(int, input().split())
        if end_time < A:
            end_time = A
        end_time += B

    print(end_time)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.3-codex.

posted:
last update: