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 listApr 1, 2025 14:47Yuriy LyfenkoYuriy LyfenkoScore: 47,016Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 1, 2025 14:4942,67849,464,70523,722,0001,031,0002,506,752
2Apr 1, 2025 14:4743,18455,506,40023,044,0002,003,0002,469,888
3Apr 1, 2025 14:4943,70952,655,94925,351,00002,510,848
4Apr 1, 2025 14:4943,77649,359,05724,375,0001,015,0002,506,752
5Apr 1, 2025 14:4943,79148,272,03425,399,00002,527,232
6Apr 1, 2025 14:4743,90755,442,21024,448,0001,018,0002,347,008
7Apr 1, 2025 14:4744,27957,479,83925,682,00002,482,176
8Apr 1, 2025 14:4744,28353,257,16525,684,00002,469,888
9Apr 1, 2025 14:4944,36760,441,32624,704,0001,029,0002,506,752
10Apr 1, 2025 14:4945,38147,297,35325,309,0001,012,0002,428,928
11Apr 1, 2025 14:4945,38646,231,88625,312,0001,012,0002,461,696
12Apr 1, 2025 14:4945,99852,630,12726,679,00002,498,560
13Apr 1, 2025 14:4946,04556,276,82026,706,00002,506,752
14Apr 1, 2025 14:4746,16451,275,95826,775,00002,297,856
15Apr 1, 2025 14:4946,37651,336,20524,906,0001,992,0002,457,600
16Apr 1, 2025 14:4747,01655,046,02927,269,00002,424,832
17Apr 1, 2025 14:4747,10054,900,65527,318,00002,490,368
18Apr 1, 2025 14:4947,10757,428,59626,311,0001,011,0002,502,656
19Apr 1, 2025 14:4948,91053,458,72628,368,00002,473,984
20Apr 1, 2025 14:4949,52453,790,00927,699,0001,025,0002,486,272
21Apr 1, 2025 14:4949,95552,940,35326,976,0001,998,0002,293,760
22Apr 1, 2025 14:4950,16754,235,00227,091,0002,006,0002,506,752
23Apr 1, 2025 14:4950,30252,695,50227,163,0002,012,0002,342,912
24Apr 1, 2025 14:4751,05758,469,46428,592,0001,021,0002,453,504
25Apr 1, 2025 14:4751,19356,603,41028,669,0001,023,0002,285,568
26Apr 1, 2025 14:4951,55356,618,47027,908,0001,993,0002,457,600
27Apr 1, 2025 14:4952,50055,454,37829,435,0001,015,0002,502,656
28Apr 1, 2025 14:4795,22679,636,76854,227,0001,004,0002,486,272
29Apr 1, 2025 14:4795,53678,784,52454,404,0001,007,0002,457,600
30Apr 1, 2025 14:4798,04884,949,99854,873,0001,995,0002,498,560