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

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:29Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:40010,732,493814,000020,480No answer to stdout. Died or killed?
2Dec 21, 2025 04:2535,67146,111,14419,655,0001,034,0002,510,848
3Dec 21, 2025 04:2436,03348,612,22619,904,000995,0002,510,848
4Dec 21, 2025 04:2536,07651,314,88118,932,0001,992,0002,510,848
5Dec 21, 2025 04:2436,14544,326,30518,968,0001,996,0002,498,560
6Dec 21, 2025 04:2536,24050,036,66519,018,0002,001,0002,457,600
7Dec 21, 2025 04:2536,32446,683,75119,062,0002,006,0002,416,640
8Dec 21, 2025 04:2536,35343,069,40720,081,0001,004,0002,441,216
9Dec 21, 2025 04:2536,39547,206,35919,099,0002,010,0002,469,888
10Dec 21, 2025 04:2436,72147,724,50921,298,00002,392,064
11Dec 21, 2025 04:2436,74747,587,56919,284,0002,029,0002,347,008
12Dec 21, 2025 04:2436,83347,408,35321,363,00002,408,448
13Dec 21, 2025 04:2536,97844,658,78020,426,0001,021,0002,482,176
14Dec 21, 2025 04:2537,09147,499,81421,513,00002,478,080
15Dec 21, 2025 04:2437,12146,217,14120,505,0001,025,0002,494,464
16Dec 21, 2025 04:2537,26743,929,88920,586,0001,029,0002,441,216
17Dec 21, 2025 04:2437,33149,153,29421,652,00002,510,848
18Dec 21, 2025 04:2537,34847,513,41521,662,00002,494,464
19Dec 21, 2025 04:2537,48448,688,23620,706,0001,035,0002,510,848
20Dec 21, 2025 04:2537,71748,032,06720,882,000994,0002,506,752
21Dec 21, 2025 04:2437,75750,951,81819,909,0001,990,0002,469,888
22Dec 21, 2025 04:2437,94545,963,32521,008,0001,000,0002,486,272