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 GarmierScore: 19,786Success
Source Code

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

Challenge History

No challenges yet.

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