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 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
Sep 30, 2023 05:47apuxbtapuxbtCPPg++9.3.0Success204,099
Sep 30, 2023 05:42apuxbtapuxbtCPPg++9.3.0Success214,659
Sep 30, 2023 05:40apuxbtapuxbtCPPg++9.3.0Success242,256
Sep 30, 2023 03:42apuxbtapuxbtCPPg++9.3.0Success319,552
Sep 30, 2023 03:39apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 03:38apuxbtapuxbtCPPg++9.3.0Success361,428
Sep 30, 2023 03:35apuxbtapuxbtCPPg++9.3.0Success494,243
Sep 30, 2023 03:34apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 03:01apuxbtapuxbtCPPg++9.3.0Success355,780
Sep 30, 2023 00:41apuxbtapuxbtCPPg++9.3.0Success349,372
Sep 30, 2023 00:38apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 00:19apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 00:18apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 00:14apuxbtapuxbtCPPg++9.3.0Success331,704
Sep 30, 2023 00:14apuxbtapuxbtCPPg++9.3.0Success335,461
Sep 30, 2023 00:01apuxbtapuxbtCPPg++9.3.0Error
Sep 30, 2023 00:00apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 23:56apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 23:55apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 23:54apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 23:49apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:54apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:49apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:47apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:47apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:44apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:39apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:31apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 22:29apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:34apuxbtapuxbtCPPg++9.3.0Success344,046
Sep 29, 2023 20:32apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:31apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:29apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:17apuxbtapuxbtCPPg++9.3.0Success219,758
Sep 29, 2023 20:15apuxbtapuxbtCPPg++9.3.0Success242,581
Sep 29, 2023 20:12apuxbtapuxbtCPPg++9.3.0Success270,823
Sep 29, 2023 20:10apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:08apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:06apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:05apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 20:02apuxbtapuxbtCPPg++9.3.0Success296,907
Sep 29, 2023 20:01apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 19:59apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 15:40apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 15:33apuxbtapuxbtCPPg++9.3.0Error