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 20, 2025 16:27idatsyidatsyScore: 65,048Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 16:2863,06457,762,79636,577,00002,285,568
2Dec 20, 2025 16:2863,14767,121,08436,625,00002,277,376
3Dec 20, 2025 16:2863,62459,663,19735,905,000997,0002,285,568
4Dec 20, 2025 16:2863,84165,852,47635,027,0002,001,0002,322,432
5Dec 20, 2025 16:2863,85364,273,55036,035,0001,000,0002,277,376
6Dec 20, 2025 16:2863,86063,539,01736,038,0001,001,0002,265,088
7Dec 20, 2025 15:5863,89162,258,68736,056,0001,001,0002,498,560
8Dec 20, 2025 15:5863,93459,875,13336,080,0001,002,0002,277,376
9Dec 20, 2025 15:5863,94361,264,77135,083,0002,004,0002,285,568
10Dec 20, 2025 15:5864,41261,800,66537,359,00002,277,376
11Dec 20, 2025 16:2864,45364,128,28536,373,0001,010,0002,285,568
12Dec 20, 2025 15:5864,49066,546,29137,404,00002,285,568
13Dec 20, 2025 16:2865,04863,958,30637,728,00002,392,064
14Dec 20, 2025 16:2765,09769,717,66435,769,0001,987,0002,322,432
15Dec 20, 2025 16:2765,63861,346,39736,067,0002,003,0002,498,560
16Dec 20, 2025 15:5865,67159,623,32836,085,0002,004,0002,494,464
17Dec 20, 2025 16:2865,67458,670,96636,087,0002,004,0002,277,376
18Dec 20, 2025 16:2866,03860,917,00037,295,0001,007,0002,494,464
19Dec 20, 2025 15:5866,08363,229,14337,320,0001,008,0002,494,464
20Dec 20, 2025 16:2866,16672,107,98137,367,0001,009,0002,265,088
21Dec 20, 2025 16:2766,18467,745,13138,387,00002,494,464
22Dec 20, 2025 15:5866,37664,405,35237,485,0001,013,0002,498,560
23Dec 20, 2025 15:5867,07962,613,35138,906,00002,269,184
24Dec 20, 2025 16:2867,35769,765,20338,066,0001,001,0002,453,504