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 11, 2022 17:27Istomin MikhailIstomin MikhailScore: 149,142Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 11, 2022 17:26129,83896,730,71373,298,0002,008,0002,510,848
2Feb 11, 2022 17:26130,493110,780,82974,677,0001,009,0002,297,856
3Feb 11, 2022 17:27130,793102,110,32272,866,0002,994,0002,506,752
4Feb 11, 2022 17:28133,264103,927,11274,282,0003,011,0002,510,848
5Feb 11, 2022 17:26133,37299,144,42275,347,0002,009,0002,506,752
6Feb 11, 2022 17:28134,343103,445,34574,923,0002,996,0002,355,200
7Feb 11, 2022 17:26134,41498,043,70374,962,0002,998,0002,306,048
8Feb 11, 2022 17:27136,240101,491,84976,019,0003,000,0002,506,752
9Feb 11, 2022 17:27139,147106,653,40178,713,0001,992,0002,285,568
10Feb 11, 2022 17:26139,993106,006,02679,192,0002,004,0002,506,752
11Feb 11, 2022 17:28143,121111,840,25381,010,0002,000,0002,289,664
12Feb 11, 2022 17:26145,324104,745,13681,278,0003,010,0002,297,856
13Feb 11, 2022 17:26149,142137,085,18086,502,21102,211,840
14Feb 11, 2022 17:27149,389147,787,59986,645,34102,224,128
15Feb 11, 2022 17:26149,903150,946,01486,943,93002,220,032
16Feb 11, 2022 17:27150,140161,966,16987,081,22902,224,128
17Feb 11, 2022 17:26150,304144,340,46887,176,04902,224,128
18Feb 11, 2022 17:28150,361151,231,59187,209,64802,256,896
19Feb 11, 2022 17:28151,330146,205,85187,771,11202,224,128
20Feb 11, 2022 17:28151,492153,616,94587,865,35102,252,800
21Feb 11, 2022 17:27156,126154,530,30290,553,09802,215,936
22Feb 11, 2022 17:26156,907147,998,73391,006,21302,224,128
23Feb 11, 2022 17:26159,059168,313,91192,254,23302,211,840
24Feb 11, 2022 17:26159,862156,832,04592,719,90502,207,744