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
Feb 10, 2025 21:32Bhavik-MakwanaBhavik-MakwanaRUSTrust-1.83.0Error
Feb 10, 2025 11:58Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success182,631
Feb 9, 2025 21:40Bhavik-MakwanaBhavik-MakwanaRUSTrust-1.83.0Success878,128
Feb 9, 2025 21:38Bhavik-MakwanaBhavik-MakwanaRUSTrust-1.83.0Success1,287,690
Feb 9, 2025 21:33Bhavik-MakwanaBhavik-MakwanaRUSTrust-1.83.0Success1,292,122
Feb 9, 2025 21:26Bhavik-MakwanaBhavik-MakwanaRUSTrust-1.83.0Success1,323,529
Feb 9, 2025 21:01Bhavik-MakwanaBhavik-MakwanaRUSTrust-1.83.0Success969,798
Feb 9, 2025 20:54Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success114,907
Feb 9, 2025 20:53Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success111,597
Feb 9, 2025 20:53Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success111,169+3.76 RP
Feb 9, 2025 20:51Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success122,428
Feb 9, 2025 20:43Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success121,598
Feb 9, 2025 20:31Daniel HarrisDaniel HarrisRUSTrust-1.84.1Error
Feb 9, 2025 20:30Daniel HarrisDaniel HarrisRUSTrust-1.84.1Error
Feb 9, 2025 20:30Daniel HarrisDaniel HarrisRUSTrust-1.84.1Error
Feb 9, 2025 20:28Daniel HarrisDaniel HarrisRUSTrust-1.84.1Error
Feb 9, 2025 20:22Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success126,016
Feb 9, 2025 20:17Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success208,283
Feb 9, 2025 19:26Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success133,003
Feb 9, 2025 18:59Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success134,443
Feb 9, 2025 18:41Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success130,540
Feb 9, 2025 18:37Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success185,469
Feb 9, 2025 17:58Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success128,028
Feb 9, 2025 17:57Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success133,950
Feb 9, 2025 17:49Daniel HarrisDaniel HarrisRUSTrust-1.84.1Success911,029
Feb 8, 2025 23:45undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:35undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:29undenuicapundenuicapCPPclang++18.1.3Error
Feb 8, 2025 23:26undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:21undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:21undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:19undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:15undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:12undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:11undenuicapundenuicapRUSTrust-1.84.0Success74,681+6.57 RP
Feb 8, 2025 23:11undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:09undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:06undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:04undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 23:00undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 23:00undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:57undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:57undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:56undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:55undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:55undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:54undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:53undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:52undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:51undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:50undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:49undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:48undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:48undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:47undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:46undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:46undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:45undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:44undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:44undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:43undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:43undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:42undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:42undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:41undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:40undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:37undenuicapundenuicapRUSTrust-1.84.0Success78,536+19.68 RP
Feb 8, 2025 22:34undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:32undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:31undenuicapundenuicapRUSTrust-1.84.0Success92,891+10.38 RP
Feb 8, 2025 22:29undenuicapundenuicapRUSTrust-1.84.0Error
Feb 8, 2025 22:23undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:20undenuicapundenuicapRUSTrust-1.84.1Error
Feb 8, 2025 22:20undenuicapundenuicapRUSTrust-1.84.1Success102,807+24.40 RP
Feb 8, 2025 22:18undenuicapundenuicapRUSTrust-1.84.1Success137,226+18.63 RP
Feb 8, 2025 22:18undenuicapundenuicapRUSTrust-1.84.1Error+54.24 RP
Feb 8, 2025 21:59alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:51alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:50alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:48alxmrcktralxmrcktrRUSTrust-1.84.0Success115,305
Feb 8, 2025 21:48alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:46alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:45alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:43alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:41alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:38alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:38alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:37alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:13alxmrcktralxmrcktrRUSTrust-1.84.0Success97,390
Feb 8, 2025 21:11alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:10alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 21:09alxmrcktralxmrcktrRUSTrust-1.84.0Success114,843
Feb 8, 2025 21:07alxmrcktralxmrcktrRUSTrust-1.84.0Success140,619
Feb 8, 2025 20:59alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:56alxmrcktralxmrcktrRUSTrust-1.84.0Success141,167
Feb 8, 2025 20:55alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:54alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:53alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:52alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:51alxmrcktralxmrcktrRUSTrust-1.84.0Error