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 19, 2025 20:49KeefeKeefeScore: 107,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 19, 2025 20:50103,21693,806,02857,870,0001,995,0002,428,928
2Apr 19, 2025 20:49104,11785,730,85557,369,0003,019,0002,351,104
3Apr 19, 2025 20:49104,74091,542,26657,712,0003,037,0002,433,024
4Apr 19, 2025 20:50105,64378,356,88059,265,0002,008,0002,510,848
5Apr 19, 2025 20:49106,23489,484,09558,586,0003,030,0002,326,528
6Apr 19, 2025 20:50106,32290,120,62458,635,0003,032,0002,330,624
7Apr 19, 2025 20:50106,36991,801,09858,660,0003,034,0002,404,352
8Apr 19, 2025 20:49107,32684,729,76656,225,0006,024,0002,412,544
9Apr 19, 2025 20:50107,34086,535,25559,245,0003,012,0002,338,816
10Apr 19, 2025 20:50107,62985,463,28260,412,0002,013,0002,486,272
11Apr 19, 2025 20:50108,01290,622,16858,606,0004,041,0002,342,912
12Apr 19, 2025 20:49108,11792,437,91758,663,0004,045,0002,420,736
13Apr 19, 2025 20:49108,39890,787,13856,884,0005,987,0002,416,640
14Apr 19, 2025 20:50108,46687,316,36157,918,0004,992,0002,314,240
15Apr 19, 2025 20:49108,61790,741,79357,999,0004,999,0002,322,432
16Apr 19, 2025 20:49108,66989,781,24161,028,0002,000,0002,428,928
17Apr 19, 2025 20:49108,67489,837,82460,030,0003,001,0002,285,568
18Apr 19, 2025 20:50108,73384,213,59257,059,0006,006,0002,457,600