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 listFeb 16, 2024 06:14Tejas G.Tejas G.Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 17, 2024 00:5700000Error: expected "1103690 ", got "1121867 "
2Feb 16, 2024 13:4400000Error: expected "842791 ", got "896527 "
3Feb 16, 2024 13:4400000Error: expected "1345914 ", got "1666724 "
4Feb 16, 2024 13:4400000Error: expected "1870571 ", got "1901731 "
5Feb 16, 2024 13:4400000Error: expected "1021860 ", got "1160241 "
6Feb 16, 2024 13:4400000Error: expected "1309538 ", got "1733855 "
7Feb 17, 2024 00:5700000Error: expected "2281489 ", got "2434115 "
8Feb 17, 2024 00:5700000Error: expected "999276 ", got "1481952 "
9Feb 17, 2024 00:5700000Error: expected "2106086 ", got "2394512 "
10Feb 16, 2024 06:1500000Error: expected "1875218 ", got "2270965 "
11Feb 17, 2024 00:5700000Error: expected "2618937 ", got "3130863 "
12Feb 17, 2024 00:5700000Error: expected "1515731 ", got "1520243 "
13Feb 17, 2024 00:5700000Error: expected "2907923 ", got "2918380 "
14Feb 17, 2024 00:5800000Error: expected "2248793 ", got "2249177 "
15Feb 17, 2024 00:5800000Error: expected "1373334 ", got "1376534 "
16Feb 17, 2024 00:5800000Error: expected "2352400 ", got "2489237 "
17Feb 17, 2024 00:5800000Error: expected "3112234 ", got "3112342 "
18Feb 17, 2024 00:5800000Error: expected "1682736 ", got "1691613 "
19Feb 16, 2024 06:1500000Error: expected "992121 ", got "1135570 "
20Feb 16, 2024 06:1400000Error: expected "985233 ", got "999164 "
21Feb 16, 2024 06:1400000Error: expected "756693 ", got "773994 "
22Feb 16, 2024 06:1400000Error: expected "1426896 ", got "1455249 "
23Feb 16, 2024 06:1400000Error: expected "978942 ", got "1829752 "
24Feb 16, 2024 06:1400000Error: expected "1909230 ", got "1911771 "
25Feb 16, 2024 06:1400000Error: expected "1084086 ", got "1206727 "
26Feb 16, 2024 06:1500000Error: expected "1457900 ", got "1855032 "
27Feb 16, 2024 06:1436,32587,997,55821,068,77202,248,704
28Feb 16, 2024 13:4436,81891,730,64221,354,15902,252,800
29Feb 16, 2024 13:4437,04469,589,36421,485,68502,256,896
30Feb 16, 2024 06:1437,682103,277,69621,855,41602,256,896
31Feb 17, 2024 00:5837,850101,292,77621,953,28202,256,896
32Feb 16, 2024 06:1438,02177,177,45622,052,20602,252,800
33Feb 17, 2024 00:5838,06095,019,61122,075,03302,260,992
34Feb 16, 2024 13:4438,08489,288,46822,088,97002,244,608
35Feb 17, 2024 00:5838,61990,198,12122,398,98202,244,608