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 17, 2024 19:20E SequeiraE SequeiraScore: 77,391Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 17, 2024 19:2075,81666,855,05441,975,0001,998,0002,510,848
2Apr 17, 2024 19:2075,86273,186,35142,000,0002,000,0002,265,088
3Apr 17, 2024 19:5675,96470,067,73642,057,0002,002,0002,285,568
4Apr 17, 2024 19:5676,02875,370,31743,094,0001,002,0002,510,848
5Apr 17, 2024 19:3376,05368,255,28942,106,0002,005,0002,510,848
6Apr 17, 2024 19:3376,06671,186,80342,113,0002,005,0002,351,104
7Apr 17, 2024 19:2076,27466,468,42942,229,0002,010,0002,269,184
8Apr 17, 2024 19:5676,43173,684,74442,315,0002,015,0002,269,184
9Apr 17, 2024 19:2076,44373,356,64243,330,0001,007,0002,285,568
10Apr 17, 2024 19:5676,48871,829,66643,355,0001,008,0002,285,568
11Apr 17, 2024 19:5676,52972,177,37643,379,0001,008,0002,260,992
12Apr 17, 2024 19:2076,55967,249,39143,395,0001,009,0002,363,392
13Apr 17, 2024 19:2076,61772,213,42243,429,0001,009,0002,285,568
14Apr 17, 2024 19:3376,68666,429,65343,468,0001,010,0002,510,848
15Apr 17, 2024 19:3376,94075,385,82343,611,0001,014,0002,285,568
16Apr 17, 2024 19:3377,11070,658,39243,708,0001,016,0002,285,568
17Apr 17, 2024 19:3377,37276,267,97843,879,000997,0002,285,568
18Apr 17, 2024 19:2077,38373,949,25043,885,000997,0002,285,568
19Apr 17, 2024 19:5677,39172,729,84843,890,000997,0002,359,296
20Apr 17, 2024 19:5677,40072,782,27043,895,000997,0002,510,848
21Apr 17, 2024 19:3377,40272,191,46942,898,0001,995,0002,510,848
22Apr 17, 2024 19:2077,47265,231,68042,937,0001,997,0002,519,040
23Apr 17, 2024 19:5677,71067,361,92043,069,0002,003,0002,285,568
24Apr 17, 2024 19:5677,75368,267,04243,093,0002,004,0002,260,992
25Apr 17, 2024 19:3377,76268,459,30144,100,0001,002,0002,519,040
26Apr 17, 2024 19:2077,81266,908,02143,126,0002,005,0002,285,568
27Apr 17, 2024 19:3378,02971,578,29344,252,0001,005,0002,260,992
28Apr 17, 2024 19:3379,759119,618,38246,260,50302,232,320
29Apr 17, 2024 19:2079,762113,432,90446,262,17202,220,032
30Apr 17, 2024 19:5679,896109,874,61946,339,87002,224,128
31Apr 17, 2024 19:3380,551115,813,31846,719,29502,228,224
32Apr 17, 2024 19:3380,666123,503,46846,786,46102,256,896
33Apr 17, 2024 19:2080,753100,743,99846,836,84002,228,224
34Apr 17, 2024 19:5680,887107,700,48646,914,46602,232,320
35Apr 17, 2024 19:5681,057104,346,58847,012,84002,232,320
36Apr 17, 2024 19:2081,402111,713,92047,212,95802,244,608