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 14:53Sergey StreminSergey StreminScore: 78,464Success
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 14:5376,80266,722,09544,545,00002,342,912
2Dec 5, 2023 14:5476,98469,328,28543,637,0001,014,0002,281,472
3Dec 5, 2023 14:5477,00274,958,48943,646,0001,015,0002,281,472
4Dec 5, 2023 14:5477,10968,746,93544,723,00002,281,472
5Dec 5, 2023 14:5377,66968,504,95743,046,0002,002,0002,359,296
6Dec 5, 2023 14:5477,67975,576,15845,054,00002,281,472
7Dec 5, 2023 14:5377,74366,777,03743,087,0002,004,0002,281,472
8Dec 5, 2023 14:5377,907119,575,29845,186,31302,297,856
9Dec 5, 2023 14:5378,104117,158,11045,300,35902,289,664
10Dec 5, 2023 14:5378,14167,580,24844,315,0001,007,0002,281,472
11Dec 5, 2023 14:5478,158109,980,95645,331,81402,289,664
12Dec 5, 2023 14:5378,27873,589,68644,393,0001,008,0002,281,472
13Dec 5, 2023 14:5378,46468,391,47045,509,00002,355,200
14Dec 5, 2023 14:5478,71474,919,94845,654,00002,281,472
15Dec 5, 2023 14:5379,09374,704,50244,877,000997,0002,453,504
16Dec 5, 2023 14:5479,43869,557,44944,071,0002,003,0002,281,472
17Dec 5, 2023 14:5479,486111,094,82246,101,99102,289,664
18Dec 5, 2023 14:5480,46667,482,91345,656,0001,014,0002,355,200
19Dec 5, 2023 14:5380,61777,112,91845,764,000994,0002,281,472
20Dec 5, 2023 14:5481,392114,961,00747,207,40902,289,664
21Dec 5, 2023 14:5381,50767,914,18146,269,0001,005,0002,351,104
22Dec 5, 2023 14:5384,077109,830,77448,764,66302,301,952
23Dec 5, 2023 14:5485,45269,880,78949,562,00002,355,200
24Dec 5, 2023 14:5488,53376,587,61651,349,00002,281,472