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 listSep 27, 2025 18:42Wookyung JeongWookyung JeongScore: 62,981Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 18:4259,50257,076,25527,406,0007,105,0002,285,568
2Sep 27, 2025 18:5059,63460,303,63526,450,0008,138,0002,277,376
3Sep 27, 2025 18:4459,86964,191,63829,618,0005,106,0002,355,200
4Sep 27, 2025 18:4460,10759,849,14528,886,0005,976,0002,392,064
5Sep 27, 2025 18:4460,29356,931,89127,976,0006,994,0002,359,296
6Sep 27, 2025 18:4460,40261,545,41527,026,0008,007,0002,347,008
7Sep 27, 2025 18:5060,47261,634,58131,066,0004,008,0002,494,464
8Sep 27, 2025 18:5060,99056,918,16426,278,0009,096,0002,498,560
9Sep 27, 2025 18:4261,08159,052,21428,342,0007,085,0002,420,736
10Sep 27, 2025 18:4461,12455,580,70629,375,0006,077,0002,510,848
11Sep 27, 2025 18:4261,22956,407,82432,469,0003,044,0002,314,240
12Sep 27, 2025 18:4261,25558,017,97429,438,0006,090,0002,342,912
13Sep 27, 2025 18:5061,56061,479,93032,645,0003,060,0002,498,560
14Sep 27, 2025 18:4261,57160,512,62933,671,0002,040,0002,383,872
15Sep 27, 2025 18:5061,65060,445,86327,584,0008,173,0002,306,048
16Sep 27, 2025 18:4361,77963,177,14932,846,0002,986,0002,449,408
17Sep 27, 2025 18:4561,87861,355,10127,914,0007,975,0002,355,200
18Sep 27, 2025 18:4462,02164,079,72630,976,0004,996,0002,363,392
19Sep 27, 2025 18:5062,04059,632,36225,988,0009,995,0002,347,008
20Sep 27, 2025 18:4362,18157,367,73331,056,0005,009,0002,355,200
21Sep 27, 2025 18:4462,22263,636,47727,067,0009,022,0002,371,584
22Sep 27, 2025 18:4362,25562,849,24330,090,0006,018,0002,453,504
23Sep 27, 2025 18:4962,40762,392,85630,164,0006,032,0002,445,312
24Sep 27, 2025 18:4162,41057,866,26431,171,0005,027,0002,420,736
25Sep 27, 2025 18:4262,54759,512,56028,216,0008,061,0002,412,544
26Sep 27, 2025 18:4562,56057,069,53631,246,0005,039,0002,273,280
27Sep 27, 2025 18:4962,57166,073,86430,243,0006,048,0002,453,504
28Sep 27, 2025 18:4462,64158,925,93031,286,0005,046,0002,277,376
29Sep 27, 2025 18:4162,79860,861,27931,365,0005,058,0002,465,792
30Sep 27, 2025 18:4262,86468,528,32132,410,0004,051,0002,387,968
31Sep 27, 2025 18:4462,98162,861,56230,441,0006,088,0002,392,064
32Sep 27, 2025 18:4162,99761,398,75130,449,0006,089,0002,310,144
33Sep 27, 2025 18:4363,08665,725,96726,426,00010,164,0002,318,336
34Sep 27, 2025 18:4563,17965,066,73130,537,0006,107,0002,293,760
35Sep 27, 2025 18:4263,38162,787,20132,677,0004,084,0002,461,696
36Sep 27, 2025 18:4463,47160,738,03428,854,0007,959,0002,433,024
37Sep 27, 2025 18:4463,48162,116,66630,849,0005,970,0002,285,568
38Sep 27, 2025 18:4263,52971,725,20732,864,0003,983,0002,310,144
39Sep 27, 2025 18:4463,57161,709,52434,878,0001,993,0002,322,432
40Sep 27, 2025 18:4463,57864,020,80128,902,0007,973,0002,359,296
41Sep 27, 2025 18:4463,60264,460,89430,907,0005,982,0002,502,656
42Sep 27, 2025 18:4963,69364,046,64330,952,0005,990,0002,404,352
43Sep 27, 2025 18:4363,78663,714,54027,997,0008,999,0002,351,104
44Sep 27, 2025 18:4263,80060,106,20931,004,0006,000,0002,461,696
45Sep 27, 2025 18:4263,84862,002,23032,028,0005,004,0002,347,008
46Sep 27, 2025 18:5063,86659,439,66731,036,0006,006,0002,338,816
47Sep 27, 2025 18:4264,01657,002,67934,119,0003,010,0002,297,856
48Sep 27, 2025 18:4464,04060,427,81732,124,0005,019,0002,277,376
49Sep 27, 2025 18:4464,24164,967,44633,232,0004,028,0002,338,816
50Sep 27, 2025 18:4464,48170,423,95331,335,0006,064,0002,330,624
51Sep 27, 2025 18:4464,50063,053,04532,355,0005,055,0002,510,848
52Sep 27, 2025 18:4464,58458,752,50733,410,0004,049,0002,310,144
53Sep 27, 2025 18:4464,77860,142,52231,479,0006,092,0002,519,040
54Sep 27, 2025 18:4464,84564,197,74332,528,0005,082,0002,297,856
55Sep 27, 2025 18:4265,12864,809,15330,816,0006,958,0002,490,368
56Sep 27, 2025 18:4365,17965,293,70427,856,0009,948,0002,293,760
57Sep 27, 2025 18:4265,30363,321,81030,899,0006,977,0002,404,352
58Sep 27, 2025 18:5065,32670,926,10129,913,0007,976,0002,424,832
59Sep 27, 2025 18:4265,93161,056,17130,190,0008,050,0002,482,176
60Sep 27, 2025 18:5066,30760,010,91431,374,0007,084,0002,486,272