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 listMar 21, 2026 02:00Dominique GarmierDominique GarmierError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 16:04Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 16:11036,069,49321,464,0004,304,0002,117,632expected "881622654 ", got "0 "
2Sep 11, 2026 16:11038,058,19023,461,0004,315,0002,117,632expected "862869826 ", got "0 "
3Sep 11, 2026 16:11042,214,50926,908,0004,313,0002,117,632expected "881652983 ", got "0 "
4Mar 20, 2026 11:2119,43125,744,0979,221,0002,049,0002,269,184
5Mar 20, 2026 11:2119,50026,765,85711,310,00002,289,664
6Mar 20, 2026 03:0419,52932,322,01610,298,0001,029,0002,273,280
7Mar 20, 2026 03:0419,59727,906,13710,333,0001,033,0002,265,088
8Mar 19, 2026 03:3219,59828,086,41210,334,0001,033,0002,289,664
9Mar 20, 2026 07:4519,63631,128,89810,354,0001,035,0002,260,992
10Mar 19, 2026 12:1319,67223,723,14311,410,00002,277,376
11Mar 20, 2026 11:3719,73827,636,92610,408,0001,040,0002,289,664
12Mar 20, 2026 07:4519,74331,436,67510,410,0001,041,0002,519,040
13Mar 19, 2026 06:4919,74325,434,63810,410,0001,041,0002,265,088
14Mar 19, 2026 03:4919,75327,592,31910,416,0001,041,0002,379,776
15Mar 19, 2026 12:1319,75927,343,91610,419,0001,041,0002,281,472
16Mar 19, 2026 03:5119,75928,896,50910,419,0001,041,0002,289,664
17Mar 20, 2026 11:2119,76732,822,16810,423,0001,042,0002,289,664
18Mar 19, 2026 06:4919,77132,726,85410,425,0001,042,0002,265,088
19Mar 19, 2026 03:5119,77126,549,77610,425,0001,042,0002,289,664
20Mar 19, 2026 03:2819,78632,766,70510,433,0001,043,0002,523,136
21Mar 20, 2026 11:3719,79730,842,46710,439,0001,043,0002,269,184
22Mar 19, 2026 12:1319,94732,763,81510,518,0001,051,0002,506,752
23Mar 19, 2026 03:4919,99833,891,32310,545,0001,054,0002,289,664
24Mar 19, 2026 03:2820,05231,821,72111,630,00002,260,992
25Mar 19, 2026 03:2820,05329,199,88511,631,00002,289,664
26Mar 19, 2026 03:3220,05929,947,71710,577,0001,057,0002,273,280
27Mar 19, 2026 03:3220,05928,438,23410,577,0001,057,0002,265,088
28Mar 19, 2026 03:5120,08131,196,33810,589,0001,058,0002,281,472
29Mar 19, 2026 03:4920,08827,691,10011,651,00002,371,584
30Mar 20, 2026 11:3720,09525,940,79911,655,00002,289,664
31Mar 20, 2026 03:0420,15332,365,08510,627,0001,062,0002,289,664
32Mar 21, 2026 02:0020,15533,055,89510,628,0001,062,0002,289,664
33Mar 20, 2026 07:4520,16429,807,58710,632,0001,063,0002,289,664
34Mar 21, 2026 02:0020,22948,545,26711,733,00002,265,088
35Mar 21, 2026 02:0020,26940,969,88010,688,0001,068,0002,260,992
36Mar 19, 2026 06:4920,59727,495,29710,951,000995,0002,265,088