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 17:54Wookyung JeongWookyung JeongScore: 63,150Success
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 17:5659,11960,478,67831,264,0003,025,0002,453,504
2Sep 27, 2025 17:5559,87459,350,27030,642,0004,085,0002,498,560
3Sep 27, 2025 17:5360,24755,643,72926,956,0007,987,0002,322,432
4Sep 27, 2025 17:5360,26956,074,94426,966,0007,990,0002,338,816
5Sep 27, 2025 17:5660,55958,777,59729,103,0006,021,0002,502,656
6Sep 27, 2025 17:5660,60756,598,82930,131,0005,021,0002,473,984
7Sep 27, 2025 17:5361,14056,965,91828,369,0007,092,0002,289,664
8Sep 27, 2025 17:5461,25257,918,80227,406,0008,120,0002,338,816
9Sep 27, 2025 17:5461,50067,207,34432,613,0003,057,0002,457,600
10Sep 27, 2025 17:5661,52465,964,79626,508,0009,176,0002,486,272
11Sep 27, 2025 17:5461,79361,909,83931,858,0003,982,0002,367,488
12Sep 27, 2025 17:5561,90363,843,15729,920,0005,984,0002,293,760
13Sep 27, 2025 17:5361,95560,101,86329,945,0005,989,0002,465,792
14Sep 27, 2025 17:5362,94156,395,23330,422,0006,084,0002,400,256
15Sep 27, 2025 17:5363,02664,162,66832,494,0004,061,0002,285,568
16Sep 27, 2025 17:5663,15062,035,44033,575,0003,052,0002,465,792
17Sep 27, 2025 17:5563,57261,472,69531,890,0004,982,0002,322,432
18Sep 27, 2025 17:5563,61063,392,65429,914,0006,980,0002,400,256
19Sep 27, 2025 17:5663,75959,918,72429,984,0006,996,0002,404,352
20Sep 27, 2025 17:5563,86658,991,21429,033,0008,009,0002,387,968
21Sep 27, 2025 17:5363,90261,604,82230,051,0007,012,0002,453,504
22Sep 27, 2025 17:5563,94363,413,36131,073,0006,014,0002,441,216
23Sep 27, 2025 17:5664,32464,608,41631,258,0006,050,0002,273,280
24Sep 27, 2025 17:5364,41659,866,89735,342,0002,019,0002,510,848
25Sep 27, 2025 17:5364,51962,250,98333,376,0004,045,0002,355,200
26Sep 27, 2025 17:5464,54864,378,30727,320,00010,118,0002,412,544
27Sep 27, 2025 17:5664,58470,121,13834,422,0003,037,0002,318,336
28Sep 27, 2025 17:5465,01970,081,59434,654,0003,057,0002,289,664
29Sep 27, 2025 17:5665,47158,032,00031,978,0005,995,0002,428,928
30Sep 27, 2025 17:5466,53359,423,54231,481,0007,108,0002,523,136