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 listMar 26, 2024 21:04E SequeiraE SequeiraScore: 80,493Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 26, 2024 21:0375,90066,159,49642,021,0002,001,0002,281,472
2Mar 26, 2024 21:0476,29368,428,49242,239,0002,011,0002,281,472
3Mar 26, 2024 21:0376,49567,130,41842,351,0002,016,0002,281,472
4Mar 26, 2024 21:0476,74371,847,81843,500,0001,011,0002,277,376
5Mar 26, 2024 21:0376,80273,308,23743,533,0001,012,0002,347,008
6Mar 26, 2024 21:0478,49172,489,09644,514,0001,011,0002,289,664
7Mar 26, 2024 21:0379,47870,398,54544,093,0002,004,0002,285,568
8Mar 26, 2024 21:0379,49069,898,93344,100,0002,004,0002,285,568
9Mar 26, 2024 21:0479,55569,482,16245,139,0001,003,0002,506,752
10Mar 26, 2024 21:0379,55576,816,42244,136,0002,006,0002,510,848
11Mar 26, 2024 21:0379,67867,145,57744,204,0002,009,0002,285,568
12Mar 26, 2024 21:0479,77272,319,94444,257,0002,011,0002,285,568
13Mar 26, 2024 21:0379,99773,742,28345,390,0001,008,0002,510,848
14Mar 26, 2024 21:0480,10567,064,00945,451,0001,010,0002,269,184
15Mar 26, 2024 21:0380,24372,762,26545,530,0001,011,0002,285,568
16Mar 26, 2024 21:0480,49375,984,19346,686,00002,506,752
17Mar 26, 2024 21:0380,56072,308,58645,710,0001,015,0002,285,568
18Mar 26, 2024 21:0480,78673,882,56645,860,000996,0002,285,568
19Mar 26, 2024 21:0480,81073,747,28545,873,000997,0002,355,200
20Mar 26, 2024 21:0480,81970,177,15745,878,000997,0002,273,280
21Mar 26, 2024 21:0380,83475,803,24545,887,000997,0002,510,848
22Mar 26, 2024 21:0380,94574,256,93044,951,0001,997,0002,285,568
23Mar 26, 2024 21:0480,96674,965,12144,962,0001,998,0002,285,568
24Mar 26, 2024 21:0481,30973,463,25245,153,0002,006,0002,510,848
25Mar 26, 2024 21:0481,326114,905,80447,168,99502,232,320
26Mar 26, 2024 21:0381,580111,169,62747,316,13202,224,128
27Mar 26, 2024 21:0381,735115,041,97547,406,26602,224,128
28Mar 26, 2024 21:0381,749130,982,19047,414,46602,220,032
29Mar 26, 2024 21:0482,076114,955,58547,604,36602,224,128
30Mar 26, 2024 21:0482,925102,989,27748,096,62302,228,224