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 listMar 27, 2024 09:26E SequeiraE SequeiraScore: 79,810Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 09:2676,17167,512,22242,171,0002,008,0002,506,752
2Mar 27, 2024 09:2676,28868,138,57642,236,0002,011,0002,281,472
3Mar 27, 2024 09:2676,37867,595,28042,286,0002,013,0002,281,472
4Mar 27, 2024 09:2676,40564,524,24042,301,0002,014,0002,293,760
5Mar 27, 2024 09:2676,52971,187,08243,379,0001,008,0002,510,848
6Mar 27, 2024 09:2676,65768,835,69743,451,0001,010,0002,281,472
7Mar 27, 2024 09:2676,78872,044,89343,525,0001,012,0002,506,752
8Mar 27, 2024 09:2677,05976,399,47043,679,0001,015,0002,347,008
9Mar 27, 2024 09:2677,07675,045,75043,688,0001,016,0002,281,472
10Mar 27, 2024 09:2679,65967,739,41644,194,0002,008,0002,277,376
11Mar 27, 2024 09:2679,81071,312,07844,278,0002,012,0002,355,200
12Mar 27, 2024 09:2680,24169,202,62045,529,0001,011,0002,519,040
13Mar 27, 2024 09:2680,25966,913,83145,539,0001,011,0002,510,848
14Mar 27, 2024 09:2680,36467,508,27945,598,0001,013,0002,506,752
15Mar 27, 2024 09:2680,43672,506,43745,639,0001,014,0002,285,568
16Mar 27, 2024 09:2680,46073,125,24845,653,0001,014,0002,285,568
17Mar 27, 2024 09:2680,80770,840,04745,871,000997,0002,285,568
18Mar 27, 2024 09:2681,70375,572,65346,380,0001,008,0002,285,568
19Mar 27, 2024 09:2682,406126,336,92947,795,22702,228,224
20Mar 27, 2024 09:2683,492111,750,72148,425,28602,228,224
21Mar 27, 2024 09:2684,434113,013,36048,971,58102,252,800