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 listDec 12, 2024 18:45Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 12, 2024 18:4500000Error: expected "1793115 ", got "1793218 "
2Dec 12, 2024 18:4500000Error: expected "1155701 ", got "1163796 "
3Dec 12, 2024 18:4600000Error: expected "1998498 ", got "2000194 "
4Dec 14, 2024 19:5900000Error: expected "2773792 ", got "2774136 "
5Dec 12, 2024 18:4536,61748,795,20220,227,0001,011,0002,285,568
6Dec 12, 2024 18:4536,78343,473,27020,319,0001,015,0002,285,568
7Dec 12, 2024 18:4536,82644,173,38720,342,0001,017,0002,404,352
8Dec 12, 2024 18:4537,86044,863,59120,961,000998,0002,285,568
9Dec 12, 2024 18:4537,86248,249,92119,964,0001,996,0002,285,568
10Dec 12, 2024 18:4538,17449,053,84721,135,0001,006,0002,285,568
11Dec 14, 2024 19:5938,33146,528,36520,211,0002,021,0002,269,184
12Dec 14, 2024 19:5838,46645,349,23420,282,0002,028,0002,273,280
13Dec 14, 2024 19:5938,82843,804,00020,473,0002,047,0002,277,376
14Dec 14, 2024 19:5839,87652,632,64821,117,0002,011,0002,285,568
15Dec 14, 2024 19:5941,16954,541,30921,889,0001,989,0002,449,408
16Dec 14, 2024 19:5841,25751,966,95721,935,0001,994,0002,285,568