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 listDec 19, 2025 05:36Josu San MartinJosu San MartinScore: 83,838Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 19, 2025 05:4082,57678,581,13945,899,0001,995,0002,277,376
2Dec 19, 2025 05:3682,69875,540,11946,966,000999,0002,355,200
3Dec 19, 2025 05:4083,02672,542,78747,152,0001,003,0002,277,376
4Dec 19, 2025 05:3683,03470,433,84447,157,0001,003,0002,277,376
5Dec 19, 2025 05:3683,47269,133,10147,406,0001,008,0002,355,200
6Dec 19, 2025 05:4083,54070,793,51848,453,00002,277,376
7Dec 19, 2025 05:4083,55970,663,22748,464,00002,277,376
8Dec 19, 2025 05:4083,75969,764,00948,580,00002,351,104
9Dec 19, 2025 05:3683,77270,493,22547,576,0001,012,0002,457,600
10Dec 19, 2025 05:3683,83874,015,28548,626,00002,277,376
11Dec 19, 2025 05:4083,92173,179,35147,660,0001,014,0002,277,376
12Dec 19, 2025 05:4084,04873,787,81346,759,0001,989,0002,351,104
13Dec 19, 2025 05:3684,12277,063,17347,796,000995,0002,359,296
14Dec 19, 2025 05:3684,22275,128,13946,856,0001,993,0002,277,376
15Dec 19, 2025 05:3684,27880,068,24547,884,000997,0002,359,296
16Dec 19, 2025 05:4084,29372,960,32746,895,0001,995,0002,355,200
17Dec 19, 2025 05:3684,31478,432,29847,904,000998,0002,355,200
18Dec 19, 2025 05:4084,44772,252,00246,980,0001,999,0002,449,408