#K40637. Booking Requests Processing

    ID: 26687 Type: Default 1000ms 256MiB

Booking Requests Processing

Booking Requests Processing

You are given n events, each with a limited number of tickets available, and m booking requests. Each booking request consists of an event date and the number of tickets requested. For each request, process it in order and book as many tickets as possible: that is, if the event has at least the requested number of tickets remaining, book that many; otherwise, book whatever is available. After processing each request, update the remaining tickets for that event accordingly. Your task is to output the number of tickets successfully booked for each request.

The problem requires you to simulate a booking system where the order of requests matters, and each request might reduce the available tickets for a given event.

Note: For each booking request, if the available tickets are less than the requested tickets, you book all the remaining tickets (which might be 0).

The mathematical formulation is as follows: For each booking request (e, r), let \(a_e\) be the remaining tickets for event e. Then, the booked tickets for that request is \(b = \min(r, a_e)\), and the remaining tickets for event e are updated as \(a_e \mathrel{-}= b\).

inputFormat

The input is given from standard input (stdin) and has the following format:

n
max_tickets[0] max_tickets[1] ... max_tickets[n-1]
m
event_1 tickets_requested_1
event_2 tickets_requested_2
... 
event_m tickets_requested_m

Where:

  • n is the number of events.
  • The second line contains n space-separated integers representing the maximum tickets available for each event.
  • m is the number of booking requests.
  • Each of the following m lines contains two integers: the event (1-indexed) and the number of tickets requested for that event.

outputFormat

For each booking request, output the number of tickets that were successfully booked. Each result should be printed on a new line to standard output (stdout).

## sample
5
100 200 150 120 80
4
1 50
3 200
2 150
5 90
50

150 150 80

</p>