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 listFeb 19, 2026 01:02Josu San MartinJosu San MartinScore: 3,779Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 19, 2026 01:023,48618,376,3881,011,0001,011,0002,314,240
2Feb 19, 2026 01:013,50318,663,3411,016,0001,016,0002,314,240
3Feb 19, 2026 01:013,67916,938,8771,067,0001,067,0002,285,568
4Feb 19, 2026 01:013,67921,439,8271,067,0001,067,0002,502,656
5Feb 19, 2026 01:013,71722,783,1311,078,0001,078,0002,433,024
6Feb 19, 2026 01:013,76920,776,23902,186,0002,498,560
7Feb 19, 2026 01:013,77919,230,44202,192,0002,265,088
8Feb 19, 2026 01:013,98620,355,2311,156,0001,156,0002,506,752
9Feb 19, 2026 01:023,99320,383,2811,158,0001,158,0002,289,664
10Feb 19, 2026 01:014,04518,890,8641,173,0001,173,0002,498,560
11Feb 19, 2026 01:024,04723,440,3712,347,00002,269,184
12Feb 19, 2026 01:014,16925,114,3531,209,0001,209,0002,510,848