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 listDec 14, 2025 20:58HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:15Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:21012,236,0101,455,000020,480No answer to stdout. Died or killed?
2Dec 14, 2025 20:5383,64771,756,65043,462,0005,053,0002,490,368
3Dec 14, 2025 20:5384,33881,391,81845,922,0002,994,0002,289,664
4Dec 14, 2025 20:5885,56978,989,89047,605,0002,025,0002,502,656
5Dec 14, 2025 20:5387,00081,397,44247,433,0003,027,0002,486,272
6Dec 14, 2025 20:5387,21677,235,60747,550,0003,035,0002,523,136
7Dec 14, 2025 20:5389,44878,960,52448,887,0002,993,0002,478,080
8Dec 14, 2025 20:5889,48489,394,15949,905,0001,996,0002,498,560
9Dec 14, 2025 20:5393,33181,965,89851,125,0003,007,0002,473,984
10Dec 14, 2025 20:5394,58180,689,48550,868,0003,989,0002,428,928
11Dec 14, 2025 20:5394,78380,026,02553,975,000999,0002,420,736
12Dec 14, 2025 20:5895,43480,154,19653,340,0002,012,0002,441,216
13Dec 14, 2025 20:5395,82479,004,96653,557,0002,021,0002,469,888