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:53HattonuriHattonuriError
Source Code

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

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