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 listMar 2, 2026 22:03JuneLJuneLScore: 57,448Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 2, 2026 22:0756,05957,494,38531,498,0001,016,0002,437,120
2Mar 2, 2026 22:0356,25255,644,02931,607,0001,019,0002,424,832
3Mar 2, 2026 22:0356,72657,836,39331,904,000997,0002,400,256
4Mar 2, 2026 22:0356,72857,509,00130,908,0001,994,0002,514,944
5Mar 2, 2026 22:0757,02262,618,29031,069,0002,004,0002,498,560
6Mar 2, 2026 22:0757,16055,291,35132,149,0001,004,0002,424,832
7Mar 2, 2026 22:0357,36055,249,40132,261,0001,008,0002,490,368
8Mar 2, 2026 22:0357,44861,102,20431,301,0002,019,0002,433,024
9Mar 2, 2026 22:0757,72661,946,57832,467,0001,014,0002,392,064
10Mar 2, 2026 22:0357,82855,072,20833,540,00002,269,184
11Mar 2, 2026 22:0758,02462,684,82833,654,00002,408,448
12Mar 2, 2026 22:0358,19566,055,53132,731,0001,022,0002,416,640
13Mar 2, 2026 22:0758,42160,889,54932,888,000996,0002,404,352
14Mar 2, 2026 22:0358,44060,860,63632,899,000996,0002,416,640
15Mar 2, 2026 22:0358,83455,778,24733,121,0001,003,0002,375,680