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 21, 2025 02:20HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:03Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:04030,003,907,30329,995,703,000016,384Killed by timeout
2Dec 21, 2025 02:2031,42631,418,60917,215,0001,012,0002,510,848
3Dec 21, 2025 02:2031,85340,108,70317,449,0001,026,0002,322,432
4Dec 21, 2025 02:2031,90238,178,57218,503,00002,281,472
5Dec 21, 2025 02:2031,94739,620,74018,529,00002,334,720
6Dec 21, 2025 02:2032,20942,159,82117,644,0001,037,0002,527,232
7Dec 21, 2025 02:2032,61437,306,70617,921,000995,0002,297,856
8Dec 21, 2025 02:2032,62243,278,16817,926,000995,0002,326,528
9Dec 21, 2025 02:2032,83342,848,47818,041,0001,002,0002,510,848
10Dec 21, 2025 02:2033,77843,397,53319,591,00002,519,040
11Dec 21, 2025 02:2033,98144,554,52519,709,00002,392,064
12Dec 21, 2025 02:2034,03846,384,52819,742,00002,281,472
13Dec 21, 2025 02:2034,31745,714,84219,904,00002,478,080