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 listFeb 28, 2026 00:490xZohan0xZohanScore: 69,176Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 28, 2026 00:4968,35075,320,75138,627,0001,016,0002,289,664
2Feb 28, 2026 00:4769,07468,464,20339,062,0001,001,0002,342,912
3Feb 28, 2026 00:4769,07462,841,40239,062,0001,001,0002,347,008
4Feb 28, 2026 00:4969,07463,117,60938,060,0002,003,0002,289,664
5Mar 1, 2026 15:4569,10565,516,36238,077,0002,004,0002,281,472
6Feb 28, 2026 00:4769,12963,095,65539,093,0001,002,0002,351,104
7Feb 28, 2026 00:4969,17465,755,77838,115,0002,006,0002,289,664
8Feb 28, 2026 00:4969,17662,020,64038,116,0002,006,0002,281,472
9Mar 1, 2026 15:4569,27264,775,67938,170,0002,008,0002,289,664
10Feb 28, 2026 00:4969,34362,882,69139,214,0001,005,0002,347,008
11Feb 28, 2026 00:4969,40363,866,47638,242,0002,012,0002,289,664
12Feb 28, 2026 00:4969,40967,220,82838,245,0002,012,0002,338,816
13Mar 1, 2026 15:4569,47965,281,02639,291,0001,007,0002,342,912
14Feb 28, 2026 00:4969,52967,098,77538,311,0002,016,0002,355,200
15Feb 28, 2026 00:4971,08664,861,17739,219,0002,011,0002,289,664