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 27, 2025 14:46Wookyung JeongWookyung JeongScore: 87,191Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 14:4677,37877,438,66339,893,0004,986,0002,510,848
2Sep 27, 2025 14:4682,73370,108,41244,986,0002,999,0002,506,752
3Sep 27, 2025 14:4583,36982,526,69440,295,0008,059,0002,318,336
4Sep 27, 2025 14:4684,02479,627,56745,689,0003,045,0002,494,464
5Sep 27, 2025 14:4585,46972,792,32142,491,0007,081,0002,273,280
6Sep 27, 2025 14:4685,47274,735,50644,516,0005,058,0002,506,752
7Sep 27, 2025 14:4685,71277,398,69145,655,0004,058,0002,314,240
8Sep 27, 2025 14:4985,99773,643,77541,898,0007,980,0002,424,832
9Sep 27, 2025 14:4987,09174,788,67945,462,0005,051,0002,269,184
10Sep 27, 2025 14:4687,19175,528,29144,503,0006,068,0002,412,544
11Sep 27, 2025 14:4988,18177,386,41842,120,0009,025,0002,433,024
12Sep 27, 2025 14:4689,94575,017,99546,149,0006,019,0002,342,912
13Sep 27, 2025 14:4690,30581,155,88945,327,0007,050,0002,375,680
14Sep 27, 2025 14:4591,44376,182,17249,035,0004,002,0002,387,968
15Sep 27, 2025 14:4692,16679,818,21146,396,0007,060,0002,351,104
16Sep 27, 2025 14:4692,26077,784,36945,434,0008,077,0002,404,352
17Sep 27, 2025 14:4693,40977,710,10449,161,0005,016,0002,396,160
18Sep 27, 2025 14:4695,80779,855,69651,527,0004,041,0002,322,432