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 24, 2025 00:44HattonuriHattonuriScore: 30,295Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 24, 2025 00:4329,36637,682,42316,031,0001,001,0002,486,272
2Dec 24, 2025 00:4429,74044,665,82117,249,00002,314,240
3Dec 24, 2025 00:4429,93330,846,66216,340,0001,021,0002,523,136
4Dec 24, 2025 00:4730,07243,480,83616,416,0001,026,0002,326,528
5Dec 24, 2025 00:4730,12444,346,49717,472,00002,326,528
6Dec 24, 2025 00:4430,27239,806,26316,526,0001,032,0002,486,272
7Dec 24, 2025 00:4430,27444,821,86116,527,0001,032,0002,338,816
8Dec 24, 2025 00:4430,29538,655,99716,538,0001,033,0002,461,696
9Dec 24, 2025 00:4430,40543,172,18915,676,0001,959,0002,502,656
10Dec 24, 2025 00:4730,54337,766,52117,715,00002,265,088
11Dec 24, 2025 00:4330,84542,440,28816,897,000993,0002,457,600
12Dec 24, 2025 00:4431,41043,931,94616,194,0002,024,0002,396,160
13Dec 24, 2025 00:4432,03642,732,05617,549,0001,032,0002,506,752
14Dec 24, 2025 00:4332,58849,265,75516,912,0001,989,0002,445,312
15Dec 24, 2025 00:4433,03843,649,39117,145,0002,017,0002,424,832