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 27, 2025 14:16Wookyung JeongWookyung JeongScore: 86,609Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 14:1683,79374,744,89240,500,0008,100,0002,424,832
2Sep 27, 2025 14:1684,01974,806,10243,655,0005,076,0002,457,600
3Sep 27, 2025 14:1684,80573,357,16944,168,0005,019,0002,424,832
4Sep 27, 2025 14:1685,69077,239,82346,658,0003,042,0002,428,928
5Sep 27, 2025 14:1686,60973,155,84143,201,0007,032,0002,289,664
6Sep 27, 2025 14:1688,75274,338,07242,392,0009,084,0002,396,160
7Sep 27, 2025 14:1689,58876,728,83146,965,0004,996,0002,445,312
8Sep 27, 2025 14:1689,96979,734,48244,154,0008,028,0002,502,656
9Sep 27, 2025 14:1692,17974,002,12748,421,0005,043,0002,420,736