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 listOct 19, 2024 09:10@drytecc@dryteccScore: 17,079Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 19, 2024 09:1016,50326,291,2777,445,0002,127,0002,469,888
2Oct 19, 2024 09:1016,57430,254,0957,477,0002,136,0002,457,600
3Oct 19, 2024 09:1016,62125,771,0797,498,0002,142,0002,338,816
4Oct 19, 2024 09:1016,67431,603,9827,522,0002,149,0002,449,408
5Oct 19, 2024 09:1017,07933,049,8157,925,0001,981,0002,469,888
6Oct 19, 2024 09:1017,08830,938,7475,947,0003,964,0002,527,232
7Oct 19, 2024 09:1017,09836,782,3106,942,0002,975,0002,457,600
8Oct 19, 2024 09:1017,14532,035,6264,972,0004,972,0002,465,792
9Oct 19, 2024 09:1017,16027,197,8896,967,0002,986,0002,293,760