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 listJan 30, 2026 17:03olliecrowolliecrowError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 12:57Andrew Epstein10.78CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 12:58030,030,318,00830,017,005,0002,008,00016,384Killed by timeout
2Jan 30, 2026 17:039,97122,856,2424,627,0001,156,0002,506,752
3Jan 30, 2026 17:0310,21220,464,3703,949,0001,974,0002,490,368
4Jan 30, 2026 17:0310,29322,188,6883,980,0001,990,0002,494,464
5Jan 30, 2026 17:0310,40724,272,8715,030,0001,006,0002,285,568
6Jan 30, 2026 17:0310,43825,147,9714,036,0002,018,0002,289,664
7Jan 30, 2026 17:0310,43823,332,1435,045,0001,009,0002,277,376
8Jan 30, 2026 17:0310,46622,892,6154,047,0002,023,0002,281,472
9Jan 30, 2026 17:0310,66228,629,8444,123,0002,061,0002,478,080
10Jan 30, 2026 17:0310,75523,024,5824,159,0002,079,0002,281,472