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 19:38Wookyung JeongWookyung JeongScore: 58,540Success
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 19:3956,98855,499,24029,047,0004,006,0002,519,040
2Sep 29, 2025 19:3957,91454,116,05928,501,0005,089,0002,502,656
3Sep 29, 2025 19:3858,01662,194,55829,571,0004,078,0002,490,368
4Sep 29, 2025 19:3858,06965,129,08330,619,0003,061,0002,355,200
5Sep 29, 2025 19:3858,14562,086,39526,571,0007,153,0002,404,352
6Sep 29, 2025 19:3858,44860,665,50926,921,0006,979,0002,396,160
7Sep 29, 2025 19:3858,54061,073,61330,958,0002,995,0002,342,912
8Sep 29, 2025 19:3859,15362,595,34329,264,0005,045,0002,433,024
9Sep 29, 2025 19:3959,66254,833,64731,551,0003,053,0002,445,312
10Sep 29, 2025 19:3860,72659,592,01829,183,0006,038,0002,437,120
11Sep 29, 2025 19:3861,02257,492,87330,337,0005,056,0002,359,296
12Sep 29, 2025 19:3861,84368,233,18032,880,0002,989,0002,379,776