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 listFeb 8, 2025 23:09undenuicapundenuicapError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 8, 2025 23:1700000Error: expected "2064299 ", got "2141961 "stderr
2Feb 8, 2025 23:0900000Error: expected "1227411 ", got "1266485 "stderr
3Feb 8, 2025 23:1000000Error: expected "1424112 ", got "1467237 "stderr
4Feb 8, 2025 23:1000000Error: expected "2585788 ", got "2640362 "stderr
5Feb 8, 2025 23:0900000Error: expected "616328 ", got "998921 "
6Feb 8, 2025 23:1000000Error: expected "1149839 ", got "1149934 "
7Feb 8, 2025 23:1000000Error: expected "2490030 ", got "2667060 "stderr
8Feb 8, 2025 23:0900000Error: expected "1157824 ", got "1376162 "stderr
9Feb 8, 2025 23:1000000Error: expected "1924934 ", got "2306681 "stderr
10Feb 8, 2025 23:1600000Error: expected "2989620 ", got "2994411 "stderr
11Feb 8, 2025 23:1600000Error: expected "1485126 ", got "1487834 "stderr
12Feb 8, 2025 23:1600000Error: expected "839265 ", got "858235 "stderr
13Feb 8, 2025 23:1600000Error: expected "2418874 ", got "2433256 "
14Feb 8, 2025 23:1600000Error: expected "775524 ", got "808116 "stderr
15Feb 8, 2025 23:1700000Error: expected "1480034 ", got "1526831 "
16Feb 8, 2025 23:0900000Error: expected "1123041 ", got "1166132 "stderr
17Feb 8, 2025 23:0900000Error: expected "1486707 ", got "1530504 "
18Feb 8, 2025 23:1700000Error: expected "2482944 ", got "2589179 "stderr
19Feb 8, 2025 23:1800000Error: expected "1692124 ", got "2161928 "stderr
20Feb 8, 2025 23:1800000Error: expected "1751051 ", got "1833233 "
21Feb 8, 2025 23:1800000Error: expected "2751475 ", got "2761603 "
22Feb 8, 2025 23:1800000Error: expected "3364915 ", got "3472027 "
23Feb 8, 2025 23:1800000Error: expected "1387264 ", got "1438698 "
24Feb 8, 2025 23:1800000Error: expected "1332921 ", got "1409393 "stderr
25Feb 8, 2025 23:0900000Error: expected "3067318 ", got "3067672 "
26Feb 8, 2025 23:1871,77473,100,65041,629,00002,420,736
27Feb 8, 2025 23:1071,83868,460,57241,666,00002,420,736
28Feb 8, 2025 23:1071,86069,079,94940,663,0001,016,0002,535,424
29Feb 8, 2025 23:1771,92965,852,88241,719,00002,527,232
30Feb 8, 2025 23:1671,93667,773,99141,723,00002,531,328
31Feb 8, 2025 23:1071,96771,967,20941,741,00002,269,184
32Feb 8, 2025 23:1071,96972,683,59141,742,00002,269,184
33Feb 8, 2025 23:1672,01069,977,85040,748,0001,018,0002,269,184
34Feb 8, 2025 23:1072,17670,051,57139,869,0001,993,0002,424,832
35Feb 8, 2025 23:1772,18465,824,07740,871,000996,0002,535,424
36Feb 8, 2025 23:1072,21267,770,64740,886,000997,0002,531,328
37Feb 8, 2025 23:1672,21271,533,53440,886,000997,0002,420,736
38Feb 8, 2025 23:1772,21267,526,15240,886,000997,0002,531,328
39Feb 8, 2025 23:1672,21769,257,80039,892,0001,994,0002,527,232
40Feb 8, 2025 23:1672,22266,657,97940,892,000997,0002,531,328
41Feb 8, 2025 23:1772,22273,126,73139,895,0001,994,0002,527,232
42Feb 8, 2025 23:1072,22668,037,54641,891,00002,535,424
43Feb 8, 2025 23:1072,22670,858,19840,894,000997,0002,416,640
44Feb 8, 2025 23:1672,23468,557,00839,901,0001,995,0002,420,736
45Feb 8, 2025 23:1972,23666,950,18640,900,000997,0002,269,184
46Feb 8, 2025 23:1072,23671,624,08439,902,0001,995,0002,269,184
47Feb 8, 2025 23:1772,23868,392,68940,901,000997,0002,420,736
48Feb 8, 2025 23:1872,24070,201,79639,904,0001,995,0002,531,328
49Feb 8, 2025 23:1772,26073,456,63139,916,0001,995,0002,531,328
50Feb 8, 2025 23:1672,26265,204,33239,917,0001,995,0002,420,736
51Feb 8, 2025 23:1072,26665,069,16039,919,0001,995,0002,535,424
52Feb 8, 2025 23:1972,27168,772,15740,919,000998,0002,269,184
53Feb 8, 2025 23:1972,28164,505,18239,927,0001,996,0002,269,184
54Feb 8, 2025 23:1072,28666,657,14440,928,000998,0002,416,640
55Feb 8, 2025 23:1772,29362,348,16139,934,0001,996,0002,412,544
56Feb 8, 2025 23:1772,29867,734,26539,937,0001,996,0002,273,280
57Feb 8, 2025 23:1772,30567,479,51639,940,0001,997,0002,535,424
58Feb 8, 2025 23:1772,31467,945,68039,945,0001,997,0002,424,832
59Feb 8, 2025 23:1072,35368,332,31940,966,000999,0002,527,232
60Feb 8, 2025 23:1672,35767,081,50039,969,0001,998,0002,420,736
61Feb 8, 2025 23:1072,38171,273,97340,982,000999,0002,416,640
62Feb 8, 2025 23:1072,38867,289,10940,986,000999,0002,269,184
63Feb 8, 2025 23:1672,47971,334,12040,037,0002,001,0002,416,640
64Feb 8, 2025 23:1072,50765,859,92940,052,0002,002,0002,535,424
65Feb 8, 2025 23:1872,52263,003,67140,060,0002,003,0002,416,640
66Feb 8, 2025 23:1072,63867,948,43841,127,0001,003,0002,531,328
67Feb 8, 2025 23:1772,64071,738,30440,125,0002,006,0002,420,736
68Feb 8, 2025 23:1072,64165,863,70441,129,0001,003,0002,531,328
69Feb 8, 2025 23:1072,70563,980,53941,165,0001,004,0002,527,232
70Feb 8, 2025 23:1772,87166,758,88040,253,0002,012,0002,428,928