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 listAug 19, 2025 00:58CadovvlCadovvlScore: 63,684Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 19, 2025 00:5961,91771,674,85533,917,0001,995,0002,318,336
2Aug 19, 2025 00:5962,73359,002,38835,375,0001,010,0002,482,176
3Aug 19, 2025 00:5962,78861,170,84435,406,0001,011,0002,301,952
4Aug 19, 2025 00:5962,87963,381,20036,470,00002,306,048
5Aug 19, 2025 00:5963,15960,233,70435,615,0001,017,0002,490,368
6Aug 19, 2025 01:0363,17460,950,92335,624,0001,017,0002,322,432
7Aug 19, 2025 00:5963,18364,709,66336,646,00002,326,528
8Aug 19, 2025 00:5963,34565,308,18935,720,0001,020,0002,490,368
9Aug 19, 2025 00:5863,54865,049,99434,866,0001,992,0002,318,336
10Aug 19, 2025 01:0363,63861,674,58134,915,0001,995,0002,506,752
11Aug 19, 2025 00:5963,68464,404,22734,941,0001,996,0002,330,624
12Aug 19, 2025 01:0363,90960,857,74836,066,0001,001,0002,510,848
13Aug 19, 2025 00:5963,97660,868,56836,104,0001,002,0002,510,848
14Aug 19, 2025 01:0263,98164,559,60035,104,0002,005,0002,342,912
15Aug 19, 2025 01:0264,02866,932,56735,129,0002,007,0002,506,752
16Aug 19, 2025 00:5864,03359,324,69235,132,0002,007,0002,330,624
17Aug 19, 2025 01:0264,15263,496,01836,203,0001,005,0002,506,752
18Aug 19, 2025 00:5864,35765,095,75436,319,0001,008,0002,482,176
19Aug 19, 2025 00:5867,21463,884,92035,986,0002,998,0002,310,144
20Aug 19, 2025 00:5867,36466,693,00638,070,0001,001,0002,490,368
21Aug 19, 2025 00:5868,03864,555,14838,451,0001,011,0002,494,464