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 29, 2024 23:42Louis PonetLouis PonetScore: 246,848Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2024 23:42243,202170,826,581139,057,0002,000,0002,338,816
2Dec 29, 2024 23:42243,924167,898,394140,473,0001,003,0002,502,656
3Dec 29, 2024 23:43243,933169,402,943140,478,0001,003,0002,494,464
4Dec 29, 2024 23:42244,119167,951,870140,585,0001,004,0002,473,984
5Dec 29, 2024 23:42246,159169,449,440140,776,0001,996,0002,514,944
6Dec 29, 2024 23:42246,245166,853,301140,825,0001,997,0002,310,144
7Dec 29, 2024 23:42246,421169,619,731140,926,0001,998,0002,502,656
8Dec 29, 2024 23:43246,443167,826,796140,938,0001,999,0002,330,624
9Dec 29, 2024 23:43246,591170,609,656142,023,0001,000,0002,297,856
10Dec 29, 2024 23:42246,848167,551,407142,171,0001,001,0002,306,048
11Dec 29, 2024 23:42247,264165,126,673142,411,0001,002,0002,338,816
12Dec 29, 2024 23:43247,474168,932,979142,532,0001,003,0002,510,848
13Dec 29, 2024 23:43248,007172,369,008142,846,000998,0002,310,144
14Dec 29, 2024 23:42248,131172,366,946141,918,0001,998,0002,334,720
15Dec 29, 2024 23:42248,719174,101,873142,254,0002,003,0002,326,528
16Dec 29, 2024 23:42251,367168,221,351144,795,000998,0002,490,368
17Dec 29, 2024 23:43251,384169,733,797144,805,000998,0002,478,080
18Dec 29, 2024 23:42251,809179,514,818145,049,0001,000,0002,506,752