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 16:53E SequeiraE SequeiraScore: 82,816Success
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 16:5382,15773,791,07746,638,0001,013,0002,510,848
2Mar 26, 2024 17:0582,162116,732,95247,654,17802,211,840
3Mar 26, 2024 16:5382,22977,196,42347,693,00002,285,568
4Mar 26, 2024 17:1282,23873,618,71846,684,0001,014,0002,285,568
5Mar 26, 2024 16:5482,292116,292,60047,729,35602,215,936
6Mar 26, 2024 16:5382,302102,595,93947,734,87902,207,744
7Mar 26, 2024 16:5482,312110,497,72747,740,74102,211,840
8Mar 26, 2024 16:5382,336121,012,19647,755,04802,211,840
9Mar 26, 2024 16:5382,402109,514,51347,793,06202,211,840
10Mar 26, 2024 17:3482,436113,361,93647,812,78002,215,936
11Mar 26, 2024 16:5382,510103,469,93447,855,57502,215,936
12Mar 26, 2024 16:5782,520106,871,76047,861,42702,215,936
13Mar 26, 2024 17:3482,535127,064,09847,870,51802,211,840
14Mar 26, 2024 17:3482,54178,231,48145,880,0001,994,0002,273,280
15Mar 26, 2024 16:5482,55575,927,95146,885,000997,0002,285,568
16Mar 26, 2024 16:5382,55974,235,33846,887,000997,0002,285,568
17Mar 26, 2024 16:5382,56475,723,31146,890,000997,0002,285,568
18Mar 26, 2024 16:5482,56474,219,15245,892,0001,995,0002,519,040
19Mar 26, 2024 16:5382,56977,929,14145,895,0001,995,0002,273,280
20Mar 26, 2024 17:3482,57674,755,69045,899,0001,995,0002,519,040
21Mar 26, 2024 17:3482,58672,972,65645,905,0001,995,0002,285,568
22Mar 26, 2024 16:5782,58679,652,04545,905,0001,995,0002,273,280
23Mar 26, 2024 16:5482,59073,481,32346,883,0001,019,0002,510,848
24Mar 26, 2024 17:3482,59575,384,58845,909,0001,996,0002,285,568
25Mar 26, 2024 17:3482,60974,403,94045,917,0001,996,0002,285,568
26Mar 26, 2024 17:0582,64178,864,48146,934,000998,0002,285,568
27Mar 26, 2024 17:3482,66070,679,77245,946,0001,997,0002,510,848
28Mar 26, 2024 16:5782,66776,459,40945,950,0001,997,0002,285,568
29Mar 26, 2024 17:1282,67871,852,21146,954,000999,0002,285,568
30Mar 26, 2024 16:5382,69171,049,73345,963,0001,998,0002,285,568
31Mar 26, 2024 16:5382,69871,759,46445,967,0001,998,0002,285,568
32Mar 26, 2024 16:5382,704109,132,70047,968,31702,215,936
33Mar 26, 2024 17:0582,71476,712,99045,976,0001,998,0002,285,568
34Mar 26, 2024 16:5382,72277,072,59745,980,0001,999,0002,285,568
35Mar 26, 2024 17:0582,72271,274,89145,980,0001,999,0002,285,568
36Mar 26, 2024 17:3482,72670,558,37246,982,000999,0002,285,568
37Mar 26, 2024 17:0582,73171,178,24745,985,0001,999,0002,285,568
38Mar 26, 2024 17:1282,76071,774,10446,001,0002,000,0002,502,656
39Mar 26, 2024 17:1282,77172,438,37946,007,0002,000,0002,506,752
40Mar 26, 2024 16:5782,77480,052,01446,009,0002,000,0002,285,568
41Mar 26, 2024 17:3482,80070,447,81146,023,0002,001,0002,285,568
42Mar 26, 2024 16:5382,80267,658,30447,025,0001,000,0002,510,848
43Mar 26, 2024 16:5482,81671,320,52847,033,0001,000,0002,285,568
44Mar 26, 2024 17:1282,87670,616,90446,066,0002,002,0002,519,040
45Mar 26, 2024 17:0582,90071,733,45346,079,0002,003,0002,510,848
46Mar 26, 2024 17:3482,910121,528,58548,087,64202,211,840
47Mar 26, 2024 16:5782,92174,899,90846,091,0002,003,0002,285,568
48Mar 26, 2024 17:0582,93176,446,88547,098,0001,002,0002,506,752
49Mar 26, 2024 16:5382,93470,351,96546,098,0002,004,0002,285,568
50Mar 26, 2024 16:5482,93470,747,82846,098,0002,004,0002,285,568
51Mar 26, 2024 16:5382,93471,882,49446,098,0002,004,0002,285,568
52Mar 26, 2024 16:5382,94083,456,89548,105,00002,506,752
53Mar 26, 2024 16:5482,94371,188,34246,103,0002,004,0002,260,992
54Mar 26, 2024 16:5782,94774,451,76146,105,0002,004,0002,342,912
55Mar 26, 2024 17:0582,94872,941,02046,106,0002,004,0002,285,568
56Mar 26, 2024 17:3482,95569,838,08246,110,0002,004,0002,285,568
57Mar 26, 2024 17:1282,98873,194,01547,131,0001,002,0002,285,568
58Mar 26, 2024 16:5383,00074,647,87546,135,0002,005,0002,285,568
59Mar 26, 2024 17:1283,032125,120,35248,158,43602,211,840
60Mar 26, 2024 17:0583,069122,587,76148,180,15402,220,032
61Mar 26, 2024 17:1283,084113,257,67448,188,77402,220,032
62Mar 26, 2024 16:5383,094109,836,14148,194,74402,211,840
63Mar 26, 2024 16:5383,11471,396,42446,198,0002,008,0002,285,568
64Mar 26, 2024 17:1283,13370,646,97847,213,0001,004,0002,371,584
65Mar 26, 2024 17:0583,148102,835,92948,225,75602,228,224
66Mar 26, 2024 16:5783,165119,793,29748,235,53502,215,936
67Mar 26, 2024 16:5783,17270,185,60046,230,0002,010,0002,347,008
68Mar 26, 2024 16:5383,19871,572,47847,250,0001,005,0002,265,088
69Mar 26, 2024 16:5783,22276,798,00346,258,0002,011,0002,506,752
70Mar 26, 2024 16:5483,29870,499,98247,307,0001,006,0002,273,280
71Mar 26, 2024 17:1283,35769,635,38546,333,0002,014,0002,285,568
72Mar 26, 2024 16:5383,43374,387,01047,383,0001,008,0002,519,040
73Mar 26, 2024 17:0583,44377,902,10946,381,0002,016,0002,510,848
74Mar 26, 2024 16:5783,45573,893,07947,396,0001,008,0002,510,848
75Mar 26, 2024 16:5383,48872,034,46847,415,0001,008,0002,506,752
76Mar 26, 2024 16:5483,62670,776,53947,493,0001,010,0002,510,848
77Mar 26, 2024 16:5483,64570,946,29447,504,0001,010,0002,285,568
78Mar 26, 2024 16:5383,71472,627,85947,543,0001,011,0002,510,848
79Mar 26, 2024 16:5483,740104,592,95948,569,17802,211,840
80Mar 26, 2024 16:5783,78373,901,97347,582,0001,012,0002,367,488
81Mar 26, 2024 16:5783,802135,509,60248,605,30002,215,936
82Mar 26, 2024 17:0584,00978,452,38247,710,0001,015,0002,506,752
83Mar 26, 2024 17:1284,02674,463,60047,720,0001,015,0002,285,568
84Mar 26, 2024 17:1284,130104,828,71248,795,23102,211,840