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 18, 2025 11:20Wookyung JeongWookyung JeongScore: 39,564Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 18, 2025 11:2038,10346,608,33021,096,0001,004,0002,506,752
2Dec 18, 2025 11:1938,61046,154,48821,377,0001,017,0002,420,736
3Dec 18, 2025 11:2038,78149,526,56121,471,0001,022,0002,469,888
4Dec 18, 2025 11:1939,02448,694,57021,606,0001,028,0002,273,280
5Dec 18, 2025 11:1939,07149,357,16721,631,0001,030,0002,412,544
6Dec 18, 2025 11:2139,11454,719,44621,655,0001,031,0002,404,352
7Dec 18, 2025 11:1939,11950,526,46621,658,0001,031,0002,416,640
8Dec 18, 2025 11:1939,48453,048,24821,906,000995,0002,486,272
9Dec 18, 2025 11:2039,53344,603,07721,887,0001,042,0002,338,816
10Dec 18, 2025 11:2139,56447,629,60920,952,0001,995,0002,445,312
11Dec 18, 2025 11:2039,60245,534,56120,972,0001,997,0002,453,504
12Dec 18, 2025 11:2039,60950,849,77020,976,0001,997,0002,486,272
13Dec 18, 2025 11:2139,68642,263,33421,017,0002,001,0002,412,544
14Dec 18, 2025 11:2039,74044,343,09722,047,0001,002,0002,433,024
15Dec 18, 2025 11:2039,80549,094,45821,080,0002,007,0002,400,256
16Dec 18, 2025 11:1939,82951,370,60922,097,0001,004,0002,420,736
17Dec 18, 2025 11:2039,88344,854,18522,127,0001,005,0002,490,368
18Dec 18, 2025 11:2040,19546,833,07822,300,0001,013,0002,478,080