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 5, 2023 09:00__Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 5, 2023 09:0100000Error: Killed by timeout
2Dec 5, 2023 09:0100000Error: Killed by timeout
3Dec 5, 2023 09:0100000Error: Killed by timeout
4Dec 5, 2023 09:0100000Error: expected "2531447 ", got "2535407"
5Dec 5, 2023 09:0100000Error: Killed by timeout
6Dec 5, 2023 09:0100000Error: expected "3112037 ", got "3144727"
7Dec 5, 2023 09:0000000Error: Killed by timeout
8Dec 5, 2023 09:0159,647104,540,00734,595,42802,310,144
9Dec 5, 2023 09:0159,678104,492,05434,613,36602,310,144
10Dec 5, 2023 09:0160,932101,797,14335,340,77602,199,552
11Dec 5, 2023 09:0161,106113,255,68235,441,68502,187,264
12Dec 5, 2023 09:0161,316108,145,92735,563,48202,187,264
13Dec 5, 2023 09:0062,003101,362,91335,961,71102,256,896
14Dec 5, 2023 09:0162,04092,294,40235,982,93202,187,264
15Dec 5, 2023 09:0062,067103,178,90135,998,71702,187,264
16Dec 5, 2023 09:0062,068123,105,94035,999,30502,191,360
17Dec 5, 2023 09:0163,869104,116,95737,043,91102,199,552
18Dec 5, 2023 09:0164,540100,769,99337,433,38102,301,952
19Dec 5, 2023 09:0164,93698,725,17237,663,13302,314,240