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 18:27__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 18:2700000Error: expected "579735 ", got "578703"
2Feb 15, 2024 18:2800000Error: expected "656462 ", got "650311"
3Feb 15, 2024 18:2800000Error: No answer to stdout. Died or killed?
4Feb 15, 2024 18:2800000Error: No answer to stdout. Died or killed?
5Feb 15, 2024 18:2800000Error: No answer to stdout. Died or killed?
6Feb 15, 2024 18:2800000Error: expected "1766170 ", got "1766400"
7Feb 15, 2024 18:2800000Error: expected "1218323 ", got "1211439"
8Feb 15, 2024 18:2800000Error: No answer to stdout. Died or killed?
9Feb 15, 2024 18:2800000Error: expected "1345986 ", got "1356186"
10Feb 15, 2024 18:2800000Error: No answer to stdout. Died or killed?
11Feb 15, 2024 18:2800000Error: No answer to stdout. Died or killed?
12Feb 15, 2024 18:2800000Error: expected "2902630 ", got "2901958"
13Feb 15, 2024 18:2700000Error: No answer to stdout. Died or killed?
14Feb 15, 2024 18:2700000Error: expected "968159 ", got "1074626"
15Feb 15, 2024 18:2700000Error: expected "1469273 ", got "1467532"
16Feb 15, 2024 18:2700000Error: No answer to stdout. Died or killed?
17Feb 15, 2024 18:2700000Error: expected "1870060 ", got "1919941"
18Feb 15, 2024 18:2700000Error: expected "753219 ", got "951279"
19Feb 15, 2024 18:2700000Error: expected "659528 ", got "659490"
20Feb 15, 2024 18:2843,684111,445,74525,336,78602,195,456
21Feb 15, 2024 18:2843,69664,945,79525,343,62402,199,552
22Feb 15, 2024 18:2843,93888,422,43925,483,82102,195,456
23Feb 15, 2024 18:2844,326100,112,03325,708,86202,191,360
24Feb 15, 2024 18:2844,42387,446,81625,765,58402,191,360
25Feb 15, 2024 18:2844,43487,171,27725,771,78202,191,360
26Feb 15, 2024 18:2844,47495,895,78225,795,10802,195,456
27Feb 15, 2024 18:2845,53687,984,12926,410,67702,191,360
28Feb 15, 2024 18:2845,99196,473,07926,674,94802,314,240