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 19, 2024 08:36E SequeiraE SequeiraScore: 76,553Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 20, 2024 13:5075,14770,442,69043,585,00002,519,040
2Apr 20, 2024 14:1675,19865,179,75242,601,0001,014,0002,285,568
3Apr 20, 2024 14:1675,33672,925,13143,695,00002,285,568
4Apr 19, 2024 08:3675,35274,555,42242,688,0001,016,0002,510,848
5Apr 20, 2024 14:1675,42170,906,24842,727,0001,017,0002,510,848
6Apr 20, 2024 14:1675,61270,850,19942,859,000996,0002,260,992
7Apr 19, 2024 08:3675,65968,439,67741,888,0001,994,0002,342,912
8Apr 19, 2024 08:3675,77273,695,25841,951,0001,997,0002,285,568
9Apr 20, 2024 13:5075,77868,204,78642,953,000998,0002,510,848
10Apr 19, 2024 08:3675,95580,431,63442,052,0002,002,0002,285,568
11Apr 19, 2024 08:3676,05569,185,48842,107,0002,005,0002,510,848
12Apr 19, 2024 08:3676,07863,215,09442,120,0002,005,0002,285,568
13Apr 20, 2024 14:1676,25066,786,25342,215,0002,010,0002,510,848
14Apr 20, 2024 14:1676,29170,470,89042,238,0002,011,0002,379,776
15Apr 20, 2024 14:1676,30571,596,26442,246,0002,011,0002,285,568
16Apr 19, 2024 08:3676,31276,583,37142,250,0002,011,0002,510,848
17Apr 20, 2024 14:1676,32166,324,67642,254,0002,012,0002,285,568
18Apr 19, 2024 08:3676,45367,660,19643,336,0001,007,0002,285,568
19Apr 20, 2024 13:5076,55365,554,37043,392,0001,009,0002,510,848
20Apr 20, 2024 13:5076,61064,941,47143,425,0001,009,0002,510,848
21Apr 20, 2024 13:5076,68667,992,79744,478,00002,285,568
22Apr 20, 2024 13:5076,69074,871,66743,470,0001,010,0002,260,992
23Apr 20, 2024 14:1676,69767,643,67943,473,0001,011,0002,510,848
24Apr 20, 2024 13:5076,69866,096,30443,474,0001,011,0002,285,568
25Apr 20, 2024 13:5076,82269,840,38843,545,0001,012,0002,285,568
26Apr 19, 2024 08:3677,36771,230,96742,879,0001,994,0002,506,752
27Apr 20, 2024 13:5077,39773,211,74642,895,0001,995,0002,519,040
28Apr 20, 2024 13:5078,593107,594,03645,584,02802,248,704
29Apr 20, 2024 14:1678,597121,021,55745,586,13302,228,224
30Apr 20, 2024 13:5078,611101,536,73345,594,31902,232,320
31Apr 19, 2024 08:3678,687113,521,00845,638,62102,228,224
32Apr 20, 2024 14:1678,758104,514,79145,679,82802,220,032
33Apr 19, 2024 08:3678,969116,483,49445,801,85702,236,416
34Apr 20, 2024 14:1679,197115,224,56545,934,08002,232,320
35Apr 20, 2024 13:5079,361119,336,82046,029,28502,232,320
36Apr 19, 2024 08:3679,812110,210,37646,291,18202,232,320