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 21, 2025 00:07Josu San MartinJosu San MartinScore: 37,059Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 00:0735,30044,889,22519,451,0001,023,0002,510,848
2Dec 21, 2025 00:0235,40339,622,88019,508,0001,026,0002,342,912
3Dec 21, 2025 00:0235,52644,342,60620,605,00002,457,600
4Dec 21, 2025 00:0236,06448,143,21819,921,000996,0002,506,752
5Dec 21, 2025 00:0236,72942,568,51920,289,0001,014,0002,273,280
6Dec 21, 2025 00:0236,74147,849,71420,296,0001,014,0002,273,280
7Dec 21, 2025 00:0737,05943,097,80620,471,0001,023,0002,342,912
8Dec 21, 2025 00:0237,31640,480,75019,582,0002,061,0002,453,504
9Dec 21, 2025 00:0737,31950,013,75620,615,0001,030,0002,469,888
10Dec 21, 2025 00:0237,44752,676,23420,685,0001,034,0002,400,256
11Dec 21, 2025 00:0237,50351,844,01621,752,00002,387,968
12Dec 21, 2025 00:0237,78352,785,06421,914,00002,420,736