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 5, 2023 10:19matsuoka-601matsuoka-601Score: 48,650Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 5, 2023 10:1947,78857,448,66726,691,0001,026,0002,318,336
2Dec 5, 2023 10:1947,79553,993,43626,695,0001,026,0002,473,984
3Dec 5, 2023 10:1948,31253,606,18026,020,0002,001,0002,285,568
4Dec 5, 2023 10:1948,33451,431,14626,032,0002,002,0002,473,984
5Dec 5, 2023 10:1948,40551,280,10527,073,0001,002,0002,285,568
6Dec 5, 2023 10:1948,60750,621,66626,179,0002,013,0002,473,984
7Dec 5, 2023 10:1948,65056,079,96927,210,0001,007,0002,285,568
8Dec 5, 2023 10:1948,67152,878,17626,213,0002,016,0002,465,792
9Dec 5, 2023 10:1949,21052,637,16027,523,0001,019,0002,277,376
10Dec 5, 2023 10:1953,70099,252,65631,145,83802,183,168
11Dec 5, 2023 10:1954,279103,003,44531,482,10302,187,264
12Dec 5, 2023 10:1956,83787,063,95932,965,39802,179,072