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 listNov 22, 2021 21:29Yuriy LyfenkoYuriy LyfenkoScore: 58,778Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 22, 2021 21:2958,12496,464,79633,711,72702,248,704
2Nov 22, 2021 21:2958,139107,178,02333,720,60802,232,320
3Nov 22, 2021 21:2958,156100,330,38533,730,70302,240,512
4Nov 22, 2021 21:3158,19788,655,19533,754,09702,236,416
5Nov 22, 2021 21:3158,243107,242,01333,781,04102,240,512
6Nov 22, 2021 21:3158,30788,347,86033,818,01902,244,608
7Nov 22, 2021 21:3158,46057,422,87732,910,000997,0002,285,568
8Nov 22, 2021 21:3158,46260,970,09831,914,0001,994,0002,285,568
9Nov 22, 2021 21:2958,54857,093,54831,961,0001,997,0002,273,280
10Nov 22, 2021 21:3158,65256,664,23033,018,0001,000,0002,285,568
11Nov 22, 2021 21:2958,65762,722,64732,020,0002,001,0002,285,568
12Nov 22, 2021 21:3158,72855,511,90033,061,0001,001,0002,285,568
13Nov 22, 2021 21:2958,77853,374,21433,089,0001,002,0002,285,568
14Nov 22, 2021 21:2958,78455,859,15432,090,0002,005,0002,285,568
15Nov 22, 2021 21:3158,81460,101,98032,106,0002,006,0002,285,568
16Nov 22, 2021 21:2958,83463,102,67032,117,0002,007,0002,273,280
17Nov 22, 2021 21:2958,84357,407,82032,122,0002,007,0002,277,376
18Nov 22, 2021 21:3158,96754,295,77832,190,0002,011,0002,273,280
19Nov 22, 2021 21:3159,00957,536,94332,212,0002,013,0002,285,568
20Nov 22, 2021 21:2959,03156,197,48133,231,0001,007,0002,285,568
21Nov 22, 2021 21:3159,07863,806,66332,250,0002,015,0002,277,376
22Nov 22, 2021 21:2959,08658,623,89733,263,0001,007,0002,277,376
23Nov 22, 2021 21:2959,12461,968,26732,275,0002,017,0002,379,776
24Nov 22, 2021 21:3159,31264,743,75834,401,00002,285,568