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 MartinScore: 36,293Success
Source Code

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

Challenge History

No challenges yet.

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