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 24, 2026 20:17Jem BishopJem BishopError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Apr 28, 2026 11:54Unays 10.58RecheckSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 28, 2026 11:54021,833,31401,856,0002,396,160expected "1684325 ", got "1688835 "
2Apr 28, 2026 11:54017,155,01001,985,0002,404,352expected "1716192 ", got "1713418 "
3Apr 28, 2026 11:54021,847,363978,000978,0002,424,832expected "1425281 ", got "1418549 "
4Feb 24, 2026 21:322,88817,641,2951,675,00002,433,024
5Feb 24, 2026 21:322,97419,114,27401,725,0002,310,144
6Feb 24, 2026 20:173,13317,361,34301,817,0002,473,984
7Feb 24, 2026 19:463,14520,934,7901,824,00002,457,600
8Feb 24, 2026 19:463,19821,501,8261,855,00002,424,832
9Feb 24, 2026 19:463,25917,602,15801,890,0002,461,696
10Feb 24, 2026 20:173,27917,650,998951,000951,0002,473,984
11Feb 24, 2026 20:173,30920,226,37101,919,0002,367,488
12Feb 24, 2026 21:323,38318,159,920981,000981,0002,420,736