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 listApr 17, 2024 20:08E SequeiraE SequeiraScore: 77,143Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 17, 2024 20:0875,68870,925,37042,902,000997,0002,519,040
2Apr 17, 2024 20:0875,69071,995,32341,905,0001,995,0002,265,088
3Apr 17, 2024 20:0875,87869,271,83842,009,0002,000,0002,519,040
4Apr 17, 2024 20:0875,89079,700,17342,016,0002,000,0002,510,848
5Apr 17, 2024 20:0876,09867,114,01943,134,0001,003,0002,519,040
6Apr 17, 2024 20:0876,60071,589,33243,419,0001,009,0002,277,376
7Apr 17, 2024 20:0877,14369,147,28143,727,0001,016,0002,273,280
8Apr 17, 2024 20:0877,15974,975,97543,735,0001,017,0002,510,848
9Apr 17, 2024 20:0877,47267,553,19342,937,0001,997,0002,506,752
10Apr 17, 2024 20:0879,569111,519,39746,149,97202,228,224
11Apr 17, 2024 20:0880,505123,190,82346,692,98702,248,704
12Apr 17, 2024 20:0881,152121,492,30547,068,12902,224,128