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 17, 2024 12:41@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 17, 2024 12:4100000Error: expected "1431475 ", got "1428121 "
2Oct 17, 2024 12:4100000Error: expected "1593296 ", got "1646620 "
3Oct 17, 2024 12:4100000Error: expected "612748 ", got "612858 "
4Oct 17, 2024 12:4100000Error: expected "1225425 ", got "1223617 "
5Oct 17, 2024 12:4100000Error: expected "1199256 ", got "1549446 "
6Oct 17, 2024 12:4100000Error: expected "3197370 ", got "3197952 "
7Oct 17, 2024 12:4100000Error: expected "1120395 ", got "1122178 "
8Oct 17, 2024 12:4100000Error: expected "1386159 ", got "1386701 "
9Oct 17, 2024 12:4100000Error: No answer to stdout. Died or killed?
10Oct 17, 2024 12:4100000Error: expected "585725 ", got "580992 "
11Oct 17, 2024 12:4200000Error: expected "797604 ", got "798244 "
12Oct 17, 2024 12:4200000Error: expected "638682 ", got "629637 "
13Oct 17, 2024 12:4200000Error: expected "1342970 ", got "1390707 "
14Oct 17, 2024 12:4200000Error: expected "832022 ", got "839988 "
15Oct 17, 2024 12:4200000Error: expected "1475352 ", got "1511163 "
16Oct 17, 2024 12:4200000Error: expected "1806369 ", got "1806732 "
17Oct 17, 2024 12:4200000Error: No answer to stdout. Died or killed?
18Oct 17, 2024 12:4200000Error: expected "1656785 ", got "1664706 "
19Oct 17, 2024 12:4100000Error: expected "1118487 ", got "5493752 "
20Oct 17, 2024 12:4100000Error: expected "2055590 ", got "2083420 "
21Oct 17, 2024 12:4100000Error: expected "2055937 ", got "2024209 "
22Oct 17, 2024 12:4100000Error: expected "882105 ", got "882777 "
23Oct 17, 2024 12:4100000Error: expected "1828494 ", got "2040138 "
24Oct 17, 2024 12:4100000Error: expected "2075229 ", got "2076903 "
25Oct 17, 2024 12:4100000Error: expected "1890558 ", got "2126007 "
26Oct 17, 2024 12:4100000Error: expected "2714114 ", got "2885039 "
27Oct 17, 2024 12:4137,45952,220,89218,623,0003,103,0002,510,848
28Oct 17, 2024 12:4137,67846,579,75219,867,0001,986,0002,482,176
29Oct 17, 2024 12:4137,74754,457,16519,903,0001,990,0002,469,888
30Oct 17, 2024 12:4137,75546,419,72019,908,0001,990,0002,465,792
31Oct 17, 2024 12:4137,76653,701,93719,913,0001,991,0002,465,792
32Oct 17, 2024 12:4138,08449,970,28418,073,0004,016,0002,469,888
33Oct 17, 2024 12:4138,12246,729,71519,096,0003,015,0002,469,888
34Oct 17, 2024 12:4138,32644,648,63417,177,0005,052,0002,510,848
35Oct 17, 2024 12:4140,72154,376,76721,565,0002,053,0002,363,392