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 5, 2023 14:58apuxbtapuxbtCPPclang++10.0.0Success78,493
Oct 5, 2023 08:01Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:59Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:58Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:56Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:55Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:54Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:52Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:50Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:48Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:30Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:26Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:26Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:25Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:25Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:23Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:20Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:19Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:14Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:12Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:06Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:05Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:05Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:04Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:03Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:02Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 07:00Robert BurkeRobert BurkeCPPg++9.3.0Error
Oct 5, 2023 01:07apuxbtapuxbtCPPclang++10.0.0Success78,078+8.91 RP
Oct 5, 2023 01:03apuxbtapuxbtCPPg++9.3.0Success84,053
Oct 5, 2023 01:03apuxbtapuxbtCPPg++9.3.0Success85,506
Oct 5, 2023 01:02apuxbtapuxbtCPPg++9.3.0Success83,914+1.13 RP
Oct 5, 2023 01:01apuxbtapuxbtCPPg++9.3.0Success84,718+3.71 RP
Oct 5, 2023 01:01apuxbtapuxbtCPPg++9.3.0Error
Oct 5, 2023 00:57apuxbtapuxbtCPPg++9.3.0Error
Oct 5, 2023 00:55apuxbtapuxbtCPPg++9.3.0Error
Oct 5, 2023 00:51apuxbtapuxbtCPPg++9.3.0Error
Oct 5, 2023 00:27apuxbtapuxbtCPPg++9.3.0Error
Oct 5, 2023 00:23apuxbtapuxbtCPPg++9.3.0Error
Oct 4, 2023 23:58apuxbtapuxbtCPPg++9.3.0Success87,469+17.05 RP
Oct 4, 2023 23:56apuxbtapuxbtCPPg++9.3.0Error
Oct 4, 2023 23:45apuxbtapuxbtCPPg++9.3.0Error
Oct 4, 2023 23:32apuxbtapuxbtCPPg++9.3.0Success112,036
Oct 4, 2023 23:17apuxbtapuxbtCPPg++9.3.0Success102,800+6.42 RP
Oct 4, 2023 22:51apuxbtapuxbtCPPg++9.3.0Success113,067
Oct 4, 2023 21:58apuxbtapuxbtCPPg++9.3.0Success110,058+3.03 RP
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