Order book Yuriy Lyfenko

Simulate a sell-side order book and calculate the cost of a final purchase as fast as possible.

Input

1,000,000 order updates on STDIN, one per line:

  • + <price> <size> – add a new sell order
  • - <position> – delete the order at the given position
  • = <size> – buy <size> shares from the top of the order book

After all updates are processed, buy 1,000 shares from the top of the order book.

Output

Print the total cost of the final 1,000-share purchase to STDOUT.

Order Book Rules

Orders are sorted by price ascending (lower is better). Orders at the same price are sorted by arrival time (earlier first). Position 0 is the best (lowest-price) offer.

The = (buy) operation consumes shares starting from position 0. If an order is fully consumed, it is removed from the book.

Example

Input Order book state
+ 1137 100 (1137,100)
+ 1130 10 (1130,10), (1137,100)
+ 1130 50 (1130,10), (1130,50), (1137,100)
- 0 (1130,50), (1137,100)
+ 1150 200 (1130,50), (1137,100), (1150,200)
= 200 (1150,150)

Total cost of the last buy: 50 * 1130 + 100 * 1137 + 50 * 1150.

Date AuthorLanguageStatus Score
Oct 4, 2023 21:58apuxbtapuxbtCPPg++9.3.0Error
Oct 4, 2023 21:34apuxbtapuxbtCPPg++9.3.0Error
Oct 4, 2023 21:33apuxbtapuxbtCPPg++9.3.0Error
Oct 4, 2023 21:14apuxbtapuxbtCPPg++9.3.0Success157,368
Oct 4, 2023 21:09apuxbtapuxbtCPPg++9.3.0Error
Oct 4, 2023 20:39apuxbtapuxbtCPPg++9.3.0Success113,850+6.33 RP
Oct 1, 2023 22:21apuxbtapuxbtCPPg++9.3.0Success123,819
Oct 1, 2023 22:02apuxbtapuxbtCPPg++9.3.0Success122,697+0.20 RP
Oct 1, 2023 21:53apuxbtapuxbtCPPg++9.3.0Success123,003+0.43 RP
Oct 1, 2023 20:38apuxbtapuxbtCPPg++9.3.0Success126,775
Oct 1, 2023 20:30apuxbtapuxbtCPPg++9.3.0Success123,974
Oct 1, 2023 19:59apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:56apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:53apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:51apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:48apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:48apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:46apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:45apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:44apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:44apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:43apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:42apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:39apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:38apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:35apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:30apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:30apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 19:25apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 18:09apuxbtapuxbtCPPg++9.3.0Success174,109
Oct 1, 2023 18:06apuxbtapuxbtCPPg++9.3.0Success138,645
Oct 1, 2023 17:34apuxbtapuxbtCPPg++9.3.0Error
Oct 1, 2023 16:53apuxbtapuxbtCPPg++9.3.0Success142,517
Sep 30, 2023 16:53apuxbtapuxbtCPPg++9.3.0Success125,017
Sep 30, 2023 16:52apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 16:15apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 16:09apuxbtapuxbtCPPg++9.3.0Success127,002
Sep 30, 2023 15:53apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:34apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:32apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:31apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:30apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:29apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:26apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:25apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:24apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:23apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:17apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:16apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:12apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:09apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:08apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:06apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:05apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:04apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:03apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 15:00apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:57apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:54apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:53apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:51apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:32apuxbtapuxbtCPPg++9.3.0Success127,813
Sep 30, 2023 14:29apuxbtapuxbtCPPg++9.3.0Success126,017
Sep 30, 2023 14:24apuxbtapuxbtCPPg++9.3.0Success126,941
Sep 30, 2023 14:22apuxbtapuxbtCPPg++9.3.0Success125,620
Sep 30, 2023 14:21apuxbtapuxbtCPPg++9.3.0Success127,165
Sep 30, 2023 14:13apuxbtapuxbtCPPg++9.3.0Success126,862
Sep 30, 2023 14:12apuxbtapuxbtCPPg++9.3.0Success126,092
Sep 30, 2023 14:11apuxbtapuxbtCPPg++9.3.0Success124,067
Sep 30, 2023 14:10apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:09apuxbtapuxbtCPPg++9.3.0Success123,656+0.25 RP
Sep 30, 2023 14:08apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:04apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 14:01apuxbtapuxbtCPPg++9.3.0Success127,898
Sep 30, 2023 14:01apuxbtapuxbtCPPg++9.3.0Success124,041+0.14 RP
Sep 30, 2023 13:57apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 13:50apuxbtapuxbtCPPg++9.3.0Success177,905
Sep 30, 2023 13:49apuxbtapuxbtCPPg++9.3.0Success177,363
Sep 30, 2023 13:48apuxbtapuxbtCPPg++9.3.0Success126,917
Sep 30, 2023 13:44apuxbtapuxbtCPPg++9.3.0Success127,898
Sep 30, 2023 13:40apuxbtapuxbtCPPg++9.3.0Success135,677
Sep 30, 2023 13:39apuxbtapuxbtCPPg++9.3.0Success134,366
Sep 30, 2023 13:39apuxbtapuxbtCPPg++9.3.0Success135,533
Sep 30, 2023 11:00apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:54apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:46apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:40apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:38apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:36apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:20apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:17apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:16apuxbtapuxbtCPPg++9.3.0Success124,907
Sep 30, 2023 10:15apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:14apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:09apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:08apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 10:07apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 09:54apuxbtapuxbtCPPg++9.3.0Success141,611
Sep 30, 2023 09:53apuxbtapuxbtCPPg++9.3.0Success351,966
Sep 30, 2023 09:24apuxbtapuxbtCPPg++9.3.0Error