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:40JuneLJuneLScore: 44,676Success
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:4043,75246,306,21224,361,0001,015,0002,269,184
2Mar 3, 2026 00:4044,20549,546,23924,614,0001,025,0002,396,160
3Mar 3, 2026 00:4144,29556,302,17424,664,0001,027,0002,441,216
4Mar 3, 2026 00:4144,30052,360,99124,667,0001,027,0002,465,792
5Mar 3, 2026 00:4144,38856,817,12424,716,0001,029,0002,306,048
6Mar 3, 2026 00:4044,39051,939,76225,746,00002,478,080
7Mar 3, 2026 00:4144,41258,934,43625,759,00002,514,944
8Mar 3, 2026 00:4144,65353,395,79324,903,000996,0002,412,544
9Mar 3, 2026 00:4044,67452,877,39224,915,000996,0002,338,816
10Mar 3, 2026 00:4044,67653,086,20223,919,0001,993,0002,523,136
11Mar 3, 2026 00:4044,71751,793,92423,941,0001,995,0002,416,640
12Mar 3, 2026 00:4144,72854,316,72425,942,00002,457,600
13Mar 3, 2026 00:4144,87257,912,08624,024,0002,002,0002,428,928
14Mar 3, 2026 00:4144,87254,621,16125,025,0001,001,0002,310,144
15Mar 3, 2026 00:4044,91751,505,45825,050,0001,002,0002,490,368
16Mar 3, 2026 00:4044,94056,061,08724,060,0002,005,0002,514,944
17Mar 3, 2026 00:4044,97856,463,28824,081,0002,006,0002,437,120
18Mar 3, 2026 00:4145,17948,639,73925,197,0001,007,0002,502,656