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 30, 2025 16:40Wookyung JeongWookyung JeongScore: 51,541Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 30, 2025 16:4148,20352,689,01524,963,0002,995,0002,359,296
2Sep 30, 2025 16:4148,22155,841,34125,971,0001,997,0002,449,408
3Sep 30, 2025 16:4148,43654,326,96726,087,0002,006,0002,375,680
4Sep 30, 2025 16:4048,61749,476,55925,177,0003,021,0002,375,680
5Sep 30, 2025 16:3948,63355,233,01924,178,0004,029,0002,490,368
6Sep 30, 2025 16:4149,05055,202,05526,417,0002,032,0002,301,952
7Sep 30, 2025 16:4149,77657,285,12824,888,0003,982,0002,502,656
8Sep 30, 2025 16:3949,78357,320,89725,887,0002,987,0002,379,776
9Sep 30, 2025 16:3949,86951,578,53726,930,0001,994,0002,519,040
10Sep 30, 2025 16:4149,90955,424,88625,953,0002,994,0002,387,968
11Sep 30, 2025 16:3949,98351,684,02926,991,0001,999,0002,301,952
12Dec 18, 2025 05:0650,03357,114,05425,017,0004,002,0002,408,448
13Sep 30, 2025 16:4150,03454,249,89526,018,0003,002,0002,310,144
14Sep 30, 2025 16:3950,12954,292,63425,065,0004,010,0002,355,200
15Sep 30, 2025 16:4150,59154,739,90925,296,0004,047,0002,510,848
16Sep 30, 2025 16:4150,60252,169,23826,313,0003,036,0002,326,528
17Sep 30, 2025 16:4150,64559,033,42026,336,0003,038,0002,330,624
18Sep 30, 2025 16:3951,01953,298,17627,551,0002,040,0002,502,656
19Sep 30, 2025 16:4051,09359,805,85427,591,0002,043,0002,478,080
20Sep 30, 2025 16:4051,54159,990,88626,905,0002,989,0002,285,568
21Sep 30, 2025 16:4051,55260,987,16627,907,0001,993,0002,510,848
22Sep 30, 2025 16:4151,55555,099,25827,909,0001,993,0002,420,736
23Sep 30, 2025 16:4151,65954,108,44826,966,0002,996,0002,351,104
24Sep 30, 2025 16:4151,71655,277,02826,996,0002,999,0002,404,352
25Dec 18, 2025 05:0651,79552,797,95227,037,0003,004,0002,494,464
26Sep 30, 2025 16:3951,81954,535,06028,052,0002,003,0002,482,176
27Sep 30, 2025 16:4151,88451,987,74327,084,0003,009,0002,314,240
28Sep 30, 2025 16:4051,98657,004,35625,127,0005,025,0002,355,200
29Sep 30, 2025 16:4152,00051,751,50326,139,0004,021,0002,494,464
30Sep 30, 2025 16:4052,01452,068,72027,152,0003,016,0002,465,792
31Sep 30, 2025 16:4052,11251,412,72728,210,0002,015,0002,457,600
32Sep 30, 2025 16:4152,31750,810,39327,310,0003,034,0002,314,240
33Dec 18, 2025 05:0652,35060,529,61326,315,0004,048,0002,502,656
34Sep 30, 2025 16:3952,49552,644,67328,418,0002,029,0002,392,064
35Sep 30, 2025 16:4152,51653,467,34726,398,0004,061,0002,265,088
36Sep 30, 2025 16:4052,56054,203,25428,453,0002,032,0002,306,048
37Sep 30, 2025 16:3953,63857,792,14829,103,0002,007,0002,506,752
38Sep 30, 2025 16:4053,92655,731,73129,260,0002,017,0002,453,504
39Sep 30, 2025 16:4154,57456,727,57729,611,0002,042,0002,404,352