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 17:25Wookyung JeongWookyung JeongScore: 70,828Success
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 17:2567,28861,985,42433,023,0006,004,0002,523,136
2Sep 27, 2025 17:2469,11063,942,37237,078,0003,006,0002,314,240
3Sep 27, 2025 17:2469,60364,448,49235,324,0005,046,0002,510,848
4Sep 27, 2025 17:2470,18168,959,02138,670,0002,035,0002,265,088
5Sep 27, 2025 17:2470,48469,841,34733,902,0006,979,0002,457,600
6Sep 27, 2025 17:2470,50065,712,84633,909,0006,981,0002,519,040
7Sep 27, 2025 17:2470,51966,752,69834,916,0005,985,0002,342,912
8Sep 27, 2025 17:2570,82864,215,04435,069,0006,011,0002,301,952
9Sep 27, 2025 17:2571,18664,201,35936,253,0005,035,0002,338,816
10Sep 27, 2025 17:2471,92868,244,00032,561,0009,157,0002,277,376
11Sep 27, 2025 17:2572,54564,566,57438,069,0004,007,0002,494,464
12Sep 27, 2025 17:2472,94865,540,69937,273,0005,037,0002,465,792
13Sep 27, 2025 17:2472,99064,358,03936,287,0006,047,0002,383,872
14Sep 27, 2025 17:2573,11667,056,98537,359,0005,048,0002,306,048
15Sep 27, 2025 17:2573,93871,656,26231,914,00010,970,0002,412,544