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 listMar 14, 2026 17:15Dominique GarmierDominique GarmierScore: 76,262Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 14, 2026 16:5573,10071,119,13841,389,0001,009,0002,420,736
2Mar 21, 2026 00:3773,64071,794,64542,711,00002,424,832
3Mar 14, 2026 17:0373,71070,769,44041,735,0001,017,0002,506,752
4Mar 14, 2026 17:0373,92871,755,99640,884,0001,994,0002,506,752
5Mar 14, 2026 16:5574,14571,809,83841,004,0002,000,0002,260,992
6Mar 14, 2026 17:3074,36766,248,96242,130,0001,003,0002,416,640
7Mar 14, 2026 17:0374,54168,164,26341,224,0002,010,0002,416,640
8Mar 14, 2026 17:0374,79870,979,31742,375,0001,008,0002,502,656
9Mar 14, 2026 16:5574,94365,951,93042,457,0001,010,0002,404,352
10Mar 14, 2026 16:5074,99767,864,30942,487,0001,011,0002,506,752
11Mar 14, 2026 16:5575,06466,803,34142,525,0001,012,0002,412,544
12Mar 14, 2026 17:0675,07869,113,63443,545,00002,506,752
13Mar 14, 2026 16:5575,14866,408,65542,573,0001,013,0002,408,448
14Mar 14, 2026 17:0675,35070,448,02841,671,0002,032,0002,392,064
15Mar 14, 2026 17:0375,40269,763,27942,716,0001,017,0002,506,752
16Mar 14, 2026 16:5075,48171,252,38343,779,00002,514,944
17Mar 14, 2026 16:5575,68674,168,84141,903,0001,995,0002,379,776
18Mar 14, 2026 16:5075,78870,629,48641,959,0001,998,0002,404,352
19Mar 21, 2026 00:3775,89071,477,21642,016,0002,000,0002,502,656
20Mar 14, 2026 17:1776,06478,424,42542,112,0002,005,0002,486,272
21Mar 14, 2026 17:0376,10272,338,23342,133,0002,006,0002,396,160
22Mar 14, 2026 17:1576,19877,391,03642,187,0002,008,0002,514,944
23Mar 14, 2026 17:0276,26273,671,04542,222,0002,010,0002,519,040
24Mar 14, 2026 16:5076,36770,639,30042,280,0002,013,0002,502,656
25Mar 14, 2026 17:0376,50071,999,08942,354,0002,016,0002,486,272
26Mar 14, 2026 16:5076,64771,451,36543,445,0001,010,0002,486,272
27Mar 21, 2026 00:3776,65970,476,61643,452,0001,010,0002,396,160
28Mar 14, 2026 17:3076,78165,113,33743,521,0001,012,0002,498,560
29Mar 14, 2026 17:0276,83167,843,76142,537,0002,025,0002,506,752
30Mar 14, 2026 16:5577,23372,169,56042,805,0001,990,0002,404,352
31Mar 14, 2026 16:5077,37874,484,90143,882,000997,0002,412,544
32Mar 14, 2026 16:5577,38870,209,06942,891,0001,994,0002,457,600
33Mar 14, 2026 17:0377,40969,578,08841,904,0002,993,0002,514,944
34Mar 14, 2026 16:5577,41275,061,41242,904,0001,995,0002,502,656
35Mar 14, 2026 17:0377,44869,089,87942,924,0001,996,0002,260,992
36Mar 14, 2026 16:5077,54571,549,03343,977,000999,0002,506,752
37Mar 14, 2026 16:5077,62670,806,19543,022,0002,001,0002,506,752
38Mar 14, 2026 17:0277,76270,013,87044,100,0001,002,0002,506,752
39Mar 14, 2026 17:3077,78870,367,32443,112,0002,005,0002,383,872
40Mar 14, 2026 17:1777,96974,403,56843,213,0002,009,0002,371,584
41Mar 14, 2026 17:1778,19371,706,66244,345,0001,007,0002,404,352
42Mar 14, 2026 17:1578,25372,010,00243,370,0002,017,0002,498,560
43Mar 14, 2026 17:0678,36272,893,37343,430,0002,020,0002,506,752
44Mar 14, 2026 17:1579,11970,898,08044,892,000997,0002,375,680
45Mar 14, 2026 16:5079,29878,122,79143,994,0001,999,0002,494,464