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 listMar 2, 2026 09:420xZohan0xZohanError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Apr 28, 2026 11:52Unays 11.63RecheckSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 28, 2026 11:54016,359,3212,420,0001,210,0002,347,008expected "1215077 ", got "1214193 "
2Apr 28, 2026 11:54022,250,5702,349,0001,174,0002,334,720expected "1255483 ", got "1251381 "
3Mar 2, 2026 09:425,77819,474,3122,234,0001,117,0002,351,104
4Mar 2, 2026 09:425,85525,996,2663,396,00002,355,200
5Mar 2, 2026 09:425,85719,312,7023,397,00002,289,664
6Mar 2, 2026 09:425,86022,156,2612,266,0001,133,0002,347,008
7Mar 2, 2026 09:425,86219,427,9092,267,0001,133,0002,289,664
8Mar 2, 2026 09:425,94520,873,5012,299,0001,149,0002,355,200
9Mar 2, 2026 09:425,98319,605,6983,470,00002,363,392
10Apr 28, 2026 11:546,00023,902,5632,320,0001,160,0002,330,624
11Mar 2, 2026 09:426,06425,966,5042,345,0001,172,0002,355,200
12Mar 2, 2026 09:426,09023,394,7302,355,0001,177,0002,359,296