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 listMar 3, 2026 00:35JuneLJuneLScore: 49,921Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 3, 2026 00:3548,18452,363,61126,949,000998,0002,514,944
2Mar 3, 2026 00:3548,57253,261,95426,160,0002,012,0002,465,792
3Mar 3, 2026 00:3548,77453,582,14227,279,0001,010,0002,514,944
4Mar 3, 2026 00:3748,93449,561,92227,369,0001,013,0002,355,200
5Mar 3, 2026 00:3749,42651,784,31228,667,00002,437,120
6Mar 3, 2026 00:3549,82459,391,13327,902,000996,0002,387,968
7Mar 3, 2026 00:3749,83860,196,12626,913,0001,993,0002,469,888
8Mar 3, 2026 00:3749,84060,342,79627,911,000996,0002,433,024
9Mar 3, 2026 00:3749,88352,922,34226,937,0001,995,0002,273,280
10Mar 3, 2026 00:3549,92155,866,07126,958,0001,996,0002,478,080
11Mar 3, 2026 00:3550,06254,905,80727,034,0002,002,0002,433,024
12Mar 3, 2026 00:3550,13349,502,54627,072,0002,005,0002,478,080
13Mar 3, 2026 00:3750,15256,366,22927,082,0002,006,0002,465,792
14Mar 3, 2026 00:3550,21955,028,55328,123,0001,004,0002,383,872
15Mar 3, 2026 00:3750,34050,180,47828,191,0001,006,0002,445,312
16Mar 3, 2026 00:3750,49757,667,77627,269,0002,019,0002,457,600
17Mar 3, 2026 00:3550,53156,248,35028,298,0001,010,0002,281,472
18Mar 3, 2026 00:3750,83354,580,92528,467,0001,016,0002,420,736