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:28HattonuriHattonuriScore: 36,516Success
Source Code

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

Challenge History

No challenges yet.

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