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 11, 2025 01:28Alexey IlyukhovAlexey IlyukhovScore: 147,586Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 13, 2025 16:42136,471101,115,35477,150,0002,003,0002,375,680
2Jul 11, 2025 01:28137,178105,981,46974,528,0005,035,0002,469,888
3Jul 13, 2025 16:42140,205110,755,62077,304,0004,015,0002,367,488
4Jul 11, 2025 01:28141,317105,477,07979,965,0001,999,0002,486,272
5Jul 13, 2025 16:42143,095108,941,81578,996,0003,999,0002,412,544
6Jul 13, 2025 16:42143,229110,160,92079,070,0004,003,0002,469,888
7Jul 11, 2025 01:28144,312107,595,02280,676,0003,025,0002,310,144
8Jul 11, 2025 01:28144,995112,614,66683,096,0001,001,0002,375,680
9Jul 13, 2025 16:42145,016106,671,64082,107,0002,002,0002,506,752
10Jul 11, 2025 01:28147,586111,480,97483,586,0002,014,0002,277,376
11Jul 13, 2025 16:42148,950111,999,96984,382,0002,009,0002,277,376
12Jul 11, 2025 01:28149,060119,480,36682,434,0004,021,0002,400,256
13Jul 13, 2025 16:42149,326110,559,09883,588,0003,021,0002,265,088
14Jul 11, 2025 01:28150,100109,538,17682,055,0005,003,0002,473,984
15Jul 11, 2025 01:28151,695111,041,81883,984,0003,999,0002,289,664
16Jul 11, 2025 01:28151,884110,992,38686,091,0002,002,0002,273,280
17Jul 13, 2025 16:42152,795114,581,41988,621,00002,363,392
18Jul 13, 2025 16:42154,957115,491,76784,882,0004,993,0002,502,656