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 26, 2026 23:08Victor MerckléVictor MerckléScore: 644,660Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 26, 2026 23:09609,015367,202,433325,211,00028,018,0002,318,336
2Apr 26, 2026 23:09616,353371,749,745332,521,00024,964,0002,367,488
3Apr 26, 2026 23:09631,001386,204,400339,983,00025,998,0002,347,008
4Apr 26, 2026 23:09635,646389,510,172337,703,00030,972,0002,445,312
5Apr 26, 2026 23:09644,660392,342,142349,910,00023,993,0002,551,808
6Apr 26, 2026 23:09653,996399,132,312352,296,00027,022,0002,490,368
7Apr 26, 2026 23:09661,293399,346,312354,509,00029,041,0002,342,912
8Apr 26, 2026 23:09714,627436,498,768384,522,00029,962,0002,330,624
9Apr 26, 2026 23:09717,037437,655,412390,890,00024,992,0002,359,296