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 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.1Success184,350+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
Feb 8, 2025 20:43alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:42alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:40alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:38alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:33alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:32alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 20:31alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:34alxmrcktralxmrcktrRUSTrust-1.84.0Success114,978
Feb 8, 2025 18:28alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:26alxmrcktralxmrcktrRUSTrust-1.84.0Success113,021
Feb 8, 2025 18:21alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:20alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:19alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:17alxmrcktralxmrcktrRUSTrust-1.84.0Success93,162
Feb 8, 2025 18:14alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:13alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:12alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:10alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:08alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:07alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:05alxmrcktralxmrcktrRUSTrust-1.84.0Error
Feb 8, 2025 18:04Code HintsCode HintsRUSTrust-1.84.0Success92,979
Feb 8, 2025 18:01alxmrcktralxmrcktrRUSTrust-1.84.0Success93,157