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 17, 2024 19:10E SequeiraE SequeiraScore: 76,462Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 17, 2024 19:1075,79766,565,88641,964,0001,998,0002,510,848
2Apr 17, 2024 19:1075,91067,584,14742,027,0002,001,0002,285,568
3Apr 17, 2024 19:1076,04367,135,07242,101,0002,004,0002,269,184
4Apr 17, 2024 19:1076,05767,514,87442,108,0002,005,0002,285,568
5Apr 17, 2024 19:1076,20365,723,79642,189,0002,009,0002,285,568
6Apr 17, 2024 19:1076,27466,346,01943,234,0001,005,0002,260,992
7Apr 17, 2024 19:1076,46275,486,63443,341,0001,007,0002,269,184
8Apr 17, 2024 19:1077,38471,101,47242,889,0001,994,0002,359,296
9Apr 17, 2024 19:1077,39371,966,09143,891,000997,0002,519,040
10Apr 17, 2024 19:1079,500110,992,30446,109,96802,224,128
11Apr 17, 2024 19:1079,573111,813,27346,152,22402,228,224
12Apr 17, 2024 19:1081,229110,318,13947,112,76302,228,224