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 listOct 17, 2024 09:06@drytecc@dryteccError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
2Oct 17, 2024 09:2100000Error: expected "1116582 ", got "1117365 "
3Oct 17, 2024 09:2100000Error: expected "1943079 ", got "1910035 "
4Oct 17, 2024 09:2100000Error: expected "2032510 ", got "827228 "
5Oct 17, 2024 09:2100000Error: expected "917546 ", got "918417 "
6Oct 17, 2024 09:0800000Error: No answer to stdout. Died or killed?
7Oct 17, 2024 09:0800000Error: No answer to stdout. Died or killed?
8Oct 17, 2024 09:0800000Error: No answer to stdout. Died or killed?
9Oct 17, 2024 09:0800000Error: No answer to stdout. Died or killed?
10Oct 17, 2024 09:0800000Error: No answer to stdout. Died or killed?
11Oct 17, 2024 09:0800000Error: No answer to stdout. Died or killed?
12Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
13Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
14Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
15Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
16Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
17Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
18Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
19Oct 17, 2024 09:0700000Error: No answer to stdout. Died or killed?
20Oct 17, 2024 09:0600000Error: No answer to stdout. Died or killed?
21Oct 17, 2024 09:0600000Error: No answer to stdout. Died or killed?
22Oct 17, 2024 09:0600000Error: No answer to stdout. Died or killed?
23Oct 17, 2024 09:2146,93350,167,87424,197,0003,024,0002,457,600
24Oct 17, 2024 09:2147,19849,789,86222,306,0005,069,0002,396,160
25Oct 17, 2024 09:2147,40750,782,76523,423,0004,073,0002,506,752
26Oct 17, 2024 09:2147,71055,467,81423,573,0004,099,0002,387,968
27Oct 17, 2024 09:2148,07153,818,52024,894,0002,987,0002,461,696
28Oct 17, 2024 09:2148,20953,040,53125,964,0001,997,0002,473,984