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 15, 2024 21:31@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 15, 2024 21:3100000Error: expected "1022661 ", got "1022757 "
2Oct 15, 2024 21:3100000Error: expected "1244593 ", got "1242282 "
3Oct 15, 2024 21:3100000Error: expected "1139473 ", got "1165852 "
4Oct 15, 2024 21:3100000Error: expected "685148 ", got "706325 "
5Oct 15, 2024 21:3100000Error: expected "968471 ", got "1033424 "
6Oct 15, 2024 21:3100000Error: expected "1685453 ", got "1731087 "
7Oct 15, 2024 21:3100000Error: expected "2001236 ", got "2004207 "
8Oct 15, 2024 21:3100000Error: expected "2420050 ", got "2500878 "
9Oct 15, 2024 21:3100000Error: expected "2244344 ", got "2258262 "
10Oct 15, 2024 21:3200000Error: expected "1860845 ", got "1947631 "
11Oct 15, 2024 21:3200000Error: expected "925592 ", got "998288 "
12Oct 15, 2024 21:3200000Error: expected "2059700 ", got "2062325 "
13Oct 15, 2024 21:3200000Error: expected "2287063 ", got "2289207 "
14Oct 15, 2024 21:3200000Error: expected "1916537 ", got "1913115 "
15Oct 15, 2024 21:3200000Error: expected "2389232 ", got "2406315 "
16Oct 15, 2024 21:3154,68860,050,69330,696,0001,023,0002,514,944
17Oct 15, 2024 21:3255,15761,883,50929,992,0001,999,0002,469,888
18Oct 15, 2024 21:3355,18655,386,96430,008,0002,000,0002,392,064
19Oct 15, 2024 21:3155,29156,623,77428,061,0004,008,0002,445,312
20Oct 15, 2024 21:3155,33357,627,47230,088,0002,005,0002,293,760
21Oct 15, 2024 21:3155,56754,790,16628,201,0004,028,0002,514,944
22Oct 15, 2024 21:3155,64758,343,53429,250,0003,025,0002,289,664
23Oct 15, 2024 21:3255,67154,760,99429,262,0003,027,0002,428,928
24Oct 15, 2024 21:3255,67461,969,36731,282,0001,009,0002,469,888
25Oct 15, 2024 21:3155,68154,595,02829,268,0003,027,0002,469,888
26Oct 15, 2024 21:3155,74753,719,11428,292,0004,041,0002,469,888
27Oct 15, 2024 21:3155,79555,534,74829,328,0003,033,0002,469,888
28Oct 15, 2024 21:3355,88661,184,46828,363,0004,051,0002,469,888
29Oct 15, 2024 21:3255,93157,295,25628,385,0004,055,0002,469,888
30Oct 15, 2024 21:3256,04563,072,64730,475,0002,031,0002,277,376
31Oct 15, 2024 21:3156,04756,216,68130,476,0002,031,0002,351,104
32Oct 15, 2024 21:3356,08155,740,89229,478,0003,049,0002,465,792
33Oct 15, 2024 21:3256,75557,320,88330,923,0001,995,0002,326,528