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 listFeb 24, 2026 16:36Jem BishopJem BishopError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 24, 2021 09:0800000no run stats recorded
2Feb 24, 2026 16:323,23815,781,3931,878,00002,420,736
3Feb 24, 2026 16:333,31221,386,13201,921,0002,322,432
4Feb 24, 2026 17:013,31619,589,3271,923,00002,355,200
5Feb 24, 2026 16:343,33121,447,50201,932,0002,363,392
6Feb 24, 2026 17:013,33618,147,8671,935,00002,400,256
7Feb 24, 2026 16:333,38325,361,44701,962,0002,396,160
8Feb 24, 2026 16:323,39019,186,89101,966,0002,371,584
9Feb 24, 2026 16:373,39019,841,973983,000983,0002,347,008
10Feb 24, 2026 16:333,40315,921,111987,000987,0002,387,968
11Feb 24, 2026 17:013,41020,376,589989,000989,0002,359,296
12Feb 24, 2026 17:013,43319,004,13901,991,0002,359,296
13Feb 24, 2026 16:373,43318,786,53701,991,0002,359,296
14Feb 24, 2026 17:013,43319,428,67701,991,0002,404,352
15Feb 24, 2026 16:343,43619,118,64601,993,0002,404,352
16Feb 24, 2026 17:013,44120,713,637998,000998,0002,387,968
17Feb 24, 2026 17:013,44819,755,49202,000,0002,383,872
18Feb 24, 2026 17:013,45518,306,17902,004,0002,367,488
19Feb 24, 2026 16:323,46022,471,28902,007,0002,355,200
20Feb 24, 2026 16:343,46017,914,63602,007,0002,371,584
21Feb 24, 2026 16:343,46218,876,1141,004,0001,004,0002,379,776
22Feb 24, 2026 17:003,46219,761,46702,008,0002,392,064
23Feb 24, 2026 17:003,47118,934,86402,013,0002,342,912
24Feb 24, 2026 16:373,47918,685,79402,018,0002,400,256
25Feb 24, 2026 16:323,48317,427,6241,010,0001,010,0002,383,872
26Feb 24, 2026 16:393,49016,766,32402,024,0002,404,352
27Feb 24, 2026 17:013,49017,646,64502,024,0002,375,680
28Feb 24, 2026 16:333,49021,220,04102,024,0002,400,256
29Feb 24, 2026 16:323,57418,969,57502,073,0002,375,680
30Feb 24, 2026 16:403,58417,215,09202,079,0002,330,624
31Feb 24, 2026 17:003,61217,647,87602,095,0002,363,392
32Feb 24, 2026 16:403,62216,216,90202,101,0002,367,488
33Feb 24, 2026 16:353,66915,641,73502,128,0002,383,872
34Feb 24, 2026 16:323,76226,025,08702,182,0002,322,432
35Feb 24, 2026 16:393,82819,223,1961,110,0001,110,0002,400,256
36Feb 24, 2026 16:333,90716,598,94102,266,0002,396,160
37Feb 24, 2026 16:363,95016,668,87902,291,0002,375,680
38Feb 24, 2026 16:333,96217,491,91502,298,0002,371,584
39Feb 24, 2026 16:353,96216,201,77702,298,0002,371,584
40Feb 24, 2026 16:403,96417,344,15302,299,0002,301,952
41Feb 24, 2026 16:394,00317,686,53302,322,0002,293,760
42Feb 24, 2026 16:354,01016,516,84102,326,0002,375,680
43Feb 24, 2026 16:354,04122,097,27402,344,0002,396,160
44Feb 24, 2026 16:324,05915,598,27102,354,0002,404,352
45Feb 24, 2026 16:354,07917,841,1181,183,0001,183,0002,383,872
46Feb 24, 2026 16:354,09116,879,55102,373,0002,400,256
47Feb 24, 2026 16:324,11719,921,84502,388,0002,363,392
48Feb 24, 2026 16:364,14019,582,07502,401,0002,363,392
49Feb 24, 2026 16:364,20722,504,49802,440,0002,289,664
50Feb 24, 2026 16:344,21926,490,87102,447,0002,289,664
51Feb 24, 2026 16:364,23419,800,9731,228,0001,228,0002,400,256
52Feb 24, 2026 16:344,31715,342,5581,252,0001,252,0002,408,448
53Feb 24, 2026 16:364,34122,072,9871,259,0001,259,0002,387,968
54Feb 24, 2026 16:324,40717,456,19702,556,0002,412,544
55Feb 24, 2026 16:364,43418,180,2201,286,0001,286,0002,318,336