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 20, 2025 00:28HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:27Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:28055,939,52947,647,000016,384No answer to stdout. Died or killed?
2Dec 20, 2025 00:2833,50338,814,62319,432,00002,531,328
3Dec 20, 2025 00:2733,96044,991,42419,697,00002,277,376
4Dec 20, 2025 00:2834,31647,009,98117,913,0001,990,0002,531,328
5Dec 20, 2025 00:2834,38642,006,18218,947,000997,0002,269,184
6Dec 20, 2025 00:2834,39844,124,47417,956,0001,995,0002,269,184
7Dec 20, 2025 00:2734,45547,476,12917,986,0001,998,0002,277,376
8Dec 20, 2025 00:2834,48848,662,41018,003,0002,000,0002,531,328
9Dec 20, 2025 00:2835,52841,244,25320,606,00002,523,136
10Dec 20, 2025 00:2836,04150,388,37819,909,000995,0002,531,328
11Dec 20, 2025 00:2936,05547,555,23519,917,000995,0002,531,328
12Dec 20, 2025 00:2836,51647,657,66120,171,0001,008,0002,269,184
13Dec 20, 2025 00:2836,69043,668,41121,280,00002,281,472
14Dec 20, 2025 00:2936,91944,188,94821,413,00002,269,184
15Dec 20, 2025 00:2736,92845,899,90621,418,00002,523,136
16Dec 20, 2025 00:2837,34749,198,02521,661,00002,539,520
17Dec 20, 2025 00:2837,76445,830,48419,912,0001,991,0002,523,136
18Dec 20, 2025 00:2837,85944,102,77419,962,0001,996,0002,523,136
19Dec 20, 2025 00:2837,86247,426,78920,962,000998,0002,269,184
20Dec 20, 2025 00:2838,12949,223,80120,105,0002,010,0002,269,184
21Dec 20, 2025 00:2840,24543,113,03623,342,00002,527,232
22Dec 20, 2025 00:2940,43650,599,72123,453,00002,527,232