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 16:01Wookyung JeongWookyung JeongScore: 75,636Success
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 15:5770,98864,037,08534,144,0007,029,0002,367,488
2Sep 27, 2025 15:5771,38365,707,59533,324,0008,078,0002,277,376
3Sep 27, 2025 15:5772,79764,433,53637,196,0005,026,0002,469,888
4Sep 27, 2025 15:5773,42266,189,42138,530,0004,055,0002,408,448
5Sep 27, 2025 16:0073,47264,958,07336,527,0006,087,0002,330,624
6Sep 27, 2025 15:5773,83168,665,75936,847,0005,975,0002,367,488
7Sep 27, 2025 16:0173,91067,693,19134,893,0007,975,0002,310,144
8Sep 27, 2025 15:5774,09870,060,55232,983,0009,994,0002,519,040
9Sep 27, 2025 16:0174,65075,054,29834,235,0009,062,0002,269,184
10Sep 27, 2025 15:5774,78171,144,68934,295,0009,078,0002,412,544
11Sep 27, 2025 15:5775,13464,547,90838,511,0005,067,0002,289,664
12Sep 27, 2025 16:0075,35370,022,98139,640,0004,065,0002,400,256
13Sep 27, 2025 16:0175,63666,845,03632,902,00010,967,0002,387,968
14Sep 27, 2025 16:0075,78665,887,06535,964,0007,992,0002,523,136
15Sep 27, 2025 16:0177,63468,240,56239,025,0006,003,0002,416,640
16Sep 27, 2025 16:0178,44566,377,51737,410,0008,088,0002,285,568
17Sep 27, 2025 15:5780,14367,256,05140,420,0006,063,0002,465,792
18Sep 27, 2025 16:0180,37980,023,53039,526,0007,094,0002,519,040
19Sep 27, 2025 16:0180,45374,882,03441,591,0005,072,0002,387,968
20Sep 27, 2025 15:5781,14076,314,41840,052,0007,009,0002,392,064
21Sep 27, 2025 15:5781,81470,310,26237,356,00010,096,0002,371,584
22Sep 27, 2025 15:5783,58670,671,24641,410,0007,070,0002,490,368
23Sep 27, 2025 16:0183,84381,490,74943,564,0005,065,0002,461,696
24Sep 27, 2025 16:0183,98174,781,23641,606,0007,103,0002,408,448