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 listSep 27, 2025 19:04Wookyung JeongWookyung JeongScore: 62,950Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 19:0459,61455,939,44131,526,0003,050,0002,445,312
2Sep 27, 2025 19:0559,95359,901,61129,806,0004,967,0002,359,296
3Sep 27, 2025 19:0560,11465,036,24928,889,0005,977,0002,310,144
4Sep 27, 2025 19:0461,35061,567,14232,533,0003,050,0002,433,024
5Sep 27, 2025 19:0561,52162,555,18332,624,0003,058,0002,367,488
6Sep 27, 2025 19:0461,82864,180,46826,895,0008,965,0002,437,120
7Sep 27, 2025 19:0562,76065,757,62132,357,0004,044,0002,453,504
8Sep 27, 2025 19:0562,95066,791,81629,412,0007,099,0002,482,176
9Sep 27, 2025 19:0563,49563,869,56631,851,0004,976,0002,416,640
10Sep 27, 2025 19:0463,52463,296,17133,857,0002,987,0002,449,408
11Sep 27, 2025 19:0563,62962,724,67129,923,0006,982,0002,490,368
12Sep 27, 2025 19:0564,13158,601,74630,159,0007,037,0002,281,472
13Sep 27, 2025 19:0564,25964,056,11733,241,0004,029,0002,461,696
14Sep 27, 2025 19:0464,49159,812,49030,329,0007,076,0002,506,752
15Sep 27, 2025 19:0464,58858,584,81830,374,0007,087,0002,449,408