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 9, 2022 13:52Istomin MikhailIstomin MikhailScore: 750,153Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 9, 2022 13:50477,884355,305,187277,172,89902,183,168
2Feb 9, 2022 13:50524,949362,866,248304,470,59202,174,976
3Feb 9, 2022 13:51567,819407,479,203329,335,22702,179,072
4Feb 9, 2022 13:52578,545391,298,786335,556,27302,301,952
5Feb 9, 2022 13:51585,057412,957,397339,332,87202,199,552
6Feb 9, 2022 13:51629,833429,226,398365,303,16902,195,456
7Feb 9, 2022 13:51634,858436,763,822368,217,61902,244,608
8Feb 9, 2022 13:51670,183445,039,394388,705,98802,301,952
9Feb 9, 2022 13:51699,096468,614,998405,475,51202,191,360
10Feb 9, 2022 13:51750,153499,031,540435,088,51202,174,976
11Feb 9, 2022 13:51765,140501,646,089443,781,08402,179,072
12Feb 9, 2022 13:52804,242531,906,616466,460,39302,174,976
13Feb 9, 2022 13:50807,899522,281,927468,581,35902,203,648
14Feb 9, 2022 13:52826,077553,069,312479,124,60502,256,896
15Feb 9, 2022 13:52844,488544,113,759489,803,06302,195,456
16Feb 9, 2022 13:52849,559562,657,481492,744,39702,195,456
17Feb 9, 2022 13:51876,721570,044,632508,498,12802,248,704
18Feb 9, 2022 13:52930,189605,229,136539,509,45202,179,072