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 5, 2023 09:28matsuoka-601matsuoka-601Score: 49,967Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 5, 2023 09:3048,69095,519,71428,240,31502,306,048
2Dec 5, 2023 09:3048,78989,417,95428,297,76102,174,976
3Dec 5, 2023 09:2848,856105,208,79628,336,41702,183,168
4Dec 5, 2023 09:3049,041117,721,32828,444,03802,306,048
5Dec 5, 2023 09:2849,07494,653,07128,462,72502,256,896
6Dec 5, 2023 09:3049,318104,819,90228,604,26102,179,072
7Dec 5, 2023 09:2849,463109,220,93528,688,53302,252,800
8Dec 5, 2023 09:2849,56258,700,26128,746,00002,285,568
9Dec 5, 2023 09:3049,78492,159,30728,874,86602,183,168
10Dec 5, 2023 09:3049,80256,715,68226,893,0001,992,0002,277,376
11Dec 5, 2023 09:3049,80753,298,94828,888,00002,473,984
12Dec 5, 2023 09:2849,80957,441,46426,897,0001,992,0002,277,376
13Dec 5, 2023 09:3049,81953,521,00126,903,0001,992,0002,478,080
14Dec 5, 2023 09:3049,82256,596,82727,901,000996,0002,285,568
15Dec 5, 2023 09:3049,82457,077,39127,902,000996,0002,277,376
16Dec 5, 2023 09:3049,83659,906,08326,912,0001,993,0002,277,376
17Dec 5, 2023 09:3049,91253,415,63226,953,0001,996,0002,473,984
18Dec 5, 2023 09:3049,91955,732,52827,955,000998,0002,285,568
19Dec 5, 2023 09:2849,96751,036,92626,983,0001,998,0002,478,080
20Dec 5, 2023 09:3049,96950,651,44626,984,0001,998,0002,482,176
21Dec 5, 2023 09:3050,05352,168,70027,029,0002,002,0002,285,568
22Dec 5, 2023 09:3050,14051,812,03227,076,0002,005,0002,473,984
23Dec 5, 2023 09:3050,14156,056,83827,077,0002,005,0002,473,984
24Dec 5, 2023 09:3050,14393,015,07329,082,89902,179,072
25Dec 5, 2023 09:3050,18852,192,34028,106,0001,003,0002,306,048
26Dec 5, 2023 09:2850,19154,152,93527,104,0002,007,0002,490,368
27Dec 5, 2023 09:3050,24554,918,64727,133,0002,009,0002,486,272
28Dec 5, 2023 09:3050,25752,545,35128,144,0001,005,0002,482,176
29Dec 5, 2023 09:2850,29355,379,26528,165,0001,005,0002,285,568
30Dec 5, 2023 09:2850,30352,995,16827,164,0002,012,0002,277,376
31Dec 5, 2023 09:2850,36052,186,81727,195,0002,014,0002,478,080
32Dec 5, 2023 09:2850,38851,391,01227,210,0002,015,0002,482,176
33Dec 5, 2023 09:3050,41051,895,51028,230,0001,008,0002,285,568
34Dec 5, 2023 09:2850,50056,868,49927,270,0002,020,0002,277,376
35Dec 5, 2023 09:3050,72152,483,59428,404,0001,014,0002,473,984
36Dec 5, 2023 09:3051,81658,714,22628,050,0002,003,0002,469,888