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
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
Sep 30, 2023 09:21apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 09:09apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 09:07apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:50apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:47apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:46apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:43apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:40apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:34apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:24apuxbtapuxbtCPPg++9.3.0Success124,262+0.37 RP
Sep 30, 2023 08:23apuxbtapuxbtCPPg++9.3.0Success125,152
Sep 30, 2023 08:22apuxbtapuxbtCPPg++9.3.0Success133,451
Sep 30, 2023 08:20apuxbtapuxbtCPPg++9.3.0Success125,347
Sep 30, 2023 08:18apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 08:17apuxbtapuxbtCPPg++9.3.0Success124,836+2.91 RP
Sep 30, 2023 08:07apuxbtapuxbtCPPg++9.3.0Success131,007
Sep 30, 2023 07:52apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:39apuxbtapuxbtCPPg++9.3.0Success129,543+1.28 RP
Sep 30, 2023 07:37apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:34apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:31apuxbtapuxbtCPPg++9.3.0Success131,721+0.36 RP
Sep 30, 2023 07:27apuxbtapuxbtCPPg++9.3.0Success132,349+0.56 RP
Sep 30, 2023 07:21apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:19apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:18apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:17apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:17apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:17apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:16apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:16apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:15apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 07:08apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:49apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:44apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:41apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:40apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:37apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:36apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:31apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:30apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:27apuxbtapuxbtCPPg++9.3.0Success133,334+4.20 RP
Sep 30, 2023 06:26apuxbtapuxbtCPPg++9.3.0Success156,118
Sep 30, 2023 06:25apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 06:25apuxbtapuxbtCPPg++9.3.0Success174,698
Sep 30, 2023 06:25apuxbtapuxbtCPPg++9.3.0Success162,797
Sep 30, 2023 06:17apuxbtapuxbtCPPg++9.3.0Success141,240+5.14 RP
Sep 30, 2023 06:11apuxbtapuxbtCPPg++9.3.0Success152,307+7.37 RP
Sep 30, 2023 06:08apuxbtapuxbtCPPg++9.3.0Success172,167
Sep 30, 2023 06:06apuxbtapuxbtCPPg++9.3.0Success224,885
Sep 30, 2023 06:06apuxbtapuxbtCPPg++9.3.0Success199,531
Sep 30, 2023 06:05apuxbtapuxbtCPPg++9.3.0Success172,572
Sep 30, 2023 06:05apuxbtapuxbtCPPg++9.3.0Success171,560+1.38 RP
Sep 30, 2023 06:05apuxbtapuxbtCPPg++9.3.0Success175,719+6.13 RP
Sep 30, 2023 06:00apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 05:47apuxbtapuxbtCPPg++9.3.0Success200,166