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 11, 2026 20:16Tim ZhamoidinTim ZhamoidinScore: 62,184Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 11, 2026 20:1658,57859,466,54331,977,0001,998,0002,367,488
2Apr 11, 2026 20:1660,87956,647,83232,284,0003,026,0002,338,816
3Apr 11, 2026 20:1661,20762,140,02231,443,0004,057,0002,445,312
4Apr 11, 2026 20:1661,56460,239,02032,647,0003,060,0002,400,256
5Apr 11, 2026 20:1662,18462,993,78334,064,0002,003,0002,359,296
6Apr 11, 2026 20:1664,34859,790,47633,288,0004,034,0002,441,216
7Apr 11, 2026 20:1664,72160,658,44031,451,0006,087,0002,437,120
8Apr 11, 2026 20:1669,89065,570,31332,429,0008,107,0002,433,024
9Apr 11, 2026 20:1670,68663,953,28036,999,0003,999,0002,478,080