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 listDec 19, 2025 14:52HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:26Andrew Epstein10.00CustomSolution Broken
Aug 31, 2026 15:42Tomislav Tunkovic10.00RecheckSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:3509,574,1371,103,000020,480No answer to stdout. Died or killed?
2Dec 19, 2025 14:5234,61742,984,95219,075,0001,003,0002,527,232
3Aug 31, 2026 15:4435,15545,378,18419,359,0001,031,0002,117,632
4Dec 19, 2025 14:5236,66741,893,83020,255,0001,012,0002,269,184
5Dec 19, 2025 14:5236,92947,378,88921,419,00002,539,520
6Aug 31, 2026 15:4437,63430,227,29720,756,0001,072,0002,117,632
7Dec 19, 2025 14:5237,76942,853,52319,915,0001,991,0002,527,232
8Dec 19, 2025 14:5238,27844,706,75920,183,0002,018,0002,539,520
9Dec 19, 2025 14:5238,36640,570,38521,241,0001,011,0002,527,232
10Aug 31, 2026 15:4438,53130,693,38221,261,0001,087,0002,117,632
11Dec 19, 2025 14:5238,96748,880,42222,601,00002,527,232
12Dec 19, 2025 14:5240,46249,762,44122,448,0001,020,0002,269,184
13Dec 19, 2025 14:5241,52947,654,81423,084,0001,003,0002,281,472