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 7, 2025 21:16Daniel LubarovDaniel LubarovScore: 72,434Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 7, 2025 21:1664,00764,481,15336,121,0001,003,0002,375,680
2Sep 7, 2025 21:2564,30360,674,73135,280,0002,016,0002,363,392
3Sep 7, 2025 21:1665,31670,452,50236,887,000996,0002,510,848
4Sep 7, 2025 21:2568,34568,472,94339,640,00002,347,008
5Sep 7, 2025 21:1668,99168,084,80838,015,0002,000,0002,510,848
6Sep 7, 2025 21:1669,90962,994,02140,547,00002,514,944
7Sep 7, 2025 21:1672,43469,494,93241,012,0001,000,0002,510,848
8Sep 7, 2025 21:1675,08364,945,84443,548,00002,342,912
9Sep 7, 2025 21:1675,94869,706,21642,048,0002,002,0002,437,120
10Sep 7, 2025 21:2578,23867,608,75145,378,00002,502,656
11Sep 7, 2025 21:1678,31471,624,65444,413,0001,009,0002,355,200
12Sep 7, 2025 21:1686,60975,110,05548,224,0002,009,0002,490,368