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.

Back to listDec 21, 2025 03:50Josu San MartinJosu San MartinScore: 38,457Success
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 03:5136,98354,288,23520,429,0001,021,0002,281,472
2Dec 21, 2025 03:4937,75050,682,81320,900,000995,0002,486,272
3Dec 21, 2025 03:5237,76452,994,07221,903,00002,330,624
4Dec 21, 2025 03:5137,77942,845,29619,920,0001,992,0002,408,448
5Dec 21, 2025 03:4938,31243,247,18820,201,0002,020,0002,482,176
6Dec 21, 2025 03:5038,32244,937,22221,217,0001,010,0002,428,928
7Dec 21, 2025 03:4938,43144,969,24020,264,0002,026,0002,433,024
8Dec 21, 2025 03:5238,45748,942,17121,292,0001,013,0002,473,984
9Dec 21, 2025 03:5138,98358,589,82521,583,0001,027,0002,502,656
10Dec 21, 2025 03:4939,48349,237,60921,905,000995,0002,338,816
11Dec 21, 2025 03:4939,50246,151,39320,919,0001,992,0002,478,080
12Dec 21, 2025 03:5039,57243,469,64121,955,000997,0002,392,064
13Dec 21, 2025 03:4939,60250,521,86220,972,0001,997,0002,453,504
14Dec 21, 2025 03:5239,82147,586,41721,088,0002,008,0002,375,680
15Dec 21, 2025 03:5040,18148,311,97921,279,0002,026,0002,457,600