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 20, 2025 21:53HattonuriHattonuriScore: 36,978Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 21:4734,32440,173,46918,913,000995,0002,531,328
2Dec 20, 2025 22:0634,38344,326,18918,945,000997,0002,269,184
3Dec 20, 2025 21:4634,69342,925,41419,116,0001,006,0002,269,184
4Dec 20, 2025 21:4634,79841,277,48619,174,0001,009,0002,531,328
5Dec 20, 2025 21:5434,84345,502,89719,199,0001,010,0002,269,184
6Dec 20, 2025 21:4634,99542,581,58619,283,0001,014,0002,285,568
7Dec 20, 2025 21:4735,22639,822,78920,431,00002,531,328
8Dec 20, 2025 21:5335,22640,628,18819,410,0001,021,0002,281,472
9Dec 20, 2025 21:4635,29748,055,51620,472,00002,523,136
10Dec 20, 2025 21:5435,44046,540,84720,555,00002,289,664
11Dec 20, 2025 21:4735,44841,442,68920,560,00002,285,568
12Dec 20, 2025 21:4735,56639,481,80119,597,0001,031,0002,269,184
13Dec 20, 2025 21:5335,79847,918,53919,775,000988,0002,531,328
14Dec 20, 2025 21:5336,06245,635,63419,920,000996,0002,273,280
15Dec 20, 2025 21:5436,21740,526,58620,006,0001,000,0002,531,328
16Dec 20, 2025 21:5336,26042,276,62919,028,0002,003,0002,531,328
17Dec 20, 2025 21:5336,36049,202,45820,085,0001,004,0002,523,136
18Dec 20, 2025 21:5436,37142,351,29819,086,0002,009,0002,527,232
19Dec 20, 2025 21:5436,38345,118,16320,098,0001,004,0002,535,424
20Dec 20, 2025 21:5336,53342,461,04419,171,0002,018,0002,523,136
21Dec 20, 2025 21:4736,54841,618,02120,189,0001,009,0002,281,472
22Dec 20, 2025 22:0636,85247,484,55020,357,0001,017,0002,523,136
23Dec 20, 2025 21:5436,85947,810,37121,378,00002,531,328
24Dec 20, 2025 21:4936,86739,542,26020,365,0001,018,0002,527,232
25Dec 20, 2025 21:4636,97843,282,85921,447,00002,269,184
26Dec 20, 2025 21:5336,98146,490,23720,428,0001,021,0002,523,136
27Dec 20, 2025 21:5336,99047,721,80421,454,00002,269,184
28Dec 20, 2025 21:4937,01045,443,29020,444,0001,022,0002,281,472
29Dec 20, 2025 21:4737,23842,385,11121,598,00002,367,488
30Dec 20, 2025 21:4637,46947,934,16421,732,00002,535,424
31Dec 20, 2025 21:4637,51946,719,66121,761,00002,527,232
32Dec 20, 2025 21:5337,75344,070,77920,902,000995,0002,375,680
33Dec 20, 2025 21:4737,76746,502,58720,910,000995,0002,527,232
34Dec 20, 2025 21:5337,89746,473,14020,981,000999,0002,535,424
35Dec 20, 2025 21:4937,90348,450,20719,986,0001,998,0002,523,136
36Dec 20, 2025 21:4737,95043,146,97021,011,0001,000,0002,265,088
37Dec 20, 2025 21:5437,95743,485,48320,014,0002,001,0002,527,232
38Dec 20, 2025 21:5438,04144,777,64720,059,0002,005,0002,527,232
39Dec 20, 2025 21:5338,15342,057,74620,118,0002,011,0002,523,136
40Dec 20, 2025 21:5338,22846,331,19320,157,0002,015,0002,277,376
41Dec 20, 2025 21:5338,39146,319,75721,255,0001,012,0002,269,184
42Dec 20, 2025 21:4638,41943,303,31920,258,0002,025,0002,519,040
43Dec 20, 2025 21:4638,66742,610,78622,427,00002,531,328
44Dec 20, 2025 21:5338,77443,778,42721,467,0001,022,0002,527,232
45Dec 20, 2025 21:5338,78443,855,56221,473,0001,022,0002,355,200
46Dec 20, 2025 21:4739,07152,488,02321,631,0001,030,0002,519,040
47Dec 20, 2025 22:0639,17647,028,66821,690,0001,032,0002,535,424
48Dec 20, 2025 21:5439,48645,589,23620,911,0001,991,0002,519,040