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 listMar 28, 2025 02:30NoSIMD_C#NoSIMD_C#Score: 1,339,391Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 28, 2025 02:301,328,353801,260,110765,449,0004,996,0002,420,736
2Mar 28, 2025 02:301,329,914805,794,796763,357,0007,993,0002,285,568
3Mar 28, 2025 02:301,337,128804,449,412768,539,0006,995,0002,469,888
4Mar 28, 2025 02:301,338,598801,643,521770,392,0005,995,0002,392,064
5Mar 28, 2025 02:301,339,391810,814,404772,848,0003,999,0002,461,696
6Mar 28, 2025 02:301,339,672799,846,504767,010,00010,000,0002,453,504
7Mar 28, 2025 02:301,342,305804,691,043770,542,0007,995,0002,449,408
8Mar 28, 2025 02:301,343,312808,268,635778,121,0001,000,0002,437,120
9Mar 28, 2025 02:301,343,907802,983,258774,470,0004,996,0002,330,624