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 17, 2023 05:19Kenneth MaplesKenneth MaplesScore: 90,488Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 17, 2023 05:2178,898105,849,94245,760,68902,285,568
2Dec 17, 2023 05:1978,914120,722,67745,770,09802,285,568
3Dec 17, 2023 05:2179,105114,278,09645,881,08002,293,760
4Dec 17, 2023 05:1979,362112,635,48346,029,97902,289,664
5Dec 17, 2023 05:2179,433117,130,55346,071,12902,297,856
6Dec 17, 2023 05:1980,102116,867,46246,459,17002,293,760
7Dec 17, 2023 05:2189,90079,251,47549,134,0003,008,0002,326,528
8Dec 17, 2023 05:2189,93180,329,31051,157,0001,003,0002,519,040
9Dec 17, 2023 05:2190,27278,771,88551,352,0001,006,0002,519,040
10Dec 17, 2023 05:1990,30077,829,65051,367,0001,007,0002,256,896
11Dec 17, 2023 05:2190,42676,834,19851,439,0001,008,0002,519,040
12Dec 17, 2023 05:1990,42674,632,87952,447,00002,322,432
13Dec 17, 2023 05:2190,48874,952,56851,474,0001,009,0002,322,432
14Dec 17, 2023 05:2190,49878,153,61452,489,00002,519,040
15Dec 17, 2023 05:1990,64877,910,78451,565,0001,011,0002,510,848
16Dec 17, 2023 05:1990,77683,747,63151,638,0001,012,0002,445,312
17Dec 17, 2023 05:1991,17282,624,03351,883,000997,0002,256,896
18Dec 17, 2023 05:2191,18181,047,11351,888,000997,0002,256,896
19Dec 17, 2023 05:2191,19877,706,62750,899,0001,996,0002,510,848
20Dec 17, 2023 05:1991,47977,208,47452,057,0001,001,0002,510,848
21Dec 17, 2023 05:2191,51780,772,98851,077,0002,003,0002,330,624
22Dec 17, 2023 05:1991,94072,061,47852,319,0001,006,0002,510,848
23Dec 17, 2023 05:1992,43374,386,47153,611,00002,519,040
24Dec 17, 2023 05:1992,92879,699,32152,882,0001,016,0002,519,040