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 19, 2025 14:47HattonuriHattonuriScore: 40,681Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 19, 2025 04:5736,78639,293,11720,320,0001,016,0002,375,680
2Dec 19, 2025 04:5837,15343,221,57420,523,0001,026,0002,281,472
3Dec 19, 2025 14:4637,78346,671,45420,918,000996,0002,285,568
4Dec 19, 2025 04:5337,82843,562,95621,940,00002,269,184
5Dec 19, 2025 04:5337,99048,940,72721,033,0001,001,0002,523,136
6Dec 19, 2025 04:5338,08646,874,53421,086,0001,004,0002,281,472
7Dec 19, 2025 04:5438,75353,601,14921,456,0001,021,0002,523,136
8Dec 19, 2025 14:4738,77243,086,25922,488,00002,285,568
9Dec 19, 2025 04:5438,77644,003,49921,468,0001,022,0002,531,328
10Dec 19, 2025 14:4639,02249,634,48522,633,00002,531,328
11Dec 19, 2025 04:5739,22249,772,98822,749,00002,523,136
12Dec 19, 2025 04:5439,22847,848,63822,752,00002,269,184
13Dec 19, 2025 04:5439,42447,540,61621,872,000994,0002,269,184
14Dec 19, 2025 04:5839,73655,935,23522,045,0001,002,0002,523,136
15Dec 19, 2025 04:5339,99345,901,14121,179,0002,017,0002,367,488
16Dec 19, 2025 14:4640,68144,941,04123,595,00002,523,136
17Dec 19, 2025 14:4640,76745,948,68523,645,00002,277,376
18Dec 19, 2025 04:5341,21047,450,45322,907,000995,0002,523,136
19Dec 19, 2025 04:5341,31745,465,70221,967,0001,997,0002,523,136
20Dec 19, 2025 14:4641,60944,813,14423,128,0001,005,0002,519,040
21Dec 19, 2025 04:5342,26446,484,49524,513,00002,523,136
22Dec 19, 2025 14:4642,69055,756,25023,729,0001,031,0002,523,136
23Dec 19, 2025 14:4742,93153,940,41122,908,0001,992,0002,531,328
24Dec 19, 2025 04:5842,94049,060,06223,909,000996,0002,527,232
25Dec 19, 2025 04:5742,94851,763,21324,910,00002,277,376
26Dec 19, 2025 04:5443,32251,905,59224,122,0001,005,0002,527,232
27Dec 19, 2025 04:5343,35247,716,47324,139,0001,005,0002,519,040
28Dec 19, 2025 04:5443,62459,367,49125,302,00002,519,040
29Dec 19, 2025 14:4744,18344,661,88824,601,0001,025,0002,527,232
30Dec 19, 2025 04:5344,25049,659,11925,665,00002,269,184