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 22, 2021 17:20Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 22, 2021 17:2100000Error: expected "3229950 ", got "3235952 "
2Dec 22, 2021 17:2000000Error: expected "1664638 ", got "1674941 "
3Dec 22, 2021 17:2274,039101,068,08042,942,87902,297,856
4Dec 22, 2021 17:2174,157111,773,15943,011,27002,285,568
5Dec 22, 2021 17:2274,160109,072,39643,012,77802,293,760
6Dec 22, 2021 17:2174,165101,859,38943,015,88302,273,280
7Dec 22, 2021 17:2074,360108,263,74243,128,79202,269,184
8Dec 22, 2021 17:2274,362122,910,02343,130,00602,256,896
9Dec 22, 2021 17:2074,373106,340,60443,136,49702,269,184
10Dec 22, 2021 17:2174,389110,275,21343,145,76802,281,472
11Dec 22, 2021 17:2074,657119,695,12743,301,14502,244,608
12Dec 22, 2021 17:2275,08163,968,04043,547,00002,506,752
13Dec 22, 2021 17:2075,15966,251,95042,579,0001,013,0002,502,656
14Dec 22, 2021 17:2075,17670,386,42043,602,00002,498,560
15Dec 22, 2021 17:2275,19865,802,88343,615,00002,514,944
16Dec 22, 2021 17:2075,27473,620,31043,659,00002,306,048
17Dec 22, 2021 17:2275,32974,618,44742,675,0001,016,0002,371,584
18Dec 22, 2021 17:2275,37168,971,38342,699,0001,016,0002,531,328
19Dec 22, 2021 17:2275,60272,651,76540,860,0002,989,0002,338,816
20Dec 22, 2021 17:2075,62166,201,56042,864,000996,0002,478,080
21Dec 22, 2021 17:2275,67473,165,89642,894,000997,0002,502,656
22Dec 22, 2021 17:2075,67669,922,28543,892,00002,355,200
23Dec 22, 2021 17:2275,67971,570,66441,899,0001,995,0002,326,528
24Dec 22, 2021 17:2075,70271,075,18241,912,0001,995,0002,498,560
25Dec 22, 2021 17:2075,71769,350,09739,924,0003,992,0002,322,432
26Dec 22, 2021 17:2275,80971,611,93741,971,0001,998,0002,306,048
27Dec 22, 2021 17:2275,84569,483,83140,991,0002,999,0002,498,560
28Dec 22, 2021 17:2075,92269,758,96042,034,0002,001,0002,306,048
29Dec 22, 2021 17:2076,07970,082,86441,118,0003,008,0002,355,200