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 29, 2025 19:55Wookyung JeongWookyung JeongScore: 54,955Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 29, 2025 19:5251,50756,778,93926,887,0002,987,0002,289,664
2Sep 29, 2025 20:0051,51056,795,47925,893,0003,983,0002,306,048
3Sep 29, 2025 19:5951,69852,073,04027,986,0001,999,0002,322,432
4Sep 29, 2025 20:0051,94356,844,77228,119,0002,008,0002,449,408
5Sep 29, 2025 19:5952,13157,279,45628,221,0002,015,0002,486,272
6Sep 29, 2025 20:0052,30352,930,05028,314,0002,022,0002,519,040
7Sep 29, 2025 20:0052,40553,803,65328,369,0002,026,0002,404,352
8Sep 29, 2025 19:5252,55351,535,41228,449,0002,032,0002,334,720
9Sep 29, 2025 19:5952,65952,642,07928,506,0002,036,0002,318,336
10Sep 29, 2025 19:5952,95556,782,33929,691,0001,023,0002,400,256
11Sep 29, 2025 20:0053,26057,784,24827,902,0002,989,0002,457,600
12Sep 29, 2025 19:5253,43454,873,90327,993,0002,999,0002,289,664
13Sep 29, 2025 19:5253,49859,048,35729,028,0002,001,0002,473,984
14Sep 29, 2025 20:0053,64160,068,01129,105,0002,007,0002,449,408
15Sep 29, 2025 19:5953,70355,567,85529,139,0002,009,0002,465,792
16Sep 29, 2025 19:5953,92152,329,15130,266,0001,008,0002,502,656
17Sep 29, 2025 20:0053,99856,904,13629,299,0002,020,0002,322,432
18Sep 29, 2025 19:5354,38858,252,73828,493,0003,052,0002,461,696
19Sep 29, 2025 19:5954,95556,513,22526,894,0004,980,0002,285,568
20Sep 29, 2025 19:5354,95760,663,51129,883,0001,992,0002,482,176
21Sep 29, 2025 19:5254,99062,686,23029,901,0001,993,0002,424,832
22Sep 29, 2025 19:5555,29058,777,54628,060,0004,008,0002,502,656
23Sep 29, 2025 20:0055,43858,360,14030,145,0002,009,0002,326,528
24Sep 29, 2025 19:5255,62452,598,28929,238,0003,024,0002,359,296
25Sep 29, 2025 19:5955,82456,563,64628,331,0004,047,0002,433,024
26Sep 29, 2025 19:5255,95755,245,27030,427,0002,028,0002,428,928
27Sep 29, 2025 20:0056,30961,985,27529,598,0003,061,0002,416,640
28Sep 29, 2025 19:5956,34164,605,07928,594,0004,084,0002,437,120
29Sep 29, 2025 19:5356,36658,266,42230,649,0002,043,0002,506,752
30Sep 29, 2025 19:5956,67258,465,34829,882,0002,988,0002,404,352
31Sep 29, 2025 19:5556,72157,989,18829,908,0002,990,0002,400,256
32Sep 29, 2025 19:5256,95557,518,47230,031,0003,003,0002,490,368
33Sep 29, 2025 19:5958,16959,442,70330,671,0003,067,0002,461,696
34Sep 29, 2025 19:5258,41659,902,28232,885,000996,0002,490,368
35Sep 29, 2025 19:5958,77157,196,23130,077,0004,010,0002,375,680
36Sep 29, 2025 19:5559,73658,457,57432,609,0002,038,0002,428,928