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 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
Sep 29, 2023 15:31apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 14:55apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 14:52apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 14:46apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 14:41apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 14:27apuxbtapuxbtCPPg++9.3.0Error
Sep 29, 2023 14:25apuxbtapuxbtCPPg++9.3.0Success196,940+20.79 RP
Sep 28, 2023 23:44apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 23:26apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 23:24apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 23:14apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 23:11apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 23:10apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 23:06apuxbtapuxbtCPPg++9.3.0Success333,458+16.69 RP
Sep 28, 2023 22:33apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 22:33apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 22:32apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 22:29apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 22:03apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:54apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:48apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:43apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:41apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:40apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:35apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:35apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:33apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:32apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:30apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:29apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:27apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:25apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:10apuxbtapuxbtCPPg++9.3.0Success766,075
Sep 28, 2023 21:09apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 21:09apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 20:43apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 20:41apuxbtapuxbtCPPg++9.3.0Error
Sep 28, 2023 18:17apuxbtapuxbtCPPg++9.3.0Success774,069
Sep 28, 2023 17:58apuxbtapuxbtCPPg++9.3.0Success763,835
Sep 28, 2023 17:55apuxbtapuxbtCPPg++9.3.0Success772,726
Sep 28, 2023 17:50apuxbtapuxbtCPPg++9.3.0Success761,066
Sep 27, 2023 21:48apuxbtapuxbtCPPg++9.3.0Success767,871
Sep 27, 2023 21:48apuxbtapuxbtCPPg++9.3.0Success769,988
Sep 27, 2023 21:46apuxbtapuxbtCPPg++9.3.0Success752,019
Sep 27, 2023 21:33apuxbtapuxbtCPPg++9.3.0Success765,429
Sep 27, 2023 21:24apuxbtapuxbtCPPg++9.3.0Error
Sep 3, 2023 19:58Matthew DowneyMatthew DowneyGOgo1.20.7Success1,588,975
Sep 3, 2023 16:50Matthew DowneyMatthew DowneyGOgo1.20.7Success1,569,813
Aug 28, 2023 18:17Matthew DowneyMatthew DowneyGOgo1.20.7Success1,410,691+7.09 RP
Apr 27, 2023 10:00unsatcoreunsatcoreCPPg++9.3.0Success89,519+8.47 RP
Apr 27, 2023 09:47unsatcoreunsatcoreCPPg++9.3.0Success96,867+1.67 RP
Apr 27, 2023 09:45unsatcoreunsatcoreCPPg++9.3.0Error
Apr 27, 2023 09:45unsatcoreunsatcoreCPPg++9.3.0Error
Apr 27, 2023 07:36unsatcoreunsatcoreCPPg++9.3.0Success98,464+3.13 RP
Apr 27, 2023 07:35unsatcoreunsatcoreCPPg++9.3.0Success2,045,593