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 22, 2025 04:20Josu San MartinJosu San MartinScore: 36,519Success
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 19:0134,59044,171,08919,059,0001,003,0002,269,184
2Dec 21, 2025 19:0134,69539,649,07018,111,0002,012,0002,486,272
3Dec 21, 2025 19:0134,75340,747,49918,142,0002,015,0002,506,752
4Dec 21, 2025 19:0134,91244,318,73619,237,0001,012,0002,453,504
5Dec 21, 2025 19:0334,91947,457,31820,253,00002,490,368
6Dec 21, 2025 19:0335,01043,353,98519,291,0001,015,0002,510,848
7Dec 21, 2025 19:0235,04042,770,22719,307,0001,016,0002,437,120
8Dec 21, 2025 19:0235,12436,755,65019,354,0001,018,0002,269,184
9Dec 21, 2025 19:0235,21448,425,38919,403,0001,021,0002,478,080
10Dec 21, 2025 19:0235,29843,248,07218,426,0002,047,0002,314,240
11Dec 21, 2025 19:0335,39043,220,13419,500,0001,026,0002,510,848
12Dec 21, 2025 19:0335,45740,814,81319,537,0001,028,0002,506,752
13Dec 22, 2025 04:2035,48340,206,90519,551,0001,029,0002,469,888
14Dec 21, 2025 19:0335,57245,780,54219,601,0001,031,0002,498,560
15Dec 21, 2025 19:0135,63353,057,27720,667,00002,457,600
16Dec 21, 2025 19:0335,77246,415,10919,711,0001,037,0002,297,856
17Dec 21, 2025 19:0236,04546,002,93318,915,0001,991,0002,457,600
18Dec 21, 2025 19:0136,07146,349,39819,925,000996,0002,478,080
19Dec 21, 2025 19:0336,32144,849,98020,063,0001,003,0002,502,656
20Dec 21, 2025 19:0136,35950,515,13920,084,0001,004,0002,416,640
21Dec 21, 2025 19:0336,40041,344,67419,102,0002,010,0002,445,312
22Dec 21, 2025 19:0336,40244,222,18719,103,0002,010,0002,465,792
23Dec 21, 2025 19:0236,41946,525,69719,112,0002,011,0002,441,216
24Dec 21, 2025 19:0336,42952,937,75119,117,0002,012,0002,351,104
25Dec 21, 2025 19:0336,49346,383,65419,151,0002,015,0002,461,696
26Dec 21, 2025 19:0236,51937,019,86419,164,0002,017,0002,301,952
27Dec 23, 2025 04:1636,58843,283,44320,211,0001,010,0002,506,752
28Dec 21, 2025 19:0336,62647,727,27119,220,0002,023,0002,457,600
29Dec 21, 2025 19:0336,62843,674,39119,221,0002,023,0002,383,872
30Dec 21, 2025 19:0236,71443,301,62720,280,0001,014,0002,465,792
31Dec 21, 2025 19:0236,83443,577,47020,347,0001,017,0002,338,816
32Dec 21, 2025 19:0236,84745,033,32619,336,0002,035,0002,453,504
33Dec 21, 2025 19:0236,87646,483,83120,370,0001,018,0002,428,928
34Dec 21, 2025 19:0236,88447,870,76920,375,0001,018,0002,318,336
35Dec 23, 2025 04:1636,88455,022,70021,393,00002,412,544
36Dec 21, 2025 19:0336,90041,469,63820,383,0001,019,0002,457,600
37Dec 21, 2025 19:0236,91040,125,00321,408,00002,506,752
38Dec 21, 2025 19:0136,97246,223,35420,423,0001,021,0002,371,584
39Dec 21, 2025 19:0236,98847,182,24820,432,0001,021,0002,424,832
40Dec 21, 2025 19:0136,99047,406,54420,433,0001,021,0002,293,760
41Dec 23, 2025 04:1637,01047,323,47520,444,0001,022,0002,330,624
42Dec 21, 2025 19:0337,05251,769,35121,490,00002,449,408
43Dec 22, 2025 04:2037,09044,588,22620,488,0001,024,0002,506,752
44Dec 21, 2025 19:0337,11946,139,29821,529,00002,428,928
45Dec 21, 2025 19:0137,14745,679,06721,545,00002,281,472
46Dec 21, 2025 19:0337,21447,626,61820,557,0001,027,0002,416,640
47Dec 21, 2025 19:0237,22443,987,11220,562,0001,028,0002,490,368
48Dec 21, 2025 19:0137,30042,656,95320,604,0001,030,0002,412,544
49Dec 22, 2025 04:2037,30542,618,14320,607,0001,030,0002,424,832
50Dec 21, 2025 19:0337,31443,381,11020,612,0001,030,0002,412,544
51Dec 21, 2025 19:0138,06745,845,39121,076,0001,003,0002,465,792