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 listDec 19, 2024 03:27yenw0dyenw0dError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 19, 2024 03:3600000Error: expected "2764656 ", got "0 "stderr
2Dec 19, 2024 03:4300000Error: expected "1805271 ", got "0 "stderr
3Dec 19, 2024 03:4200000Error: expected "2337063 ", got "0 "stderr
4Dec 19, 2024 03:4200000Error: expected "1563709 ", got "0 "stderr
5Dec 19, 2024 03:4100000Error: expected "1291742 ", got "0 "stderr
6Dec 19, 2024 03:4000000Error: expected "1476014 ", got "0 "stderr
7Dec 19, 2024 03:3900000Error: expected "1450208 ", got "0 "stderr
8Dec 19, 2024 03:3900000Error: expected "1004533 ", got "382935 "stderr
9Dec 19, 2024 03:3800000Error: expected "1423256 ", got "0 "stderr
10Dec 19, 2024 03:3800000Error: expected "1992087 ", got "0 "stderr
11Dec 19, 2024 03:3700000Error: expected "1312466 ", got "0 "stderr
12Dec 19, 2024 03:2700000Error: expected "1081803 ", got "0 "stderr
13Dec 19, 2024 03:3500000Error: expected "2045312 ", got "0 "stderr
14Dec 19, 2024 03:3400000Error: expected "1982488 ", got "0 "stderr
15Dec 19, 2024 03:3200000Error: expected "1603107 ", got "0 "stderr
16Dec 19, 2024 03:3100000Error: expected "2636821 ", got "0 "stderr
17Dec 19, 2024 03:3000000Error: expected "1153833 ", got "43426 "stderr
18Dec 19, 2024 03:2900000Error: expected "1519114 ", got "0 "stderr
19Dec 19, 2024 03:2900000Error: expected "2245591 ", got "0 "stderr
20Dec 19, 2024 03:2800000Error: expected "3184157 ", got "0 "stderr
21Dec 19, 2024 03:2800000Error: expected "1624303 ", got "0 "stderr