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 2, 2026 22:12JuneLJuneLError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:12Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:15011,186,874969,000024,576No answer to stdout. Died or killed?
2Mar 2, 2026 22:1152,69558,088,76729,545,0001,018,0002,441,216
3Mar 2, 2026 22:1152,90255,015,99430,683,00002,510,848
4Mar 2, 2026 22:1153,24858,371,10628,892,0001,992,0002,523,136
5Mar 2, 2026 22:1253,39561,222,11728,971,0001,998,0002,433,024
6Mar 2, 2026 22:1253,41658,252,10228,983,0001,998,0002,433,024
7Mar 2, 2026 22:1253,50564,960,07429,031,0002,002,0002,334,720
8Mar 2, 2026 22:1253,52863,636,69530,045,0001,001,0002,510,848
9Mar 2, 2026 22:1153,54860,555,20430,057,0001,001,0002,289,664
10Mar 2, 2026 22:1253,58855,174,65229,076,0002,005,0002,375,680
11Mar 2, 2026 22:1253,81253,010,93429,198,0002,013,0002,281,472
12Mar 2, 2026 22:1153,82456,762,37230,211,0001,007,0002,310,144
13Mar 2, 2026 22:1253,85259,158,16830,227,0001,007,0002,400,256
14Mar 2, 2026 22:1253,88651,883,50129,238,0002,016,0002,416,640
15Mar 2, 2026 22:1153,91754,842,33829,255,0002,017,0002,367,488
16Mar 2, 2026 22:1253,92955,177,12229,261,0002,018,0002,527,232
17Mar 2, 2026 22:1253,94854,089,98130,281,0001,009,0002,465,792
18Mar 2, 2026 22:1254,02153,923,28130,322,0001,010,0002,416,640
19Mar 2, 2026 22:1254,07154,074,91030,350,0001,011,0002,408,448
20Mar 2, 2026 22:1254,10554,115,42630,369,0001,012,0002,367,488
21Mar 2, 2026 22:1254,15256,562,72130,395,0001,013,0002,379,776
22Mar 2, 2026 22:1254,16660,714,80630,403,0001,013,0002,355,200
23Mar 2, 2026 22:1154,20255,567,56130,423,0001,014,0002,383,872
24Mar 2, 2026 22:1254,24057,693,19930,445,0001,014,0002,334,720
25Mar 2, 2026 22:1254,24154,076,54831,460,00002,469,888
26Mar 2, 2026 22:1154,39158,114,27231,547,00002,473,984
27Mar 2, 2026 22:1154,40257,650,96531,553,00002,424,832
28Mar 2, 2026 22:1254,45252,670,15331,582,00002,379,776
29Mar 2, 2026 22:1254,49853,820,00730,590,0001,019,0002,490,368
30Mar 2, 2026 22:1254,50765,743,38130,595,0001,019,0002,281,472
31Mar 2, 2026 22:1254,52959,157,71730,607,0001,020,0002,482,176