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:43@drytecc@dryteccError
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:5700000Error: expected "1540161 ", got "1540337 "
2Oct 15, 2024 19:5700000Error: expected "1923839 ", got "1940811 "
3Oct 15, 2024 19:5700000Error: expected "1659218 ", got "1680547 "
4Oct 15, 2024 19:5200000Error: No answer to stdout. Died or killed?
5Oct 15, 2024 19:5100000Error: No answer to stdout. Died or killed?
6Oct 15, 2024 19:4400000Error: No answer to stdout. Died or killed?
7Oct 15, 2024 19:4300000Error: No answer to stdout. Died or killed?
8Oct 15, 2024 19:4300000Error: No answer to stdout. Died or killed?
9Oct 15, 2024 19:4300000Error: No answer to stdout. Died or killed?
10Oct 15, 2024 19:4463,81261,462,61135,011,0002,000,0002,461,696
11Oct 15, 2024 19:4464,05560,707,68034,140,0003,012,0002,310,144
12Oct 15, 2024 19:4464,36460,264,75234,305,0003,026,0002,433,024
13Oct 15, 2024 19:4464,39159,246,14833,310,0004,037,0002,461,696
14Oct 15, 2024 19:4464,86463,902,89834,571,0003,050,0002,465,792
15Oct 15, 2024 19:4465,37169,302,17934,922,0002,993,0002,465,792
16Oct 15, 2024 19:4466,06465,543,45533,276,0005,041,0002,473,984
17Oct 15, 2024 19:4466,46761,922,82635,508,0003,043,0002,469,888
18Oct 15, 2024 19:4466,55261,829,56935,553,0003,047,0002,473,984
19Oct 15, 2024 19:5770,09567,767,34436,590,0004,065,0002,404,352
20Oct 15, 2024 19:5770,75368,671,10638,035,0003,002,0002,338,816
21Oct 15, 2024 19:5770,89868,525,52839,116,0002,005,0002,469,888
22Oct 15, 2024 19:5770,99065,068,06438,162,0003,012,0002,490,368
23Oct 15, 2024 19:5771,05366,038,78638,196,0003,015,0002,465,792
24Oct 15, 2024 19:5771,10964,520,44639,232,0002,011,0002,465,792