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 listApr 25, 2026 12:39olliecrowolliecrowScore: 54,643Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 25, 2026 12:4052,67544,454,80529,534,0001,018,0002,334,720
2Apr 25, 2026 12:4053,37937,592,49328,963,0001,997,0002,453,504
3Apr 25, 2026 12:4054,16337,765,02131,415,00002,334,720
4Apr 25, 2026 12:4054,36238,050,95230,513,0001,017,0002,334,720
5Apr 25, 2026 12:4054,64349,414,30530,671,0001,022,0002,379,776
6Apr 25, 2026 12:4054,70638,472,11030,707,0001,023,0002,334,720
7Apr 25, 2026 12:4054,84138,392,68030,782,0001,026,0002,334,720
8Apr 25, 2026 12:4054,93437,647,66828,875,0002,987,0002,334,720
9Apr 25, 2026 12:4058,41339,463,80530,891,0002,989,0002,334,720