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 listFeb 8, 2025 23:12undenuicapundenuicapError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 8, 2025 23:1300000Error: expected "2465359 ", got "2491497 "
2Feb 8, 2025 23:1300000Error: expected "1031309 ", got "1037870 "
3Feb 8, 2025 23:1300000Error: expected "2861468 ", got "2920254 "
4Feb 8, 2025 23:1200000Error: expected "1143811 ", got "1144466 "stderr
5Feb 8, 2025 23:1200000Error: expected "1444102 ", got "1485597 "
6Feb 8, 2025 23:1200000Error: expected "1938915 ", got "1938972 "stderr
7Feb 8, 2025 23:1200000Error: expected "2628533 ", got "2694180 "stderr
8Feb 8, 2025 23:1200000Error: expected "1432591 ", got "1480722 "stderr
9Feb 8, 2025 23:1275,95070,751,16243,050,0001,001,0002,535,424
10Feb 8, 2025 23:1276,12466,198,43342,146,0002,006,0002,535,424
11Feb 8, 2025 23:1376,12872,704,36343,151,0001,003,0002,424,832
12Feb 8, 2025 23:1376,19569,188,70842,185,0002,008,0002,531,328
13Feb 8, 2025 23:1376,20964,686,39443,197,0001,004,0002,531,328
14Feb 8, 2025 23:1376,22468,011,95243,206,0001,004,0002,535,424
15Feb 8, 2025 23:1376,25071,443,80142,215,0002,010,0002,531,328
16Feb 8, 2025 23:1376,27470,211,44043,234,0001,005,0002,531,328
17Feb 8, 2025 23:1376,29773,955,91142,241,0002,011,0002,531,328
18Feb 8, 2025 23:1476,30965,807,22242,248,0002,011,0002,531,328
19Feb 8, 2025 23:1376,37968,559,93942,287,0002,013,0002,535,424
20Feb 8, 2025 23:1376,38468,561,11942,290,0002,013,0002,420,736
21Feb 8, 2025 23:1376,40973,938,86044,317,00002,420,736
22Feb 8, 2025 23:1476,46972,846,86843,344,0001,008,0002,424,832
23Feb 8, 2025 23:1476,55072,561,85044,399,00002,420,736
24Feb 8, 2025 23:1276,62872,207,56344,444,00002,527,232
25Feb 8, 2025 23:1376,63466,127,26944,448,00002,428,928
26Feb 8, 2025 23:1376,64371,171,87242,433,0002,020,0002,420,736