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 listOct 18, 2024 13:04matsuoka-601matsuoka-601Score: 38,819Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 18, 2024 10:0627,55536,615,98013,985,0001,997,0002,510,848
2Oct 18, 2024 13:1329,43651,646,14015,065,0002,008,0002,428,928
3Oct 18, 2024 10:0730,40743,220,33516,599,0001,037,0002,273,280
4Oct 18, 2024 10:0630,88839,552,96215,925,0001,990,0002,535,424
5Oct 18, 2024 10:0631,17140,927,79517,075,0001,004,0002,424,832
6Oct 18, 2024 10:0731,63645,095,12317,330,0001,019,0002,420,736
7Oct 18, 2024 08:2831,81037,419,25117,425,0001,025,0002,273,280
8Oct 18, 2024 08:3333,84342,396,00418,596,0001,033,0002,420,736
9Oct 18, 2024 10:0635,14341,066,38819,364,0001,019,0002,424,832
10Oct 18, 2024 08:2935,23355,355,17119,414,0001,021,0002,428,928
11Oct 18, 2024 08:3335,39042,510,69819,500,0001,026,0002,424,832
12Oct 18, 2024 08:2835,45041,172,51019,533,0001,028,0002,510,848
13Oct 18, 2024 08:2835,61049,785,11619,622,0001,032,0002,428,928
14Oct 18, 2024 08:2936,08648,532,25118,937,0001,993,0002,273,280
15Oct 18, 2024 10:0536,52145,168,14520,174,0001,008,0002,428,928
16Oct 18, 2024 10:0636,96051,774,13220,417,0001,020,0002,428,928
17Oct 18, 2024 10:0536,96050,803,11120,417,0001,020,0002,273,280
18Oct 18, 2024 13:0537,06643,706,37720,475,0001,023,0002,428,928
19Oct 18, 2024 13:0437,11639,119,72221,527,00002,510,848
20Oct 18, 2024 10:0637,19746,956,84221,574,00002,535,424
21Oct 18, 2024 08:3337,43146,855,35621,710,00002,424,832
22Oct 18, 2024 13:0437,47450,035,24320,700,0001,035,0002,428,928
23Oct 18, 2024 13:0437,78153,938,02420,917,000996,0002,420,736
24Oct 18, 2024 10:0737,94745,989,46620,009,0002,000,0002,424,832
25Oct 18, 2024 08:2937,96950,855,46021,021,0001,001,0002,424,832
26Oct 18, 2024 13:0438,07844,763,33720,078,0002,007,0002,433,024
27Oct 18, 2024 10:0538,08836,344,62620,083,0002,008,0002,433,024
28Oct 18, 2024 08:2938,41244,255,50821,267,0001,012,0002,420,736
29Oct 18, 2024 13:0538,64846,306,88822,416,00002,428,928
30Oct 18, 2024 13:1338,79746,984,87222,502,00002,437,120
31Oct 18, 2024 13:0538,81942,435,73922,515,00002,510,848
32Oct 18, 2024 08:2939,49841,866,55420,917,0001,992,0002,428,928
33Oct 18, 2024 08:29893,597537,193,880516,285,0002,001,0002,379,776
34Oct 18, 2024 10:06895,962544,456,089516,660,0002,998,0002,379,776
35Oct 18, 2024 13:04897,614546,894,756518,618,0001,998,0002,375,680
36Oct 18, 2024 10:07898,498541,798,151519,129,0002,000,0002,375,680
37Oct 18, 2024 10:05898,797545,656,905518,301,0003,001,0002,326,528
38Oct 18, 2024 08:28899,416550,915,761518,663,0002,998,0002,375,680
39Oct 18, 2024 10:07899,890541,108,856516,937,0004,999,0002,486,272
40Oct 18, 2024 08:33900,214542,478,279522,124,00002,486,272
41Oct 18, 2024 10:06900,531551,552,479519,307,0003,001,0002,375,680
42Oct 18, 2024 10:05901,141545,261,508520,664,0001,998,0002,486,272
43Oct 18, 2024 08:33906,681548,071,327523,876,0001,999,0002,531,328
44Oct 18, 2024 08:33907,910552,340,104524,590,0001,998,0002,486,272
45Oct 18, 2024 10:06908,133551,331,994521,720,0004,997,0002,387,968
46Oct 18, 2024 10:06908,795548,982,322525,101,0002,000,0002,486,272
47Oct 18, 2024 08:29908,845547,968,484525,130,0002,000,0002,486,272
48Oct 18, 2024 10:07909,086553,821,190524,269,0003,001,0002,486,272
49Oct 18, 2024 10:05909,126551,128,672524,292,0003,001,0002,486,272
50Oct 18, 2024 08:28909,955548,381,434524,776,0002,998,0002,379,776
51Oct 18, 2024 08:29910,209548,917,929525,922,0001,999,0002,383,872
52Oct 18, 2024 13:04912,738566,029,660527,387,0002,001,0002,379,776
53Oct 18, 2024 10:06913,250555,686,807527,687,0001,998,0002,486,272
54Oct 18, 2024 13:04914,021550,023,074529,132,0001,000,0002,383,872
55Oct 18, 2024 08:28915,126555,248,266528,774,0001,999,0002,486,272
56Oct 18, 2024 10:06917,071555,948,485530,902,000999,0002,486,272
57Oct 18, 2024 13:041,980,0761,180,730,0961,146,445,0001,999,0002,367,488
58Oct 18, 2024 13:041,981,1881,178,225,0621,147,089,0002,000,0002,482,176
59Oct 18, 2024 08:291,981,9761,176,579,7761,147,547,0001,999,0002,469,888
60Oct 18, 2024 13:131,991,8861,178,878,6981,153,296,0001,998,0002,535,424