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 listDec 20, 2024 19:02Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2024 19:0500000Error: expected "1198699 ", got "1230759 "
2Dec 20, 2024 19:0500000Error: expected "1900608 ", got "1903836 "
3Dec 20, 2024 19:0400000Error: expected "1200491 ", got "1200853 "
4Dec 20, 2024 19:0300000Error: expected "713035 ", got "714007 "
5Dec 20, 2024 19:0300000Error: expected "1071000 ", got "1073997 "
6Dec 20, 2024 19:0200000Error: expected "1660960 ", got "1663570 "
7Dec 20, 2024 19:0332,59847,287,27317,912,000995,0002,277,376
8Dec 20, 2024 19:0332,59844,287,71817,912,000995,0002,387,968
9Dec 20, 2024 19:0332,61747,853,28717,923,000995,0002,285,568
10Dec 20, 2024 19:0532,63444,476,16716,936,0001,992,0002,277,376
11Dec 20, 2024 19:0432,65743,435,34816,948,0001,993,0002,269,184
12Dec 20, 2024 19:0332,66240,605,54617,947,000997,0002,269,184
13Dec 20, 2024 19:0332,76946,652,56917,006,0002,000,0002,285,568
14Dec 20, 2024 19:0432,77632,976,49218,010,0001,000,0002,285,568
15Dec 20, 2024 19:0532,77841,033,29217,010,0002,001,0002,285,568
16Dec 20, 2024 19:0332,79539,548,97917,019,0002,002,0002,285,568
17Dec 20, 2024 19:0432,83645,168,48117,041,0002,004,0002,273,280
18Dec 20, 2024 19:0532,83847,189,15617,042,0002,004,0002,277,376
19Dec 20, 2024 19:0532,86443,691,98118,058,0001,003,0002,371,584
20Dec 20, 2024 19:0332,89846,479,43517,073,0002,008,0002,285,568
21Dec 20, 2024 19:0532,92844,980,36618,093,0001,005,0002,285,568
22Dec 20, 2024 19:0332,93642,546,87217,093,0002,010,0002,285,568
23Dec 20, 2024 19:0333,00741,317,30217,129,0002,015,0002,269,184
24Dec 20, 2024 19:0533,05540,801,97118,163,0001,009,0002,269,184