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 listSep 29, 2025 08:17Wookyung JeongWookyung JeongScore: 59,555Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 29, 2025 08:1756,01453,626,66830,458,0002,030,0002,293,760
2Sep 29, 2025 08:1757,06960,251,18728,085,0005,015,0002,449,408
3Sep 29, 2025 08:1657,09355,641,30629,101,0004,013,0002,482,176
4Sep 29, 2025 08:1757,69554,475,65630,421,0003,042,0002,297,856
5Sep 29, 2025 08:1657,87458,644,06430,516,0003,051,0002,334,720
6Sep 29, 2025 08:1758,99554,980,43030,192,0004,025,0002,371,584
7Sep 29, 2025 08:1659,17657,277,20931,294,0003,028,0002,277,376
8Sep 29, 2025 08:1659,51257,621,19832,487,0002,030,0002,506,752
9Sep 29, 2025 08:1659,53457,327,25931,484,0003,046,0002,412,544
10Sep 29, 2025 08:1659,55562,384,07726,415,0008,127,0002,293,760
11Sep 29, 2025 08:1659,68363,433,47629,526,0005,090,0002,318,336
12Sep 29, 2025 08:1760,17260,252,31429,915,0004,985,0002,478,080
13Sep 29, 2025 08:1761,03458,981,45831,355,0004,045,0002,510,848
14Sep 29, 2025 08:1661,38463,154,96730,517,0005,086,0002,318,336
15Sep 29, 2025 08:1761,87662,056,37830,904,0004,984,0002,359,296
16Sep 29, 2025 08:1662,44559,055,61430,182,0006,036,0002,428,928
17Sep 29, 2025 08:1762,95559,337,18532,457,0004,057,0002,322,432
18Sep 29, 2025 08:1764,19561,115,51633,208,0004,025,0002,502,656