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:25HattonuriHattonuriScore: 37,764Success
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:3134,40743,675,90017,961,0001,995,0002,269,184
2Dec 17, 2025 01:3134,65046,007,87218,088,0002,009,0002,531,328
3Dec 17, 2025 01:3134,66640,145,42719,101,0001,005,0002,269,184
4Dec 17, 2025 01:3135,17840,397,50620,403,00002,269,184
5Dec 17, 2025 01:3035,19544,645,78920,413,00002,269,184
6Dec 17, 2025 01:3036,04145,212,24519,909,000995,0002,531,328
7Dec 17, 2025 01:2536,04555,251,56519,911,000995,0002,269,184
8Dec 17, 2025 01:3036,16244,753,86919,976,000998,0002,531,328
9Dec 17, 2025 01:3136,25751,004,43020,028,0001,001,0002,269,184
10Dec 17, 2025 01:3036,93142,602,13920,400,0001,020,0002,527,232
11Dec 17, 2025 01:3136,94045,586,09321,425,00002,527,232
12Dec 17, 2025 01:3037,14547,190,01221,544,00002,527,232
13Dec 17, 2025 01:3137,76439,249,15520,908,000995,0002,527,232
14Dec 17, 2025 01:3138,51244,202,05620,307,0002,030,0002,527,232
15Dec 17, 2025 01:3138,82243,620,14322,517,00002,539,520
16Dec 17, 2025 01:3038,92144,760,03922,574,00002,523,136
17Dec 17, 2025 01:2539,24053,068,89122,759,00002,539,520
18Dec 17, 2025 01:2539,47244,801,37820,904,0001,990,0002,527,232
19Dec 17, 2025 01:3139,48149,699,11521,904,000995,0002,527,232
20Dec 17, 2025 01:3139,48850,948,08421,908,000995,0002,527,232
21Dec 17, 2025 01:3139,51053,052,77121,920,000996,0002,527,232
22Dec 17, 2025 01:3039,57144,448,95821,954,000997,0002,527,232
23Dec 17, 2025 01:3039,83145,468,27222,098,0001,004,0002,269,184
24Dec 17, 2025 01:3041,50544,481,32122,067,0002,006,0002,277,376