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 11, 2023 22:12PiggyChan-2020PiggyChan-2020Score: 110,819Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 11, 2023 22:10107,34395,402,42160,251,0002,008,0002,273,280
2Dec 11, 2023 22:12108,45991,225,09160,909,0001,997,0002,494,464
3Dec 11, 2023 22:12108,50785,479,41261,936,000998,0002,330,624
4Dec 11, 2023 22:12108,75288,904,40662,075,0001,001,0002,281,472
5Dec 11, 2023 22:12108,83385,924,89462,122,0001,001,0002,498,560
6Dec 11, 2023 22:10108,95088,735,92961,185,0002,006,0002,281,472
7Dec 11, 2023 22:12109,07887,122,01061,257,0002,008,0002,498,560
8Dec 11, 2023 22:10109,72691,362,21762,631,0001,010,0002,494,464
9Dec 11, 2023 22:12110,19087,809,05462,912,000998,0002,506,752
10Dec 11, 2023 22:12110,81991,593,69762,267,0002,008,0002,281,472
11Dec 11, 2023 22:12111,95989,358,05963,937,000999,0002,498,560
12Dec 11, 2023 22:12113,96293,365,63965,097,0001,001,0002,281,472
13Dec 11, 2023 22:10131,863136,640,39076,480,82802,220,032
14Dec 11, 2023 22:10132,421139,271,53076,804,21602,215,936
15Dec 11, 2023 22:12137,887155,269,19479,974,71102,211,840
16Dec 11, 2023 22:12140,227141,414,62581,331,48202,203,648
17Dec 11, 2023 22:12141,113147,133,17081,845,59102,211,840
18Dec 11, 2023 22:10141,697143,307,36982,184,24802,215,936