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 16, 2025 04:48HattonuriHattonuriScore: 41,260Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 16, 2025 04:4938,78845,198,38821,475,0001,022,0002,527,232
2Dec 16, 2025 04:4939,53847,150,08222,932,00002,523,136
3Dec 16, 2025 04:4939,91448,132,94522,144,0001,006,0002,269,184
4Dec 16, 2025 04:4840,06754,664,45521,219,0002,020,0002,510,848
5Dec 16, 2025 04:4940,63445,416,82823,568,00002,519,040
6Dec 16, 2025 04:4940,92844,751,56122,706,0001,032,0002,269,184
7Dec 16, 2025 04:4940,93449,392,71022,710,0001,032,0002,269,184
8Dec 16, 2025 04:4941,26047,061,44121,937,0001,994,0002,269,184
9Dec 16, 2025 04:4941,69547,139,54722,168,0002,015,0002,531,328
10Dec 16, 2025 04:4942,13344,963,11323,419,0001,018,0002,531,328
11Dec 16, 2025 04:4942,39342,043,85524,588,00002,523,136
12Dec 16, 2025 04:4942,45046,376,18323,596,0001,025,0002,531,328
13Dec 16, 2025 04:4842,94049,535,09323,909,000996,0002,531,328
14Dec 16, 2025 04:4843,27845,950,36624,097,0001,004,0002,527,232
15Dec 16, 2025 04:4943,45747,966,80123,189,0002,016,0002,531,328