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 23, 2025 15:27Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:22Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:26010,974,937975,000024,576No answer to stdout. Died or killed?
2Dec 23, 2025 15:1134,30744,531,95518,904,000994,0002,461,696
3Dec 23, 2025 15:2534,32846,506,57919,910,00002,428,928
4Dec 23, 2025 15:1134,33648,673,80017,924,0001,991,0002,404,352
5Dec 25, 2025 01:3134,64547,113,01818,085,0002,009,0002,441,216
6Dec 23, 2025 15:1135,03442,202,82720,320,00002,392,064
7Dec 23, 2025 15:1135,11648,039,61020,367,00002,437,120
8Dec 25, 2025 01:3135,21747,369,58920,426,00002,473,984
9Dec 23, 2025 15:2735,53846,045,65520,612,00002,379,776
10Dec 23, 2025 15:1035,54545,516,92119,586,0001,030,0002,400,256
11Dec 23, 2025 15:1135,64546,439,92119,641,0001,033,0002,486,272
12Dec 23, 2025 15:1035,66748,660,55720,687,00002,437,120
13Dec 23, 2025 15:1135,69743,748,53319,669,0001,035,0002,445,312
14Dec 23, 2025 15:1135,70948,768,95519,676,0001,035,0002,367,488
15Dec 23, 2025 15:1136,00942,332,97619,891,000994,0002,277,376
16Dec 25, 2025 01:2936,03646,763,86118,911,0001,990,0002,351,104
17Dec 23, 2025 15:1136,04039,075,71320,903,00002,359,296
18Dec 23, 2025 15:1136,04347,652,30320,905,00002,437,120
19Dec 23, 2025 15:2636,28449,304,00720,043,0001,002,0002,506,752
20Dec 25, 2025 01:3136,29351,000,99619,046,0002,004,0002,326,528
21Dec 23, 2025 15:1136,35953,404,46920,084,0001,004,0002,297,856
22Dec 25, 2025 01:2936,39144,841,35719,097,0002,010,0002,289,664
23Dec 23, 2025 15:1136,39547,367,21319,099,0002,010,0002,301,952
24Dec 23, 2025 15:1136,40947,737,55120,112,0001,005,0002,330,624
25Dec 23, 2025 15:1136,41250,037,54820,114,0001,005,0002,453,504
26Dec 23, 2025 15:1136,42444,545,54519,114,0002,012,0002,297,856
27Dec 23, 2025 15:2636,56046,513,82819,186,0002,019,0002,293,760
28Dec 23, 2025 15:2636,57943,976,34820,206,0001,010,0002,437,120
29Dec 23, 2025 15:1136,63444,706,97120,237,0001,011,0002,420,736
30Dec 23, 2025 15:1136,65045,045,44619,233,0002,024,0002,342,912
31Dec 23, 2025 15:1136,68347,259,14420,263,0001,013,0002,404,352
32Dec 25, 2025 01:2936,75042,325,58119,285,0002,030,0002,392,064
33Dec 23, 2025 15:2536,87642,903,03421,388,00002,371,584
34Dec 23, 2025 15:1036,90950,790,19420,388,0001,019,0002,269,184
35Dec 23, 2025 15:2737,07650,264,76721,504,00002,514,944
36Dec 23, 2025 15:2537,12446,050,90420,507,0001,025,0002,363,392
37Dec 23, 2025 15:2737,16943,735,71721,558,00002,355,200