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 3, 2023 17:52Taiwan QuantTaiwan QuantScore: 122,503Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 3, 2023 17:52122,16296,754,91868,859,0001,995,0002,396,160
2Dec 3, 2023 17:52122,19796,612,40768,878,0001,996,0002,408,448
3Dec 3, 2023 17:52122,207102,085,98569,882,000998,0002,482,176
4Dec 3, 2023 17:52122,30397,213,82768,938,0001,998,0002,445,312
5Dec 3, 2023 17:52122,32897,077,33069,951,000999,0002,428,928
6Dec 3, 2023 17:52122,49594,437,49870,047,0001,000,0002,461,696
7Dec 3, 2023 17:52122,50399,131,27669,051,0002,001,0002,506,752
8Dec 3, 2023 17:52122,512100,169,23769,056,0002,001,0002,449,408
9Dec 3, 2023 17:52122,82696,957,24470,236,0001,003,0002,473,984
10Dec 3, 2023 17:52161,408155,017,01093,616,36002,285,568
11Dec 3, 2023 17:52161,453154,129,72393,642,70902,273,280
12Dec 3, 2023 17:52167,482152,084,04697,139,54502,269,184