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 29, 2025 17:52Unays Unays Score: 98,202Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 17:5295,36984,323,85251,292,0004,022,0002,379,776
2Dec 29, 2025 17:5295,48883,767,68150,349,0005,034,0002,289,664
3Dec 28, 2025 18:1095,49386,044,69250,351,0005,035,0002,281,472
4Dec 29, 2025 17:5295,65982,203,97950,439,0005,043,0002,285,568
5Dec 29, 2025 17:5295,69879,385,37651,469,0004,036,0002,510,848
6Dec 29, 2025 17:5295,92880,768,25449,677,0005,961,0002,363,392
7Dec 29, 2025 17:5296,07986,758,66652,687,0003,039,0002,359,296
8Dec 29, 2025 17:5296,09081,639,16151,679,0004,053,0002,314,240
9Dec 29, 2025 17:5296,32887,377,00650,882,0004,988,0002,486,272
10Dec 28, 2025 18:1096,33186,749,66251,882,0003,990,0002,289,664
11Dec 29, 2025 17:5196,40382,227,15251,921,0003,993,0002,461,696
12Dec 29, 2025 17:5296,48386,817,08551,963,0003,997,0002,363,392
13Dec 29, 2025 17:5296,92880,725,78551,199,0005,019,0002,486,272
14Dec 29, 2025 17:5297,18382,210,85052,340,0004,026,0002,396,160
15Dec 29, 2025 17:5298,06085,047,15652,884,0003,991,0002,412,544
16Dec 29, 2025 17:5298,20280,158,48453,960,0002,997,0002,273,280
17Dec 29, 2025 17:5298,27687,632,23453,000,0004,000,0002,441,216
18Dec 29, 2025 17:5198,49882,578,88852,118,0005,011,0002,445,312
19Dec 29, 2025 17:5298,97289,636,61152,369,0005,035,0002,367,488
20Dec 29, 2025 17:5299,74583,167,68453,863,0003,989,0002,277,376
21Dec 29, 2025 17:5299,95587,598,14053,976,0003,998,0002,510,848
22Dec 29, 2025 17:52100,78482,860,14654,424,0004,031,0002,285,568
23Dec 28, 2025 18:10101,04086,920,35954,562,0004,041,0002,449,408
24Dec 29, 2025 17:52101,78188,989,09954,031,0005,002,0002,441,216
25Dec 29, 2025 17:52101,94587,916,64854,118,0005,010,0002,441,216
26Dec 29, 2025 17:52102,27287,072,91254,291,0005,027,0002,523,136
27Dec 29, 2025 17:52102,52685,714,32555,434,0004,031,0002,482,176
28Dec 29, 2025 17:51102,52981,753,48854,428,0005,039,0002,330,624
29Dec 29, 2025 17:52102,73389,002,10955,546,0004,039,0002,297,856
30Dec 29, 2025 17:52103,75385,115,43855,163,0005,014,0002,465,792