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 listJan 19, 2022 20:21Yuriy 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
1Jan 19, 2022 20:4700000Error: expected "1934818 ", got "1937307 "
2Jan 19, 2022 20:4874,048116,359,73342,948,07602,174,976
3Jan 19, 2022 20:2174,108112,923,76542,982,44102,269,184
4Jan 19, 2022 20:4874,33396,768,93643,112,90902,277,376
5Jan 19, 2022 20:4874,485135,499,61443,201,20202,269,184
6Jan 19, 2022 20:2174,627115,061,79643,283,70402,265,088
7Jan 19, 2022 20:2174,673110,249,56443,310,17102,240,512
8Jan 19, 2022 20:4875,10366,539,33642,547,0001,013,0002,514,944
9Jan 19, 2022 20:2175,11964,436,62842,556,0001,013,0002,502,656
10Jan 19, 2022 20:4875,12170,866,45941,544,0002,026,0002,494,464
11Jan 19, 2022 20:2175,19365,217,12043,612,00002,498,560
12Jan 19, 2022 20:2175,25770,735,51642,634,0001,015,0002,539,520
13Jan 19, 2022 20:4875,27471,452,00542,644,0001,015,0002,326,528
14Jan 19, 2022 20:2175,27868,132,29742,646,0001,015,0002,285,568
15Jan 19, 2022 20:4875,37869,842,18343,719,00002,412,544
16Jan 19, 2022 20:4875,44066,774,27243,755,00002,363,392
17Jan 19, 2022 20:2175,55370,441,60941,830,0001,991,0002,301,952
18Jan 19, 2022 20:4875,67268,766,73741,895,0001,995,0002,490,368
19Jan 19, 2022 20:2175,67471,029,26640,899,0002,992,0002,465,792
20Jan 19, 2022 20:2175,67477,717,34442,894,000997,0002,297,856
21Jan 19, 2022 20:4875,68173,371,10641,900,0001,995,0002,330,624
22Jan 19, 2022 20:2175,68678,389,36040,905,0002,993,0002,494,464
23Jan 19, 2022 20:2175,75372,839,69041,940,0001,997,0002,486,272
24Jan 19, 2022 20:4875,75770,248,30741,942,0001,997,0002,375,680
25Jan 19, 2022 20:4876,17472,226,12442,173,0002,008,0002,473,984