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 21, 2025 01:59HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:11Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:1108,641,308789,000020,480No answer to stdout. Died or killed?
2Dec 21, 2025 01:5931,50335,160,94317,257,0001,015,0002,531,328
3Dec 21, 2025 01:5932,12936,254,96518,635,00002,531,328
4Dec 21, 2025 02:0032,19543,803,44217,636,0001,037,0002,527,232
5Dec 21, 2025 01:5932,59747,639,76716,916,0001,990,0002,441,216
6Dec 21, 2025 01:5832,67642,752,28717,955,000997,0002,297,856
7Dec 21, 2025 01:5932,67844,653,13316,958,0001,995,0002,523,136
8Dec 21, 2025 01:5933,43441,642,42818,372,0001,020,0002,293,760
9Dec 21, 2025 01:5833,44135,143,43718,376,0001,020,0002,527,232
10Dec 21, 2025 02:0033,46446,904,03919,409,00002,531,328
11Dec 21, 2025 02:0133,61245,788,39918,469,0001,026,0002,289,664
12Dec 21, 2025 01:5833,77442,355,62718,558,0001,031,0002,527,232
13Dec 21, 2025 02:0033,83648,527,75118,593,0001,032,0002,523,136
14Dec 21, 2025 01:5934,00044,180,77518,683,0001,037,0002,285,568
15Dec 21, 2025 01:5834,52945,441,56319,026,0001,001,0002,523,136
16Dec 21, 2025 01:5934,57448,610,20119,051,0001,002,0002,297,856
17Dec 21, 2025 02:0034,85043,821,30419,203,0001,010,0002,531,328
18Dec 21, 2025 02:0134,92240,553,82120,255,00002,527,232
19Dec 21, 2025 01:5935,08350,549,87519,331,0001,017,0002,289,664
20Dec 21, 2025 01:5835,16641,998,36119,377,0001,019,0002,531,328
21Dec 21, 2025 01:5935,24739,919,47119,421,0001,022,0002,289,664
22Dec 21, 2025 01:5835,25753,842,68019,427,0001,022,0002,523,136
23Dec 21, 2025 02:0035,33348,482,81619,469,0001,024,0002,523,136
24Dec 21, 2025 02:0135,38141,261,20219,495,0001,026,0002,523,136
25Dec 21, 2025 01:5935,50345,746,61320,592,00002,281,472
26Dec 21, 2025 02:0035,51751,115,10220,600,00002,523,136
27Dec 21, 2025 01:5936,05545,634,41718,921,0001,991,0002,289,664
28Dec 21, 2025 01:5936,08844,033,15719,935,000996,0002,519,040