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 listNov 27, 2024 19:53Joad NacerJoad NacerError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 27, 2024 19:5400000Error: expected "1998011 ", got "1986229 "stderr
2Nov 27, 2024 19:5600000Error: expected "1407112 ", got "1363117 "stderr
3Nov 27, 2024 19:5600000Error: expected "617148 ", got "615237 "stderr
4Nov 27, 2024 19:5600000Error: expected "904930 ", got "876682 "stderr
5Nov 27, 2024 19:5600000Error: expected "1566626 ", got "1440000 "stderr
6Nov 27, 2024 19:5600000Error: expected "2924584 ", got "2895000 "stderr
7Nov 27, 2024 19:5600000Error: expected "2236014 ", got "2186544 "stderr
8Nov 27, 2024 19:5500000Error: expected "1676922 ", got "1558000 "stderr
9Nov 27, 2024 19:5500000Error: expected "660302 ", got "630379 "stderr
10Nov 27, 2024 19:5500000Error: expected "757853 ", got "757545 "stderr
11Nov 27, 2024 19:5500000Error: expected "744959 ", got "679000 "stderr
12Nov 27, 2024 19:5500000Error: expected "2372672 ", got "2037000 "stderr
13Nov 27, 2024 19:5500000Error: expected "1753353 ", got "1647464 "stderr
14Nov 27, 2024 19:5400000Error: expected "1304782 ", got "1255000 "stderr
15Nov 27, 2024 19:5300000Error: expected "808650 ", got "731300 "stderr
16Nov 27, 2024 19:5300000Error: expected "1664156 ", got "1588000 "stderr
17Nov 27, 2024 19:5521,99345,660,42212,756,00002,314,240
18Nov 27, 2024 19:5524,49531,934,74113,193,0001,014,0002,465,792
19Nov 27, 2024 19:5525,75031,070,12813,940,000995,0002,420,736
20Nov 27, 2024 19:5530,57643,391,00417,734,00002,453,504
21Nov 27, 2024 19:5631,87650,733,48817,461,0001,027,0002,412,544
22Nov 27, 2024 19:5635,26648,512,17619,432,0001,022,0002,498,560
23Nov 27, 2024 19:5636,22949,216,89320,013,0001,000,0002,404,352
24Nov 27, 2024 19:5537,09149,018,30120,489,0001,024,0002,269,184
25Nov 27, 2024 19:5637,13149,595,39420,511,0001,025,0002,510,848
26Nov 27, 2024 19:5638,62938,637,12722,405,00002,514,944
27Nov 27, 2024 19:5539,49744,418,53422,908,00002,297,856
28Nov 27, 2024 19:5640,39049,290,97923,426,00002,310,144