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 14:39Dominique GarmierDominique GarmierScore: 7,686Success
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 14:397,43821,393,6104,314,00002,359,296
2Mar 26, 2026 14:487,55223,575,3622,190,0002,190,0002,359,296
3Mar 26, 2026 14:397,60326,370,2682,205,0002,205,0002,367,488
4Mar 26, 2026 14:487,61627,998,9213,313,0001,104,0002,371,584
5Mar 26, 2026 14:487,62823,400,0572,212,0002,212,0002,486,272
6Mar 26, 2026 14:397,63823,497,6054,430,00002,281,472
7Mar 26, 2026 14:487,65525,252,2153,330,0001,110,0002,281,472
8Mar 26, 2026 14:487,66923,296,5623,336,0001,112,0002,363,392
9Mar 26, 2026 14:487,67627,536,8443,339,0001,113,0002,375,680
10Mar 26, 2026 14:397,68623,117,9933,344,0001,114,0002,367,488
11Mar 26, 2026 14:487,68624,535,6463,344,0001,114,0002,285,568
12Mar 26, 2026 14:397,69018,119,3663,345,0001,115,0002,281,472
13Mar 26, 2026 14:487,70321,956,4183,351,0001,117,0002,371,584
14Mar 26, 2026 14:397,80226,744,1633,394,0001,131,0002,363,392
15Mar 26, 2026 14:397,80221,194,3473,394,0001,131,0002,371,584
16Mar 26, 2026 14:397,87122,374,1933,424,0001,141,0002,371,584
17Mar 26, 2026 14:397,88622,270,1713,431,0001,143,0002,371,584
18Mar 26, 2026 14:487,94125,690,1293,455,0001,151,0002,355,200