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:52KeefeKeefeScore: 105,567Success
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:52103,98182,062,22158,299,0002,010,0002,306,048
2Apr 19, 2025 20:52104,34381,740,64855,476,0005,043,0002,260,992
3Apr 19, 2025 20:52105,20384,262,56355,017,0006,001,0002,506,752
4Apr 19, 2025 20:52105,40288,203,91158,127,0003,006,0002,306,048
5Apr 19, 2025 20:52105,43186,731,49455,136,0006,014,0002,285,568
6Apr 19, 2025 20:52105,55082,296,41250,180,00011,039,0002,424,832
7Apr 19, 2025 20:52105,56782,883,67556,211,0005,018,0002,498,560
8Apr 19, 2025 20:52105,73384,373,85456,299,0005,026,0002,506,752
9Apr 19, 2025 20:52105,97491,364,62253,404,0008,061,0002,502,656
10Apr 19, 2025 20:52106,24786,215,56357,583,0004,040,0002,265,088
11Apr 19, 2025 20:52106,65286,913,63758,865,0002,993,0002,314,240
12Apr 19, 2025 20:52106,91491,687,48355,009,0007,001,0002,424,832