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:36matsuoka-601matsuoka-601Score: 36,864Success
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:3728,82243,459,69315,673,0001,044,0002,273,280
2Oct 17, 2024 12:3730,57842,171,58516,692,0001,043,0002,428,928
3Oct 17, 2024 12:3732,02439,744,30117,543,0001,031,0002,273,280
4Oct 17, 2024 12:3632,28849,082,24117,687,0001,040,0002,535,424
5Oct 17, 2024 12:3632,61945,328,52916,928,0001,991,0002,273,280
6Oct 17, 2024 12:3632,94841,225,11417,099,0002,011,0002,273,280
7Oct 17, 2024 12:3633,02844,035,05017,140,0002,016,0002,424,832
8Oct 17, 2024 12:3734,42639,891,10318,969,000998,0002,273,280
9Oct 17, 2024 12:3735,00344,711,08819,287,0001,015,0002,420,736
10Oct 17, 2024 12:3735,70948,113,13319,676,0001,035,0002,420,736
11Oct 17, 2024 12:3636,86434,806,77920,363,0001,018,0002,510,848
12Oct 17, 2024 12:3637,09345,947,65621,514,00002,273,280
13Oct 17, 2024 12:3637,46253,199,89620,694,0001,034,0002,535,424
14Oct 17, 2024 12:3737,87139,268,65319,969,0001,996,0002,297,856
15Oct 17, 2024 12:3638,30047,846,73720,195,0002,019,0002,433,024
16Oct 17, 2024 12:36895,505546,833,097515,390,0004,003,0002,375,680
17Oct 17, 2024 12:36912,041557,600,712525,985,0002,999,0002,375,680
18Oct 17, 2024 12:36913,503554,458,767528,833,000999,0002,383,872
19Oct 17, 2024 12:361,979,7361,171,157,2301,146,249,0001,998,0002,535,424
20Oct 17, 2024 12:371,984,8411,178,274,9971,150,209,000999,0002,449,408
21Oct 17, 2024 12:371,987,8641,177,493,0951,152,961,00002,273,280