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 listDec 21, 2025 03:52Josu San MartinJosu San MartinScore: 37,793Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 03:3935,57243,749,34520,632,00002,510,848
2Dec 21, 2025 03:3936,05352,210,39819,916,000995,0002,404,352
3Dec 21, 2025 03:5136,14746,216,18619,967,000998,0002,486,272
4Dec 21, 2025 03:4036,23447,602,59620,016,0001,000,0002,494,464
5Dec 21, 2025 03:5136,30541,235,59219,052,0002,005,0002,416,640
6Dec 21, 2025 03:3936,50243,135,67419,155,0002,016,0002,482,176
7Dec 21, 2025 03:3936,54544,813,52720,187,0001,009,0002,510,848
8Dec 21, 2025 03:4036,60547,926,45220,220,0001,011,0002,416,640
9Dec 21, 2025 03:4036,61946,744,65121,239,00002,412,544
10Dec 21, 2025 03:5136,66943,413,25920,256,0001,012,0002,408,448
11Dec 21, 2025 03:4936,82849,043,38720,343,0001,017,0002,301,952
12Dec 21, 2025 03:4937,14350,368,04720,518,0001,025,0002,404,352
13Dec 21, 2025 03:5137,36748,904,19720,641,0001,032,0002,416,640
14Dec 21, 2025 03:5137,37855,687,47220,647,0001,032,0002,494,464
15Dec 21, 2025 03:3937,39547,889,60921,689,00002,502,656
16Dec 21, 2025 03:3937,43447,863,98020,679,0001,033,0002,314,240
17Dec 21, 2025 03:4937,76047,357,13220,906,000995,0002,351,104
18Dec 21, 2025 03:3937,77148,410,51919,916,0001,991,0002,490,368
19Dec 21, 2025 03:3937,79344,186,75419,928,0001,992,0002,445,312
20Dec 21, 2025 03:3937,86044,714,52619,963,0001,996,0002,465,792
21Dec 21, 2025 03:4038,01446,226,27820,044,0002,004,0002,396,160
22Dec 21, 2025 03:5238,07247,316,65220,075,0002,007,0002,473,984
23Dec 21, 2025 03:4938,09045,111,31221,088,0001,004,0002,428,928
24Dec 21, 2025 03:5238,15048,221,05021,122,0001,005,0002,408,448
25Dec 21, 2025 03:4038,20945,438,78721,154,0001,007,0002,342,912
26Dec 21, 2025 03:4938,25045,554,71220,169,0002,016,0002,482,176
27Dec 21, 2025 03:5138,26248,582,10220,175,0002,017,0002,412,544
28Dec 21, 2025 03:5238,32246,305,20620,207,0002,020,0002,355,200
29Dec 21, 2025 03:3938,37147,917,30020,232,0002,023,0002,359,296
30Dec 21, 2025 03:5238,40747,153,69220,251,0002,025,0002,400,256
31Dec 21, 2025 03:4938,49043,816,05120,295,0002,029,0002,379,776
32Dec 21, 2025 03:4038,50750,681,30521,319,0001,015,0002,498,560
33Dec 21, 2025 03:3938,52248,836,91321,328,0001,015,0002,445,312
34Dec 21, 2025 03:5238,65245,547,20421,399,0001,019,0002,428,928
35Dec 21, 2025 03:3938,72847,477,70321,441,0001,021,0002,387,968
36Dec 21, 2025 03:5238,78851,813,76820,452,0002,045,0002,383,872