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 26, 2026 15:25Dominique GarmierDominique GarmierScore: 8,291Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 26, 2026 15:247,99726,460,9672,319,0002,319,0002,289,664
2Mar 26, 2026 15:248,04121,968,9143,498,0001,166,0002,269,184
3Mar 26, 2026 15:248,06620,907,4922,339,0002,339,0002,310,144
4Mar 26, 2026 15:258,07625,814,4383,513,0001,171,0002,527,232
5Mar 26, 2026 15:258,11026,360,2313,528,0001,176,0002,465,792
6Mar 26, 2026 15:258,11722,675,4804,708,00002,310,144
7Mar 26, 2026 15:248,19020,578,1323,563,0001,187,0002,306,048
8Mar 26, 2026 15:258,29121,658,1414,809,00002,318,336
9Mar 26, 2026 15:248,34819,985,6593,632,0001,210,0002,420,736
10Mar 26, 2026 15:258,46621,560,7152,946,0001,964,0002,514,944
11Mar 26, 2026 15:258,48423,030,0913,937,000984,0002,461,696
12Mar 26, 2026 15:258,48624,053,3943,938,000984,0002,306,048
13Mar 26, 2026 15:258,53123,980,1543,711,0001,237,0002,301,952
14Mar 26, 2026 15:248,56023,727,4262,979,0001,986,0002,514,944
15Mar 26, 2026 15:258,57820,856,5322,985,0001,990,0002,322,432