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 17:54__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 17:5600000Error: expected "2126251 ", got "2122554"
2Feb 15, 2024 17:5500000Error: No answer to stdout. Died or killed?
3Feb 15, 2024 17:5500000Error: expected "538532 ", got "537020"
4Feb 15, 2024 17:5500000Error: expected "818520 ", got "800273"
5Feb 15, 2024 17:5500000Error: expected "712304 ", got "704997"
6Feb 15, 2024 17:5500000Error: expected "820618 ", got "805897"
7Feb 15, 2024 17:5500000Error: No answer to stdout. Died or killed?
8Feb 15, 2024 17:5500000Error: expected "867499 ", got "865889"
9Feb 15, 2024 17:5500000Error: expected "1187407 ", got "1170714"
10Feb 15, 2024 17:5600000Error: No answer to stdout. Died or killed?
11Feb 15, 2024 17:5500000Error: No answer to stdout. Died or killed?
12Feb 15, 2024 17:5600000Error: expected "2334084 ", got "2295497"
13Feb 15, 2024 17:5600000Error: expected "2010016 ", got "2000264"
14Feb 15, 2024 17:5600000Error: No answer to stdout. Died or killed?
15Feb 15, 2024 17:5600000Error: No answer to stdout. Died or killed?
16Feb 15, 2024 17:5600000Error: expected "1508191 ", got "1511194"
17Feb 15, 2024 17:5600000Error: expected "2630238 ", got "2623593"
18Feb 15, 2024 17:5600000Error: expected "1322323 ", got "1323836"
19Feb 15, 2024 17:5700000Error: expected "2203100 ", got "2166461"
20Feb 15, 2024 17:5700000Error: expected "556858 ", got "547310"
21Feb 15, 2024 17:5400000Error: expected "1783337 ", got "1787355"
22Feb 15, 2024 17:5400000Error: No answer to stdout. Died or killed?
23Feb 15, 2024 17:5400000Error: expected "2351254 ", got "2336502"
24Feb 15, 2024 17:5400000Error: expected "3263602 ", got "3283753"
25Feb 15, 2024 17:5400000Error: No answer to stdout. Died or killed?
26Feb 15, 2024 17:5400000Error: expected "833578 ", got "830014"
27Feb 15, 2024 17:5400000Error: expected "1552874 ", got "1556529"
28Feb 15, 2024 17:5400000Error: No answer to stdout. Died or killed?
29Feb 15, 2024 17:5400000Error: expected "1266319 ", got "1403247"
30Feb 15, 2024 17:5400000Error: expected "1457022 ", got "1458634"
31Feb 15, 2024 17:5400000Error: expected "2074496 ", got "2088790"
32Feb 15, 2024 17:5400000Error: No answer to stdout. Died or killed?
33Feb 15, 2024 17:5400000Error: expected "680006 ", got "668756"
34Feb 15, 2024 17:5400000Error: No answer to stdout. Died or killed?
35Feb 15, 2024 17:5400000Error: expected "1738223 ", got "1736054"
36Feb 15, 2024 17:5400000Error: expected "1243128 ", got "1242621"
37Feb 15, 2024 17:5400000Error: expected "2313072 ", got "2260474"
38Feb 15, 2024 17:5500000Error: expected "1073614 ", got "1071340"
39Feb 15, 2024 17:5500000Error: expected "983562 ", got "982549"
40Feb 15, 2024 17:5500000Error: expected "1587775 ", got "1636026"