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:38HattonuriHattonuriScore: 38,226Success
Source Code

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

Challenge History

No challenges yet.

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