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 listApr 2, 2025 14:51Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 2, 2025 14:5100000Testing system error
2Apr 2, 2025 14:5000000Testing system error
3Apr 2, 2025 14:5442,59847,445,69524,707,00002,527,232
4Apr 2, 2025 14:5243,53652,471,85624,241,0001,010,0002,437,120
5Apr 2, 2025 14:5443,63448,800,62224,296,0001,012,0002,490,368
6Apr 2, 2025 14:5244,18150,579,78924,600,0001,025,0002,506,752
7Apr 2, 2025 14:5444,31051,828,35024,672,0001,028,0002,342,912
8Apr 2, 2025 14:5444,31050,155,59424,672,0001,028,0002,523,136
9Apr 2, 2025 14:5444,69350,180,09524,925,000997,0002,478,080
10Apr 2, 2025 14:5244,92848,241,28925,056,0001,002,0002,498,560
11Apr 2, 2025 14:5444,94150,742,10424,061,0002,005,0002,506,752
12Apr 2, 2025 14:5445,08351,263,80724,137,0002,011,0002,510,848
13Apr 2, 2025 14:5445,20950,120,21524,204,0002,017,0002,330,624
14Apr 2, 2025 14:5445,42948,754,26826,349,00002,506,752
15Apr 2, 2025 14:5245,46248,429,02125,354,0001,014,0002,314,240
16Apr 2, 2025 14:5245,87652,656,28526,608,00002,478,080
17Apr 2, 2025 14:5245,89053,828,41925,593,0001,023,0002,502,656
18Apr 2, 2025 14:5246,40348,455,07824,921,0001,993,0002,502,656
19Apr 2, 2025 14:5246,40347,839,86824,921,0001,993,0002,293,760
20Apr 2, 2025 14:5446,89551,174,95726,192,0001,007,0002,322,432
21Apr 2, 2025 14:5247,42849,503,01626,490,0001,018,0002,293,760
22Apr 2, 2025 14:5447,70554,925,88526,645,0001,024,0002,289,664
23Apr 2, 2025 14:5248,20553,926,10825,962,0001,997,0002,445,312
24Apr 2, 2025 14:5448,64357,020,87527,206,0001,007,0002,461,696
25Apr 2, 2025 14:5249,29551,649,01527,570,0001,021,0002,437,120
26Apr 2, 2025 14:5249,42254,128,36728,665,00002,449,408
27Apr 2, 2025 14:5449,54561,067,68628,736,00002,265,088
28Apr 2, 2025 14:5449,63655,451,33227,797,000992,0002,461,696
29Apr 2, 2025 14:5251,02953,170,93528,577,0001,020,0002,412,544
30Apr 2, 2025 14:5451,54558,589,13528,900,000996,0002,457,600
31Apr 2, 2025 14:5251,75550,334,14728,017,0002,001,0002,379,776
32Apr 2, 2025 14:5255,09758,428,69930,958,000998,0002,490,368
33Apr 2, 2025 14:5196,34892,522,68553,887,0001,995,0002,322,432
34Apr 2, 2025 14:5197,61981,850,41855,608,0001,011,0002,441,216
35Apr 2, 2025 14:5198,51080,948,60056,134,0001,002,0002,269,184