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 08:54Sergey StreminSergey StreminScore: 82,091Success
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 08:5481,262105,490,19247,131,89602,314,240
2Dec 5, 2023 08:5481,28373,171,82545,138,0002,006,0002,363,392
3Dec 5, 2023 08:5481,64170,241,81447,352,00002,363,392
4Dec 5, 2023 08:5481,85271,022,69446,464,0001,010,0002,367,488
5Dec 5, 2023 08:5481,88374,047,19246,482,0001,010,0002,363,392
6Dec 5, 2023 08:5481,90068,707,70047,502,00002,367,488
7Dec 5, 2023 08:5481,93672,910,37847,523,00002,363,392
8Dec 5, 2023 08:5481,94374,814,27446,516,0001,011,0002,363,392
9Dec 5, 2023 08:5481,957106,291,37047,535,22702,314,240
10Dec 5, 2023 08:5482,00371,370,51946,551,0001,011,0002,281,472
11Dec 5, 2023 08:5482,00771,980,17447,564,00002,363,392
12Dec 5, 2023 08:5482,021117,553,94947,571,89302,301,952
13Dec 5, 2023 08:5482,091108,927,57247,612,66802,306,048
14Dec 5, 2023 08:5482,36472,219,97446,755,0001,016,0002,363,392
15Dec 5, 2023 08:5482,495114,297,93347,847,19902,314,240
16Dec 5, 2023 08:5482,51975,282,47546,864,000997,0002,281,472
17Dec 5, 2023 08:5482,53175,206,68345,874,0001,994,0002,363,392
18Dec 5, 2023 08:5482,55275,261,88945,885,0001,995,0002,281,472
19Dec 5, 2023 08:5482,56674,784,68945,893,0001,995,0002,457,600
20Dec 5, 2023 08:5482,568122,775,49147,889,15902,310,144
21Dec 5, 2023 08:5482,57276,463,83845,897,0001,995,0002,355,200
22Dec 5, 2023 08:5482,58173,797,45846,900,000997,0002,281,472
23Dec 5, 2023 08:5482,66979,261,02745,951,0001,997,0002,375,680
24Dec 5, 2023 08:5484,01674,666,87947,714,0001,015,0002,281,472