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 listSep 23, 2026 19:48Matthias GörgensMatthias GörgensError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:14Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:220201,111,634171,521,00019,100,00024,576expected "867848699 ", got "955000029 "
2Oct 5, 2026 19:220198,652,809168,812,00019,391,00020,480expected "870762221 ", got "29 "
3Oct 5, 2026 19:220201,361,342169,960,00020,803,00024,576expected "868763215 ", got "944000029 "
4Sep 23, 2026 19:48287,534177,871,449149,762,00017,008,0002,117,632
5Sep 23, 2026 19:48289,029178,035,708151,693,00015,944,0002,117,632
6Sep 23, 2026 19:48292,584180,205,586152,226,00017,473,0002,117,632
7Sep 23, 2026 19:48294,346181,030,235153,811,00016,910,0002,117,632
8Sep 23, 2026 19:48294,527181,223,658153,856,00016,970,0002,117,632
9Sep 23, 2026 19:48295,948182,011,430154,547,00017,103,0002,117,632
10Sep 23, 2026 19:48297,351182,897,838154,889,00017,575,0002,117,632
11Sep 23, 2026 19:48306,436188,226,572160,103,00017,630,0002,117,632
12Sep 23, 2026 19:48306,750188,373,742161,699,00016,216,0002,117,632