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 listApr 10, 2025 17:18yenw0dyenw0dScore: 123,629Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 10, 2025 17:18122,366105,207,83965,974,0004,998,0002,433,024
2Apr 10, 2025 17:18123,005102,425,80565,314,0006,029,0002,314,240
3Apr 10, 2025 17:18123,12498,450,09067,389,0004,023,0002,424,832
4Apr 10, 2025 17:18123,34193,627,12265,493,0006,045,0002,502,656
5Apr 10, 2025 17:18123,62998,660,75867,666,0004,039,0002,437,120
6Apr 10, 2025 17:18123,96498,376,44767,905,0003,994,0002,494,464
7Apr 10, 2025 17:18124,228102,223,80068,050,0004,002,0002,449,408
8Apr 10, 2025 17:18124,366100,287,92467,123,0005,009,0002,359,296
9Apr 10, 2025 17:18124,916101,205,75168,426,0004,025,0002,473,984