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 31, 2025 21:50Yuriy LyfenkoYuriy LyfenkoScore: 60,641Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 31, 2025 21:4954,97962,668,47729,895,0001,993,0002,482,176
2Mar 31, 2025 21:4955,59154,855,09231,236,0001,007,0002,514,944
3Mar 31, 2025 21:4956,12154,995,33231,533,0001,017,0002,514,944
4Mar 31, 2025 21:4956,72162,694,17130,905,0001,993,0002,420,736
5Mar 31, 2025 21:5057,17855,324,01431,154,0002,009,0002,498,560
6Mar 31, 2025 21:4958,11262,572,34632,684,0001,021,0002,486,272
7Mar 31, 2025 21:4958,87459,799,85932,139,0002,008,0002,408,448
8Mar 31, 2025 21:4960,64162,149,45533,163,0002,009,0002,482,176
9Mar 31, 2025 21:5062,91961,108,74635,480,0001,013,0002,371,584
10Mar 31, 2025 21:5063,34066,047,16635,717,0001,020,0002,404,352
11Mar 31, 2025 21:5063,56268,055,37935,870,000996,0002,424,832
12Mar 31, 2025 21:5063,56460,945,13535,871,000996,0002,510,848
13Mar 31, 2025 21:4964,11461,057,07936,181,0001,005,0002,449,408
14Mar 31, 2025 21:5072,23867,298,99839,903,0001,995,0002,416,640
15Mar 31, 2025 21:4973,57667,422,56541,658,0001,016,0002,273,280