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 26, 2026 18:36TudyMTudyMError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:01Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:080780,429,296762,521,0007,673,0002,113,536expected "861838369 ", got "966000006 "
2Oct 5, 2026 19:080802,131,789784,076,0007,597,0002,113,536expected "870780494 ", got "943000029 "
3Oct 5, 2026 19:080838,172,628820,118,0007,716,0002,113,536expected "872723490 ", got "943000029 "
4Apr 26, 2026 18:36475,531293,733,808267,814,0007,994,0002,322,432
5Apr 26, 2026 18:36477,525295,986,361267,967,0008,998,0002,318,336
6Apr 26, 2026 18:36478,513296,657,520266,557,00010,981,0002,555,904
7Apr 26, 2026 18:36478,948300,177,880267,798,0009,992,0002,310,144
8Apr 26, 2026 18:36479,948297,057,174270,360,0008,010,0002,318,336
9Apr 26, 2026 18:36481,248295,742,334270,120,0009,004,0002,400,256
10Apr 26, 2026 18:36482,400294,630,757266,802,00012,990,0002,297,856
11Apr 26, 2026 18:36482,593295,540,828264,910,00014,994,0002,408,448
12Apr 26, 2026 18:36484,451296,612,234268,983,00011,999,0002,330,624