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 5, 2023 15:00apuxbtapuxbtScore: 79,100Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2023 15:0077,798105,638,62145,123,07202,195,456
2Oct 5, 2023 15:0077,89371,506,97340,159,0005,019,0002,486,272
3Oct 5, 2023 15:0078,03367,260,06541,236,0004,023,0002,494,464
4Oct 5, 2023 15:0078,26269,123,82543,375,0002,017,0002,486,272
5Oct 5, 2023 15:0078,61065,782,02739,515,0006,079,0002,490,368
6Oct 5, 2023 15:0079,085104,337,92645,869,04402,203,648
7Oct 5, 2023 15:0079,100112,064,10245,877,94102,215,936
8Oct 5, 2023 15:0079,25973,120,30739,974,0005,996,0002,355,200
9Oct 5, 2023 15:0079,37170,202,19939,030,0007,005,0002,482,176
10Oct 5, 2023 15:0079,54373,542,74643,127,0003,008,0002,506,752
11Oct 5, 2023 15:0079,90773,449,67841,309,0005,037,0002,326,528
12Oct 5, 2023 15:0080,15270,274,63741,435,0005,053,0002,453,504