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 17, 2025 01:38HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 22:07Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 22:08076,240,09968,423,000016,384No answer to stdout. Died or killed?
2Dec 17, 2025 01:3534,39138,592,09718,950,000997,0002,269,184
3Dec 17, 2025 01:3534,53140,309,36219,027,0001,001,0002,523,136
4Dec 17, 2025 01:3535,41240,974,61520,539,00002,281,472
5Dec 17, 2025 01:3435,43641,465,07320,553,00002,531,328
6Dec 17, 2025 01:3836,04844,396,10119,913,000995,0002,531,328
7Dec 17, 2025 01:3436,10043,550,02618,944,0001,994,0002,367,488
8Dec 17, 2025 01:3436,45241,354,94719,129,0002,013,0002,281,472
9Dec 17, 2025 01:3436,47440,696,98220,148,0001,007,0002,527,232
10Dec 17, 2025 01:3436,89046,645,41721,396,00002,269,184
11Dec 17, 2025 01:3537,19840,076,86920,548,0001,027,0002,539,520
12Dec 17, 2025 01:3537,37443,177,91121,677,00002,539,520
13Dec 17, 2025 01:3537,47951,196,40721,738,00002,527,232
14Dec 17, 2025 01:3537,82236,273,06520,940,000997,0002,527,232
15Dec 17, 2025 01:3438,22645,714,36621,164,0001,007,0002,531,328
16Dec 17, 2025 01:3438,28454,181,96321,196,0001,009,0002,527,232
17Dec 17, 2025 01:3538,40745,268,92322,276,00002,527,232
18Dec 17, 2025 01:3838,57152,306,88421,355,0001,016,0002,269,184
19Dec 17, 2025 01:3438,80044,973,78621,482,0001,022,0002,527,232
20Dec 17, 2025 01:3839,09153,817,60121,643,0001,030,0002,539,520
21Dec 17, 2025 01:3839,60050,335,14120,971,0001,997,0002,527,232
22Dec 17, 2025 01:3539,70245,552,93622,026,0001,001,0002,355,200
23Dec 17, 2025 01:3839,73646,487,75622,045,0001,002,0002,527,232
24Dec 17, 2025 01:3439,80545,572,26821,080,0002,007,0002,523,136
25Dec 17, 2025 01:3539,84841,998,62522,108,0001,004,0002,527,232
26Dec 17, 2025 01:3840,08645,608,88222,240,0001,010,0002,527,232
27Dec 17, 2025 01:3540,64846,573,90223,576,00002,527,232
28Dec 17, 2025 01:3541,68850,761,80323,172,0001,007,0002,527,232