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 listSep 29, 2025 18:35Wookyung JeongWookyung JeongScore: 59,731Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 29, 2025 18:3656,05755,146,63228,449,0004,064,0002,416,640
2Sep 29, 2025 18:3656,68156,484,76828,891,0003,984,0002,371,584
3Sep 29, 2025 18:3657,54060,136,22229,328,0004,045,0002,269,184
4Sep 29, 2025 18:3657,62658,797,83129,372,0004,051,0002,351,104
5Sep 29, 2025 18:3557,82956,820,70329,476,0004,065,0002,293,760
6Sep 29, 2025 18:3558,71260,703,96628,044,0006,009,0002,314,240
7Sep 29, 2025 18:3659,06461,107,29230,227,0004,030,0002,277,376
8Sep 29, 2025 18:3659,73164,521,19128,531,0006,113,0002,420,736
9Sep 29, 2025 18:3659,77164,631,66528,550,0006,117,0002,326,528
10Sep 29, 2025 18:3560,18862,836,80829,922,0004,987,0002,285,568
11Sep 29, 2025 18:3660,34856,733,05530,002,0005,000,0002,277,376
12Sep 29, 2025 18:3560,38157,419,92432,020,0003,001,0002,465,792
13Sep 29, 2025 18:3560,53157,953,55230,093,0005,015,0002,265,088
14Sep 29, 2025 18:3660,54158,580,54832,105,0003,009,0002,330,624
15Sep 29, 2025 18:3561,25562,500,63629,438,0006,090,0002,306,048