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
Mar 14, 2026 16:45Dominique GarmierDominique GarmierCPPclang++18.1.3Success82,681+6.27 RP
Mar 14, 2026 16:44Dominique GarmierDominique GarmierCPPclang++18.1.3Success181,109
Mar 14, 2026 16:33olliecrowolliecrowCPPg++13.3.0Success5,940
Mar 14, 2026 16:27olliecrowolliecrowCPPg++13.3.0Success5,876
Mar 14, 2026 16:24olliecrowolliecrowCPPg++13.3.0Success6,022
Mar 14, 2026 15:50Dominique GarmierDominique GarmierCPPclang++18.1.3Error
Mar 14, 2026 15:35olliecrowolliecrowCPPg++13.3.0Success5,959
Mar 14, 2026 15:16olliecrowolliecrowCPPg++13.3.0Success5,955
Mar 14, 2026 15:11olliecrowolliecrowCPPg++13.3.0Success6,167
Mar 14, 2026 15:04olliecrowolliecrowCPPg++13.3.0Success6,145
Mar 14, 2026 14:32olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 14:23olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 14:18olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 14:01olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 13:47olliecrowolliecrowCPPg++13.3.0Success6,005
Mar 14, 2026 13:41olliecrowolliecrowCPPg++13.3.0Success5,810
Mar 14, 2026 12:52olliecrowolliecrowCPPg++13.3.0Success5,907
Mar 14, 2026 12:45olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 12:25olliecrowolliecrowCPPg++13.3.0Success6,072
Mar 14, 2026 12:19olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 11:58olliecrowolliecrowCPPg++13.3.0Success6,017
Mar 14, 2026 11:51olliecrowolliecrowCPPg++13.3.0Success5,803+16.76 RP
Mar 14, 2026 11:04olliecrowolliecrowCPPg++13.3.0Success5,934
Mar 14, 2026 11:00olliecrowolliecrowCPPg++13.3.0Success6,141
Mar 14, 2026 10:48olliecrowolliecrowCPPg++13.3.0Success5,945
Mar 14, 2026 10:42olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 10:28olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 10:24olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 09:47olliecrowolliecrowCPPg++13.3.0Success5,953
Mar 14, 2026 09:43olliecrowolliecrowCPPg++13.3.0Success6,026
Mar 14, 2026 09:38olliecrowolliecrowCPPg++13.3.0Success6,119
Mar 14, 2026 09:28olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 09:14olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 09:07olliecrowolliecrowCPPg++13.3.0Success5,990
Mar 14, 2026 08:22olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 08:06olliecrowolliecrowCPPg++13.3.0Success5,860
Mar 14, 2026 08:03olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 07:51olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 07:46olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 07:43olliecrowolliecrowCPPg++13.3.0Success6,036
Mar 14, 2026 07:27olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 07:22olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 06:37olliecrowolliecrowCPPg++13.3.0Success6,010
Mar 14, 2026 06:23olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 05:57olliecrowolliecrowCPPg++13.3.0Success6,088
Mar 14, 2026 05:19olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 05:12olliecrowolliecrowCPPg++13.3.0Error
Mar 14, 2026 04:58olliecrowolliecrowCPPg++13.3.0Success6,378
Mar 14, 2026 04:51olliecrowolliecrowCPPg++13.3.0Success5,898
Mar 13, 2026 19:01olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 18:51olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 18:41olliecrowolliecrowCPPg++13.3.0Success5,945
Mar 13, 2026 18:28olliecrowolliecrowCPPg++13.3.0Success6,095
Mar 13, 2026 18:17olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 18:11olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 17:17olliecrowolliecrowCPPg++13.3.0Success5,947
Mar 13, 2026 16:58olliecrowolliecrowCPPg++13.3.0Success6,029
Mar 13, 2026 16:41olliecrowolliecrowCPPg++13.3.0Success5,964
Mar 13, 2026 16:06olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 15:53olliecrowolliecrowCPPg++13.3.0Success6,016
Mar 13, 2026 15:43olliecrowolliecrowCPPg++13.3.0Success6,079
Mar 13, 2026 15:35olliecrowolliecrowCPPg++13.3.0Success5,909
Mar 13, 2026 15:22richardbranway1richardbranway1CPPg++13.3.0Success77,395
Mar 13, 2026 15:19olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 15:19richardbranway1richardbranway1CPPg++13.3.0Success66,157+7.51 RP
Mar 13, 2026 15:00richardbranway1richardbranway1CPPg++13.3.0Success96,381
Mar 13, 2026 14:49olliecrowolliecrowCPPg++13.3.0Success5,984
Mar 13, 2026 14:49richardbranway1richardbranway1CPPg++13.3.0Success94,038
Mar 13, 2026 14:42richardbranway1richardbranway1CPPg++13.3.0Success179,024
Mar 13, 2026 14:39olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 14:38richardbranway1richardbranway1CPPg++13.3.0Success178,110
Mar 13, 2026 14:33olliecrowolliecrowCPPg++13.3.0Success6,103
Mar 13, 2026 14:29richardbranway1richardbranway1CPPg++13.3.0Success176,452
Mar 13, 2026 14:26richardbranway1richardbranway1CPPg++13.3.0Success171,667
Mar 13, 2026 14:21olliecrowolliecrowCPPg++13.3.0Success6,031
Mar 13, 2026 14:19richardbranway1richardbranway1CPPg++13.3.0Success69,616+8.90 RP
Mar 13, 2026 14:19richardbranway1richardbranway1CPPg++13.3.0Success74,216+29.05 RP
Mar 13, 2026 14:11Dominique GarmierDominique GarmierCPPclang++18.1.3Success87,198+3.36 RP
Mar 13, 2026 14:10Dominique GarmierDominique GarmierCPPclang++18.1.3Error
Mar 13, 2026 14:10Dominique GarmierDominique GarmierCPPclang++18.1.3Error
Mar 13, 2026 14:08richardbranway1richardbranway1CPPg++13.3.0Success94,614+61.36 RP
Mar 13, 2026 14:06Dominique GarmierDominique GarmierCPPclang++18.1.3Success89,826+30.10 RP
Mar 13, 2026 14:04richardbranway1richardbranway1CPPg++13.3.0Success252,400
Mar 13, 2026 14:03Dominique GarmierDominique GarmierCPPg++13.3.0Error
Mar 13, 2026 14:03richardbranway1richardbranway1CPPg++13.3.0Success253,716
Mar 13, 2026 14:02richardbranway1richardbranway1CPPclang++18.1.3Success265,047
Mar 13, 2026 13:59richardbranway1richardbranway1CPPg++13.3.0Success252,690
Mar 13, 2026 13:58richardbranway1richardbranway1CPPg++13.3.0Success253,698
Mar 13, 2026 13:56Dominique GarmierDominique GarmierCPPclang++18.1.3Success123,112+25.07 RP
Mar 13, 2026 13:55Dominique GarmierDominique GarmierCPPclang++18.1.3Success181,041
Mar 13, 2026 13:54richardbranway1richardbranway1CPPg++13.3.0Success255,891
Mar 13, 2026 13:54Dominique GarmierDominique GarmierCPPclang++18.1.3Success178,067+22.39 RP
Mar 13, 2026 13:51richardbranway1richardbranway1CPPg++13.3.0Success268,214
Mar 13, 2026 13:42richardbranway1richardbranway1CPPg++13.3.0Success314,900
Mar 13, 2026 13:40olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:35olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:29richardbranway1richardbranway1CPPg++13.3.0Success225,555+0.08 RP
Mar 13, 2026 13:27olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:23richardbranway1richardbranway1CPPg++13.3.0Success231,488
Mar 13, 2026 13:16olliecrowolliecrowCPPg++13.3.0Error