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 listJul 18, 2025 15:01Alexey IlyukhovAlexey IlyukhovScore: 77,395Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 15, 2025 19:5274,31267,847,43141,097,0002,004,0002,326,528
2Jul 18, 2025 13:4575,03864,891,74341,498,0002,024,0002,412,544
3Jul 18, 2025 15:0176,02169,448,54242,088,0002,004,0002,277,376
4Jul 15, 2025 19:5276,55566,119,00544,402,00002,473,984
5Jul 15, 2025 19:5476,66473,862,12542,444,0002,021,0002,433,024
6Jul 18, 2025 13:4576,85367,000,95143,562,0001,013,0002,482,176
7Jul 15, 2025 19:5476,94872,745,65044,630,00002,490,368
8Jul 15, 2025 19:5277,02169,625,71542,687,0001,985,0002,461,696
9Jul 15, 2025 19:5277,06271,930,22043,681,0001,015,0002,347,008
10Jul 15, 2025 19:5277,39569,424,63643,892,000997,0002,293,760
11Jul 18, 2025 15:0177,40770,873,92342,901,0001,995,0002,445,312
12Jul 15, 2025 19:5277,41775,111,29943,905,000997,0002,363,392
13Jul 15, 2025 19:5277,52266,952,01842,965,0001,998,0002,478,080
14Jul 18, 2025 13:4577,53168,149,67842,970,0001,998,0002,490,368
15Jul 15, 2025 19:5477,54869,195,42743,979,000999,0002,441,216
16Jul 15, 2025 19:5277,74767,810,20443,089,0002,004,0002,424,832
17Jul 18, 2025 15:0177,83872,581,87743,140,0002,006,0002,375,680
18Jul 15, 2025 19:5279,80377,598,48443,268,0003,018,0002,269,184