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 13, 2026 14:49richardbranway1richardbranway1Score: 94,038Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 13, 2026 14:0993,46680,796,52051,199,0003,011,0002,424,832
2Mar 13, 2026 14:0993,52279,868,05951,230,0003,013,0002,465,792
3Mar 15, 2026 18:2093,59178,092,43049,257,0005,026,0002,482,176
4Mar 13, 2026 14:0993,64381,661,44751,296,0003,017,0002,445,312
5Mar 13, 2026 14:4993,87884,121,70353,441,0001,008,0002,461,696
6Mar 13, 2026 14:0993,91774,951,76652,455,0002,017,0002,457,600
7Mar 13, 2026 14:0993,99877,662,17052,500,0002,019,0002,473,984
8Mar 15, 2026 18:2094,03872,374,49452,522,0002,020,0002,465,792
9Mar 13, 2026 14:0994,09777,084,84450,534,0004,042,0002,461,696
10Mar 13, 2026 14:4994,13181,393,27653,585,0001,011,0002,473,984
11Mar 13, 2026 14:0994,36480,717,32852,704,0002,027,0002,465,792
12Mar 13, 2026 14:0994,62279,078,02351,888,0002,993,0002,441,216
13Mar 15, 2026 18:2094,62981,151,09150,894,0003,991,0002,494,464
14Mar 13, 2026 14:0994,63482,779,50953,891,000997,0002,506,752
15Mar 13, 2026 14:4994,78879,165,57851,979,0002,998,0002,502,656