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 listAug 27, 2026 08:00Haha ExpertHaha ExpertError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:40Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:48011,998,75902,112,00045,056No answer to stdout. Died or killed?stderr
2Aug 27, 2026 08:04270,487166,678,093150,040,0006,843,0002,142,208
3Aug 27, 2026 08:04272,593169,214,902151,162,0006,942,0002,142,208
4Aug 27, 2026 08:04277,355171,872,315154,831,0006,035,0002,203,648
5Aug 27, 2026 08:04283,381174,009,218157,866,0006,495,0002,142,208
6Aug 27, 2026 08:04285,931175,605,191159,791,0006,049,0002,142,208
7Aug 27, 2026 08:04289,886177,875,458161,917,0006,217,0002,203,648
8Aug 27, 2026 08:04291,008178,559,865161,999,0006,786,0002,142,208
9Aug 27, 2026 08:04295,081181,361,322164,305,0006,842,0002,142,208
10Aug 27, 2026 08:04326,044198,757,423182,696,0006,410,0002,166,784