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:10Sergey StreminSergey StreminScore: 81,443Success
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:1080,571109,879,33746,731,04502,310,144
2Dec 5, 2023 09:1080,706118,213,04046,809,43802,314,240
3Dec 5, 2023 09:1080,80968,964,92045,872,000997,0002,281,472
4Dec 5, 2023 09:1080,869125,004,86246,904,08302,301,952
5Dec 5, 2023 09:1080,958115,351,26846,955,53002,310,144
6Dec 5, 2023 09:1081,028106,648,69646,996,45902,191,360
7Dec 5, 2023 09:1081,033116,463,75846,999,30802,310,144
8Dec 5, 2023 09:1081,05277,291,44146,010,0001,000,0002,281,472
9Dec 5, 2023 09:1081,06272,671,90346,016,0001,000,0002,367,488
10Dec 5, 2023 09:1081,06475,607,67846,017,0001,000,0002,473,984
11Dec 5, 2023 09:1081,16469,805,38246,074,0001,001,0002,363,392
12Dec 5, 2023 09:1081,18469,450,30146,086,0001,001,0002,281,472
13Dec 5, 2023 09:1081,44380,923,67145,227,0002,010,0002,363,392
14Dec 5, 2023 09:1081,46274,001,87245,238,0002,010,0002,359,296
15Dec 5, 2023 09:1081,47173,127,79345,243,0002,010,0002,363,392
16Dec 5, 2023 09:1081,53169,492,87945,276,0002,012,0002,363,392
17Dec 5, 2023 09:1081,56476,179,28547,307,00002,281,472
18Dec 5, 2023 09:1081,59075,842,11746,316,0001,006,0002,277,376
19Dec 5, 2023 09:1081,64177,053,76746,345,0001,007,0002,367,488
20Dec 5, 2023 09:1081,65071,644,14846,350,0001,007,0002,355,200
21Dec 5, 2023 09:1081,68674,062,94446,370,0001,008,0002,367,488
22Dec 5, 2023 09:1081,99771,101,49347,558,00002,277,376
23Dec 5, 2023 09:1082,01470,773,03447,568,00002,277,376
24Dec 5, 2023 09:1082,09169,495,90046,600,0001,013,0002,281,472