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:35undenuicapundenuicapError
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:3800000Error: expected "1804571 ", got "1941257 "stderr
2Feb 8, 2025 23:4300000Error: expected "1694777 ", got "1709468 "
3Feb 8, 2025 23:4200000Error: expected "2639538 ", got "2655641 "stderr
4Feb 8, 2025 23:4200000Error: expected "1485428 ", got "1498234 "stderr
5Feb 8, 2025 23:4200000Error: expected "3410813 ", got "3412397 "stderr
6Feb 8, 2025 23:4200000Error: expected "1353860 ", got "1363913 "stderr
7Feb 8, 2025 23:4200000Error: expected "2115754 ", got "2119968 "stderr
8Feb 8, 2025 23:4200000Error: expected "1818924 ", got "1852320 "stderr
9Feb 8, 2025 23:4200000Error: expected "1138843 ", got "1153880 "stderr
10Feb 8, 2025 23:4100000Error: expected "881022 ", got "890256 "stderr
11Feb 8, 2025 23:4100000Error: expected "662557 ", got "679316 "stderr
12Feb 8, 2025 23:4000000Error: expected "1512702 ", got "1604472 "stderr
13Feb 8, 2025 23:3900000Error: expected "1558989 ", got "1800920 "
14Feb 8, 2025 23:3900000Error: expected "1342399 ", got "1343736 "stderr
15Feb 8, 2025 23:3900000Error: expected "934511 ", got "1184849 "stderr
16Feb 8, 2025 23:3800000Error: expected "783125 ", got "796221 "stderr
17Feb 8, 2025 23:3700000Error: expected "1494350 ", got "1497702 "stderr
18Feb 8, 2025 23:3700000Error: expected "1390405 ", got "2028344 "stderr
19Feb 8, 2025 23:3600000Error: expected "2274262 ", got "2278780 "stderr
20Feb 8, 2025 23:3600000Error: expected "1412795 ", got "1419020 "
21Feb 8, 2025 23:3500000Error: expected "1036265 ", got "1037810 "stderr
22Feb 8, 2025 23:4270,78468,779,08739,053,0002,002,0002,420,736
23Feb 8, 2025 23:4270,81767,534,00439,071,0002,003,0002,424,832
24Feb 8, 2025 23:4270,83865,194,52939,082,0002,004,0002,416,640
25Feb 8, 2025 23:4270,86770,009,45140,101,0001,002,0002,424,832
26Feb 8, 2025 23:4270,89565,123,07238,111,0003,008,0002,424,832
27Feb 8, 2025 23:4270,93669,665,49340,140,0001,003,0002,531,328
28Feb 8, 2025 23:4271,00762,978,39839,175,0002,009,0002,428,928
29Feb 8, 2025 23:4271,15264,788,84740,262,0001,006,0002,420,736
30Feb 8, 2025 23:4271,16468,087,26539,262,0002,013,0002,531,328