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 04:07Josu San MartinJosu San MartinScore: 37,250Success
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 04:0735,47845,237,71519,549,0001,028,0002,523,136
2Dec 21, 2025 04:0735,56747,805,36119,598,0001,031,0002,506,752
3Dec 21, 2025 04:0736,36039,546,63819,081,0002,008,0002,506,752
4Dec 21, 2025 04:1036,41942,617,50020,118,0001,005,0002,523,136
5Dec 21, 2025 04:0736,55048,899,20619,180,0002,019,0002,498,560
6Dec 21, 2025 04:0536,90046,851,54920,383,0001,019,0002,502,656
7Dec 21, 2025 04:1036,98448,350,85320,430,0001,021,0002,412,544
8Dec 21, 2025 04:0737,12143,830,96020,505,0001,025,0002,260,992
9Dec 21, 2025 04:0537,18846,982,38421,569,00002,269,184
10Dec 21, 2025 04:0737,25047,486,16820,577,0001,028,0002,506,752
11Dec 21, 2025 04:0737,29742,981,69820,602,0001,030,0002,408,448
12Dec 21, 2025 04:0737,40349,378,49121,694,00002,293,760
13Dec 21, 2025 04:0737,46747,829,51221,731,00002,510,848
14Dec 21, 2025 04:0737,72148,165,77321,878,00002,265,088
15Dec 21, 2025 04:0737,76048,229,40321,901,00002,269,184
16Dec 21, 2025 04:1037,84050,702,31220,950,000997,0002,269,184
17Dec 21, 2025 04:0537,90041,205,59019,984,0001,998,0002,486,272
18Dec 21, 2025 04:0737,98843,233,98621,032,0001,001,0002,519,040