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 15, 2024 16:32__Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 15, 2024 16:3600000Error: expected "605302 ", got "604699"
2Feb 15, 2024 16:3200000Error: No answer to stdout. Died or killed?
3Feb 15, 2024 16:3200000Error: expected "1303035 ", got "1320129"
4Feb 15, 2024 16:3200000Error: No answer to stdout. Died or killed?
5Feb 15, 2024 16:3200000Error: No answer to stdout. Died or killed?
6Feb 15, 2024 16:3200000Error: No answer to stdout. Died or killed?
7Feb 15, 2024 16:3200000Error: No answer to stdout. Died or killed?
8Feb 15, 2024 16:3200000Error: No answer to stdout. Died or killed?
9Feb 15, 2024 16:3300000Error: expected "664284 ", got "659782"
10Feb 15, 2024 16:3600000Error: No answer to stdout. Died or killed?
11Feb 15, 2024 16:3200000Error: No answer to stdout. Died or killed?
12Feb 15, 2024 17:0300000Error: expected "1708927 ", got "1709487"
13Feb 15, 2024 17:0300000Error: expected "1087115 ", got "1089761"
14Feb 15, 2024 17:0300000Error: No answer to stdout. Died or killed?
15Feb 15, 2024 17:0300000Error: expected "538841 ", got "540603"
16Feb 15, 2024 17:1600000Error: No answer to stdout. Died or killed?
17Feb 15, 2024 17:2800000Error: No answer to stdout. Died or killed?
18Feb 15, 2024 17:2800000Error: expected "2390021 ", got "2395165"
19Feb 15, 2024 17:2800000Error: expected "1022580 ", got "1023264"
20Feb 15, 2024 17:2800000Error: expected "1200732 ", got "1200498"
21Feb 15, 2024 16:3200000Error: expected "2364738 ", got "2364606"
22Feb 15, 2024 17:2849,75296,923,79728,855,97302,199,552
23Feb 15, 2024 17:2849,994110,769,97328,996,36102,195,456
24Feb 15, 2024 17:2850,16296,623,75729,093,75602,191,360
25Feb 15, 2024 16:3650,18189,749,24229,104,82702,187,264
26Feb 15, 2024 17:0350,27984,549,73529,161,58302,191,360
27Feb 15, 2024 16:3650,427107,037,59229,247,54002,187,264
28Feb 15, 2024 17:0350,745122,619,13229,432,21002,187,264
29Feb 15, 2024 16:3250,93387,800,50529,540,99502,191,360
30Feb 15, 2024 16:3650,997102,762,11929,578,13302,191,360
31Feb 15, 2024 16:3251,03694,844,65229,600,59102,183,168
32Feb 15, 2024 16:3251,09399,339,78229,634,03602,191,360
33Feb 15, 2024 17:1651,140102,723,89229,661,17502,256,896
34Feb 15, 2024 17:0351,225101,280,47429,710,37202,187,264
35Feb 15, 2024 17:1652,000112,188,18030,159,94202,183,168
36Feb 15, 2024 17:1652,143125,092,32730,243,14202,199,552
37Feb 15, 2024 17:1652,42892,458,27230,408,01102,260,992
38Feb 15, 2024 17:1653,43095,245,00330,989,15202,195,456
39Feb 15, 2024 17:1655,580112,995,81532,236,37902,256,896