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 listOct 15, 2024 19:32@drytecc@dryteccScore: 40,213,452Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 15, 2024 19:3140,169,76623,331,926,67823,296,465,0001,999,0002,416,640
2Oct 15, 2024 19:3240,179,71023,337,043,00623,301,234,0002,998,0002,433,024
3Oct 15, 2024 19:3140,182,50023,347,835,48823,302,852,0002,998,0002,281,472
4Oct 15, 2024 19:3140,189,02223,357,792,88023,307,634,0001,999,0002,281,472
5Oct 15, 2024 19:3140,193,85323,350,162,68123,309,437,0002,998,0002,433,024
6Oct 15, 2024 19:3240,200,19023,355,611,00723,314,111,0001,999,0002,293,760
7Oct 15, 2024 19:3140,203,68423,355,675,23323,315,139,0002,998,0002,293,760
8Oct 15, 2024 19:3140,210,83123,357,386,49223,319,284,0002,998,0002,281,472
9Oct 15, 2024 19:3240,211,26623,358,043,45323,318,536,0003,998,0002,281,472
10Oct 15, 2024 19:3240,213,45223,360,091,59723,320,804,0002,998,0002,281,472
11Oct 15, 2024 19:3240,214,77623,360,786,93123,320,572,0003,998,0002,424,832
12Oct 15, 2024 19:3240,233,23623,372,945,57723,334,278,000999,0002,293,760
13Oct 15, 2024 19:3140,242,94123,375,640,24023,338,907,0001,999,0002,281,472
14Oct 15, 2024 19:3140,248,04823,381,915,45723,339,870,0003,998,0002,359,296
15Oct 15, 2024 19:3240,276,07123,399,591,61323,355,123,0004,998,0002,424,832
16Oct 15, 2024 19:3240,301,12923,409,874,16023,372,656,0001,999,0002,281,472
17Oct 15, 2024 19:3140,344,29323,437,547,39523,397,691,0001,999,0002,293,760
18Oct 15, 2024 19:3240,347,18623,440,677,07623,397,370,0003,998,0002,293,760