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 18, 2022 05:55Istomin MikhailIstomin MikhailScore: 83,326Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 18, 2022 05:5561,99062,931,25834,956,000998,0002,293,760
2Feb 18, 2022 05:5562,34366,536,91434,151,0002,008,0002,486,272
3Feb 18, 2022 05:5563,74362,682,63934,973,0001,998,0002,486,272
4Feb 18, 2022 05:5563,90761,380,85435,063,0002,003,0002,289,664
5Feb 18, 2022 05:5564,69861,488,76137,525,00002,486,272
6Feb 18, 2022 05:5564,88864,887,57637,635,00002,490,368
7Feb 18, 2022 05:5567,79861,748,35437,307,0002,016,0002,473,984
8Feb 18, 2022 05:5568,81265,553,05136,918,0002,993,0002,473,984
9Feb 18, 2022 05:5576,91673,442,36742,584,0002,027,0002,510,848
10Feb 18, 2022 05:5582,60079,116,83545,912,0001,996,0002,285,568
11Feb 18, 2022 05:5583,326131,259,70748,329,16302,187,264
12Feb 18, 2022 05:5583,861109,128,48248,639,37002,256,896
13Feb 18, 2022 05:5584,945124,244,99549,267,92402,244,608
14Feb 18, 2022 05:5586,32678,190,40347,065,0003,004,0002,461,696
15Feb 18, 2022 05:5587,02876,233,31248,457,0002,019,0002,285,568
16Feb 18, 2022 05:5587,69175,433,79945,875,0004,986,0002,461,696
17Feb 18, 2022 05:5590,41776,657,57449,417,0003,025,0002,441,216
18Feb 18, 2022 05:5592,27279,146,96550,489,0003,029,0002,473,984
19Feb 18, 2022 05:5598,17980,998,11052,948,0003,996,0002,301,952
20Feb 18, 2022 05:5598,73878,895,71854,254,0003,014,0002,441,216
21Feb 18, 2022 05:55100,35080,554,42955,193,0003,010,0002,445,312