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 27, 2024 10:51Louis PonetLouis PonetScore: 286,648Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 27, 2024 10:51285,838192,877,483153,802,00011,984,0002,293,760
2Dec 27, 2024 10:51286,338195,390,604153,070,00013,006,0002,387,968
3Dec 27, 2024 10:51286,362191,608,717151,082,00015,008,0002,514,944
4Dec 27, 2024 10:51286,372195,546,509154,089,00012,007,0002,433,024
5Dec 27, 2024 10:51286,648190,634,293153,236,00013,020,0002,297,856
6Dec 27, 2024 10:51286,771197,456,347153,302,00013,025,0002,269,184
7Dec 27, 2024 10:51287,624197,620,215151,838,00014,984,0002,342,912
8Dec 27, 2024 10:51287,921197,806,766155,995,00010,999,0002,318,336
9Dec 27, 2024 10:51288,543194,907,098156,332,00011,023,0002,514,944