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 listFeb 9, 2022 12:51Istomin MikhailIstomin MikhailScore: 524,704Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 9, 2022 12:51317,155246,211,528183,950,16602,195,456
2Feb 9, 2022 12:52355,251270,533,595206,045,77202,191,360
3Feb 9, 2022 12:50387,245294,582,240224,602,04302,195,456
4Feb 9, 2022 12:50405,052297,814,849234,929,96302,203,648
5Feb 9, 2022 12:49444,727317,144,052257,941,75202,195,456
6Feb 9, 2022 12:49480,162336,693,593278,494,07802,195,456
7Feb 9, 2022 12:50485,916338,861,735281,831,11202,187,264
8Feb 9, 2022 12:52488,464345,512,274283,309,09902,207,744
9Feb 9, 2022 12:50499,213361,360,426289,543,32802,207,744
10Feb 9, 2022 12:51524,704362,680,536304,328,44002,191,360
11Feb 9, 2022 12:49536,755380,979,924311,317,65602,199,552
12Feb 9, 2022 12:50608,605419,773,863352,991,18402,199,552
13Feb 9, 2022 12:51682,839486,349,908396,046,69702,191,360
14Feb 9, 2022 12:51685,363459,381,013397,510,71402,256,896
15Feb 9, 2022 12:51745,428486,273,777432,347,95302,195,456
16Feb 9, 2022 12:51788,391520,670,187457,266,98102,199,552
17Feb 9, 2022 12:52790,832523,807,941458,682,82902,195,456
18Feb 9, 2022 12:50894,660587,746,391518,903,05102,199,552