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 listFeb 18, 2026 23:37Josu San MartinJosu San MartinScore: 4,583Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 18, 2026 23:384,27221,865,3792,478,00002,306,048
2Feb 18, 2026 23:374,27618,828,8111,240,0001,240,0002,293,760
3Feb 18, 2026 23:374,33119,187,4322,512,00002,293,760
4Feb 18, 2026 23:384,44517,138,4471,289,0001,289,0002,490,368
5Feb 18, 2026 23:384,51722,518,4462,620,00002,285,568
6Feb 18, 2026 23:384,51720,389,7492,620,00002,527,232
7Feb 18, 2026 23:384,52116,904,0522,622,00002,433,024
8Feb 18, 2026 23:384,52821,333,7851,313,0001,313,0002,473,984
9Feb 18, 2026 23:374,55918,422,4892,644,00002,486,272
10Feb 18, 2026 23:374,58319,910,7061,329,0001,329,0002,449,408
11Feb 18, 2026 23:374,59326,806,6512,664,00002,412,544
12Feb 18, 2026 23:374,59821,913,0422,667,00002,404,352
13Feb 18, 2026 23:374,60020,034,6482,668,00002,424,832
14Feb 18, 2026 23:384,62615,988,4852,683,00002,437,120
15Feb 18, 2026 23:374,67422,801,3312,711,00002,277,376
16Feb 18, 2026 23:374,68319,445,3901,358,0001,358,0002,273,280
17Feb 18, 2026 23:384,68624,462,5202,718,00002,510,848
18Feb 18, 2026 23:384,82820,312,7531,400,0001,400,0002,523,136