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 21, 2025 00:11Josu San MartinJosu San MartinScore: 37,124Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 01:4035,02136,267,22019,297,0001,015,0002,293,760
2Dec 21, 2025 00:1035,29535,563,35419,448,0001,023,0002,306,048
3Dec 21, 2025 00:1035,37943,759,36919,494,0001,026,0002,338,816
4Dec 21, 2025 00:1035,62851,435,00819,631,0001,033,0002,342,912
5Dec 21, 2025 01:4035,67149,789,92820,689,00002,301,952
6Dec 21, 2025 00:1135,78645,412,09319,719,0001,037,0002,326,528
7Dec 21, 2025 00:1136,03148,225,73818,908,0001,990,0002,347,008
8Dec 21, 2025 01:4036,04052,512,66219,908,000995,0002,494,464
9Dec 21, 2025 00:0936,09341,268,39019,938,000996,0002,342,912
10Dec 21, 2025 00:1136,18849,365,33319,990,000999,0002,322,432
11Dec 21, 2025 00:0936,28844,675,70120,045,0001,002,0002,441,216
12Dec 21, 2025 00:1036,30547,674,21119,052,0002,005,0002,301,952
13Dec 21, 2025 00:1036,32643,851,90219,063,0002,006,0002,285,568
14Dec 21, 2025 01:4036,38838,040,57119,095,0002,010,0002,490,368
15Dec 21, 2025 00:1236,39339,208,14620,103,0001,005,0002,441,216
16Dec 21, 2025 01:4036,41643,221,40119,110,0002,011,0002,289,664
17Dec 21, 2025 00:0936,53344,568,33319,171,0002,018,0002,457,600
18Dec 21, 2025 00:1136,74544,593,77420,298,0001,014,0002,314,240
19Dec 21, 2025 00:0936,79841,614,53119,311,0002,032,0002,297,856
20Dec 21, 2025 00:1236,81048,015,73220,334,0001,016,0002,506,752
21Dec 21, 2025 00:0936,98647,704,19220,431,0001,021,0002,510,848
22Dec 21, 2025 00:1137,12443,735,56919,575,0001,957,0002,289,664
23Dec 21, 2025 00:1137,12943,082,05721,535,00002,330,624
24Dec 21, 2025 00:0937,14142,010,60320,517,0001,025,0002,506,752
25Dec 21, 2025 00:1137,17146,489,29219,506,0002,053,0002,506,752
26Dec 21, 2025 00:1237,22246,633,95721,589,00002,482,176
27Dec 21, 2025 00:1037,35553,943,03821,666,00002,457,600
28Dec 21, 2025 00:1037,39348,222,08420,656,0001,032,0002,351,104
29Dec 21, 2025 00:1037,46047,739,89321,727,00002,269,184
30Dec 21, 2025 00:1137,48646,460,67920,707,0001,035,0002,322,432
31Dec 21, 2025 00:0937,50745,841,67520,719,0001,035,0002,408,448
32Dec 21, 2025 01:4037,55240,263,11120,743,0001,037,0002,318,336
33Dec 21, 2025 01:4037,58456,238,76720,761,0001,038,0002,285,568
34Dec 21, 2025 00:1137,72450,342,73319,891,0001,989,0002,408,448
35Dec 21, 2025 00:0937,76649,700,80020,909,000995,0002,310,144
36Dec 21, 2025 00:1137,78351,911,08919,922,0001,992,0002,273,280
37Dec 21, 2025 00:1037,80549,739,32319,934,0001,993,0002,478,080
38Dec 21, 2025 00:1137,81450,581,54520,936,000996,0002,412,544
39Dec 21, 2025 01:4037,82645,385,14520,942,000997,0002,506,752
40Dec 21, 2025 00:0937,85749,948,19920,959,000998,0002,273,280
41Dec 21, 2025 01:4037,94747,681,64620,009,0002,000,0002,371,584
42Dec 21, 2025 00:1138,03647,944,50121,059,0001,002,0002,404,352