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 23, 2025 04:15Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 21:12Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 21:23010,646,657783,000020,480No answer to stdout. Died or killed?
2Dec 23, 2025 04:1536,05748,988,90218,922,0001,991,0002,420,736
3Dec 23, 2025 04:1536,48643,672,46319,147,0002,015,0002,322,432
4Dec 23, 2025 04:1536,68443,137,28819,251,0002,026,0002,498,560
5Dec 23, 2025 04:1537,04543,894,66520,463,0001,023,0002,506,752
6Dec 23, 2025 04:1537,10244,851,85021,519,00002,293,760
7Dec 23, 2025 04:1537,17944,208,61420,538,0001,026,0002,400,256
8Dec 23, 2025 04:1537,19347,812,52320,545,0001,027,0002,416,640
9Dec 23, 2025 04:1537,24742,863,67220,575,0001,028,0002,387,968
10Dec 23, 2025 04:1537,31644,877,16919,582,0002,061,0002,310,144
11Dec 23, 2025 04:1537,34849,043,81220,631,0001,031,0002,396,160
12Dec 23, 2025 04:1537,40950,089,91120,664,0001,033,0002,498,560
13Dec 23, 2025 04:1537,56651,940,93920,751,0001,037,0002,486,272