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 listOct 18, 2026 09:01KacpxerKacpxerError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 25, 2026 16:12Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 25, 2026 16:140128,856,265110,635,0006,273,0002,113,536expected "867794112 ", got "943000030 "
2Oct 25, 2026 16:140128,763,137111,534,0006,363,0002,113,536expected "865846232 ", got "30 "
3Oct 25, 2026 16:140127,788,489110,715,0006,254,0002,113,536expected "876704340 ", got "943000030 "
4Oct 18, 2026 09:0239,76833,758,17521,918,0001,148,0002,113,536
5Oct 18, 2026 09:0240,05133,851,56522,002,0001,228,0002,113,536
6Oct 18, 2026 09:0241,07734,483,85222,588,0001,237,0002,113,536
7Oct 18, 2026 09:0242,64635,406,73723,480,0001,255,0002,113,536
8Oct 18, 2026 09:0242,76235,482,37723,547,0001,255,0002,113,536
9Oct 18, 2026 09:0242,97035,770,86023,751,0001,172,0002,113,536
10Oct 18, 2026 09:0243,02235,605,18723,680,0001,273,0002,113,536
11Oct 18, 2026 09:0244,59436,974,44724,606,0001,259,0002,113,536
12Oct 18, 2026 09:0245,20636,837,78424,962,0001,258,0002,113,536