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 27, 2026 20:57TudyMTudyMScore: 165,772Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 27, 2026 20:58161,732111,068,63683,826,0009,979,0002,396,160
2Apr 27, 2026 20:58164,251115,095,41784,236,00011,030,0002,400,256
3Apr 27, 2026 20:58164,800115,986,62783,511,00012,073,0002,347,008
4Apr 27, 2026 20:58164,975112,894,77383,600,00012,086,0002,334,720
5Apr 27, 2026 20:58165,772112,124,19482,127,00014,021,0002,351,104
6Apr 27, 2026 20:58167,139112,479,40289,946,0006,995,0002,322,432
7Apr 27, 2026 20:58169,355118,390,97687,201,00011,025,0002,539,520
8Apr 27, 2026 20:58175,879121,768,60689,009,00013,001,0002,318,336
9Apr 27, 2026 20:58176,548121,280,00092,359,00010,039,0002,351,104