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 22:47HattonuriHattonuriScore: 36,076Success
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 22:5133,70043,115,03019,546,00002,531,328
2Dec 20, 2025 22:5133,75034,830,17518,545,0001,030,0002,527,232
3Dec 20, 2025 22:5134,31945,627,47018,910,000995,0002,527,232
4Dec 20, 2025 22:4834,83338,199,13719,193,0001,010,0002,527,232
5Dec 20, 2025 22:5134,83838,584,00819,196,0001,010,0002,281,472
6Dec 20, 2025 22:4934,97447,541,41818,257,0002,028,0002,273,280
7Dec 20, 2025 22:5135,08846,418,85620,351,00002,527,232
8Dec 20, 2025 22:4735,30042,640,94720,474,00002,527,232
9Dec 20, 2025 22:4935,30243,684,87220,475,00002,523,136
10Dec 20, 2025 22:5035,51241,694,16319,568,0001,029,0002,519,040
11Dec 20, 2025 22:5135,56746,398,83719,598,0001,031,0002,519,040
12Dec 20, 2025 22:4935,60748,928,58420,652,00002,527,232
13Dec 20, 2025 22:4735,75347,596,92919,701,0001,036,0002,519,040
14Dec 20, 2025 22:4935,81745,175,55920,774,00002,527,232
15Dec 20, 2025 22:4935,82951,165,16419,742,0001,039,0002,281,472
16Dec 20, 2025 22:4936,06249,384,52118,924,0001,992,0002,523,136
17Dec 20, 2025 22:4936,07648,605,86720,924,00002,281,472
18Dec 20, 2025 22:4836,09545,209,15018,942,0001,993,0002,265,088
19Dec 20, 2025 22:4736,25246,591,62619,024,0002,002,0002,527,232
20Dec 20, 2025 22:5436,36444,645,77020,087,0001,004,0002,519,040
21Dec 20, 2025 22:5136,39041,472,78419,096,0002,010,0002,527,232
22Dec 20, 2025 22:5436,44742,049,49519,126,0002,013,0002,527,232
23Dec 20, 2025 22:4736,62844,074,94819,221,0002,023,0002,519,040
24Dec 20, 2025 22:5036,62935,936,68020,234,0001,011,0002,527,232
25Dec 20, 2025 22:5136,65544,443,68420,248,0001,012,0002,527,232
26Dec 20, 2025 22:5036,84343,466,16921,369,00002,523,136
27Dec 20, 2025 22:4936,85043,571,40221,373,00002,527,232
28Dec 20, 2025 22:4936,97441,687,38020,424,0001,021,0002,519,040
29Dec 20, 2025 22:4736,99345,538,56821,456,00002,519,040
30Dec 20, 2025 22:4837,09338,939,60221,514,00002,527,232
31Dec 20, 2025 22:5137,22147,891,70321,588,00002,527,232
32Dec 20, 2025 22:4737,25040,008,43521,605,00002,519,040
33Dec 20, 2025 22:5437,51953,437,95120,725,0001,036,0002,527,232