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 25, 2025 14:26HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:50Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:5102,770,872,8072,762,916,000016,384No answer to stdout. Died or killed?
2Dec 25, 2025 14:2429,14832,194,32215,912,000994,0002,510,848
3Dec 25, 2025 14:2429,14841,657,72316,906,00002,519,040
4Dec 25, 2025 14:2629,53337,724,40115,114,0002,015,0002,531,328
5Dec 25, 2025 14:2529,84136,016,42116,290,0001,018,0002,506,752
6Dec 25, 2025 14:2430,13143,179,90317,476,00002,523,136
7Dec 25, 2025 14:2430,53343,756,83117,709,00002,498,560
8Dec 25, 2025 14:2530,86743,160,98516,909,000994,0002,453,504
9Dec 25, 2025 14:2630,87240,230,26915,917,0001,989,0002,330,624
10Dec 25, 2025 14:2430,87638,417,87917,908,00002,482,176
11Dec 25, 2025 14:2430,87947,610,36016,915,000995,0002,478,080
12Dec 25, 2025 14:2630,92245,659,26816,880,0001,055,0002,363,392
13Dec 25, 2025 14:2630,96733,274,63716,964,000997,0002,523,136
14Dec 25, 2025 14:2530,96939,021,54815,967,0001,995,0002,281,472
15Dec 25, 2025 14:2630,98340,757,03016,972,000998,0002,502,656
16Dec 25, 2025 14:2631,02440,956,87715,995,0001,999,0002,400,256