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 9, 2025 03:40Fernando Clark del ValleFernando Clark del ValleError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:02Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:04051,581,22930,771,00010,716,00024,576No answer to stdout. Died or killed?stderr
2Sep 9, 2025 03:40124,16299,409,26463,013,0009,001,0002,498,560
3Sep 9, 2025 03:40124,52897,674,06064,201,0008,025,0002,433,024
4Sep 9, 2025 03:40125,13395,886,93266,529,0006,048,0002,314,240
5Sep 9, 2025 03:40125,981102,796,70267,064,0006,005,0002,367,488
6Sep 9, 2025 03:40127,05997,787,03465,618,0008,076,0002,347,008
7Sep 9, 2025 03:40133,597103,072,48169,436,0008,050,0002,465,792
8Sep 9, 2025 03:40134,248102,206,93971,875,0005,989,0002,433,024
9Sep 9, 2025 03:40139,666110,213,94271,006,00010,000,0002,379,776
10Sep 9, 2025 03:40163,698121,283,01385,951,0008,994,0002,363,392