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 07:33matsuoka-601matsuoka-601Score: 1,966,800Success
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 07:0119,08332,990,67710,062,0001,006,0002,424,832
2Oct 17, 2024 12:5819,41228,478,9529,212,0002,047,0002,420,736
3Oct 18, 2024 07:3320,24532,710,65611,742,00002,535,424
4Oct 17, 2024 12:5820,83131,651,80110,069,0002,013,0002,424,832
5Oct 18, 2024 07:3421,68431,331,47311,529,0001,048,0002,535,424
6Oct 17, 2024 12:5821,76230,127,15611,571,0001,051,0002,535,424
7Oct 18, 2024 06:5822,42935,751,82612,009,0001,000,0002,535,424
8Oct 17, 2024 12:5822,61729,767,47611,100,0002,018,0002,424,832
9Oct 18, 2024 07:3423,31434,592,48312,482,0001,040,0002,535,424
10Oct 18, 2024 06:5823,60531,698,92012,638,0001,053,0002,273,280
11Oct 18, 2024 07:3224,00933,011,43412,931,000994,0002,433,024
12Oct 18, 2024 06:5824,32432,009,66613,101,0001,007,0002,433,024
13Oct 18, 2024 06:5824,97137,558,18714,483,00002,273,280
14Oct 17, 2024 12:4625,04834,721,72013,491,0001,037,0002,433,024
15Oct 17, 2024 12:4625,09534,754,94913,516,0001,039,0002,273,280
16Oct 18, 2024 07:3325,29029,406,71213,621,0001,047,0002,424,832
17Oct 18, 2024 07:0125,36232,412,13313,660,0001,050,0002,428,928
18Oct 18, 2024 07:3425,69531,573,28113,910,000993,0002,273,280
19Oct 17, 2024 12:5826,45331,594,10114,321,0001,022,0002,273,280
20Oct 18, 2024 07:341,955,3521,159,404,6011,133,105,000999,0002,482,176
21Oct 18, 2024 06:581,957,5381,163,301,2841,131,375,0003,997,0002,375,680
22Oct 17, 2024 12:461,959,4101,158,457,1491,136,458,00002,285,568
23Oct 18, 2024 07:331,960,5861,162,797,6821,133,144,0003,996,0002,510,848
24Oct 18, 2024 07:331,960,7141,163,063,5451,135,216,0001,998,0002,482,176
25Oct 18, 2024 07:331,961,2881,165,332,8611,135,548,0001,999,0002,367,488
26Oct 18, 2024 07:011,962,4781,163,928,6081,136,239,0001,998,0002,482,176
27Oct 18, 2024 07:341,962,7171,167,166,9381,135,378,0002,998,0002,363,392
28Oct 18, 2024 07:011,964,3911,169,088,8381,136,349,0002,998,0002,482,176
29Oct 18, 2024 07:321,964,4191,164,436,7131,136,365,0002,998,0002,363,392
30Oct 18, 2024 07:011,964,6141,169,585,8381,136,478,0002,998,0002,478,080
31Oct 18, 2024 07:331,964,7531,165,566,8651,137,558,0001,999,0002,371,584
32Oct 17, 2024 12:581,965,6171,168,054,3101,137,061,0002,997,0002,482,176
33Oct 17, 2024 12:461,965,6171,166,358,3831,138,058,0002,000,0002,482,176
34Oct 18, 2024 07:331,966,8001,163,001,9021,138,745,0001,999,0002,482,176
35Oct 18, 2024 07:331,967,2001,169,672,1591,137,977,0002,999,0002,273,280
36Oct 18, 2024 07:341,967,3091,169,209,4591,139,041,0001,998,0002,375,680
37Oct 17, 2024 12:461,967,4861,167,686,5371,139,142,0002,000,0002,482,176
38Oct 17, 2024 12:581,967,8621,168,032,9101,139,362,0001,998,0002,482,176
39Oct 18, 2024 07:331,968,1551,162,079,8621,137,532,0003,998,0002,371,584
40Oct 17, 2024 12:461,968,1911,165,584,8481,139,552,0001,999,0002,285,568
41Oct 18, 2024 07:331,968,2261,168,526,5731,139,572,0001,999,0002,482,176
42Oct 18, 2024 07:341,968,3031,169,352,7211,137,618,0003,998,0002,482,176
43Oct 18, 2024 06:581,968,5881,165,700,2541,139,782,0001,999,0002,375,680
44Oct 18, 2024 07:341,969,1431,172,713,3531,139,103,0003,000,0002,371,584
45Oct 18, 2024 07:011,969,2621,173,874,7371,141,173,000999,0002,482,176
46Oct 18, 2024 06:581,969,2971,174,612,5241,140,194,0001,998,0002,371,584
47Oct 18, 2024 07:331,969,6811,173,753,5961,140,417,0001,998,0002,375,680
48Oct 18, 2024 06:581,970,0671,173,516,1321,140,640,0001,999,0002,482,176
49Oct 18, 2024 07:011,970,3951,164,116,2051,141,830,000999,0002,482,176
50Oct 18, 2024 07:331,970,5431,172,807,5661,138,916,0003,999,0002,367,488
51Oct 18, 2024 07:321,971,2691,172,532,8261,139,339,0003,997,0002,371,584
52Oct 18, 2024 07:011,971,6221,162,970,9351,142,542,000999,0002,482,176
53Oct 18, 2024 07:341,971,6601,172,535,8821,139,565,0003,998,0002,371,584
54Oct 17, 2024 12:581,971,7881,168,292,4841,140,638,0002,999,0002,482,176
55Oct 17, 2024 12:581,971,8101,167,913,4921,140,651,0002,999,0002,273,280
56Oct 18, 2024 07:341,972,1071,172,020,1991,141,825,0001,997,0002,273,280
57Oct 18, 2024 07:341,972,3191,171,597,4221,140,948,0002,997,0002,482,176
58Oct 18, 2024 07:331,972,4001,169,696,1041,140,995,0002,997,0002,482,176
59Oct 18, 2024 07:341,973,6241,169,689,3601,140,704,0003,998,0002,482,176
60Oct 18, 2024 07:331,974,0861,168,107,3211,141,971,0002,999,0002,482,176
61Oct 17, 2024 12:461,974,3291,177,814,9101,141,115,0003,996,0002,482,176
62Oct 17, 2024 12:461,974,9951,171,853,4841,145,497,00002,273,280
63Oct 18, 2024 07:011,976,2221,176,512,5921,144,211,0001,998,0002,482,176
64Oct 17, 2024 12:461,976,6551,168,709,3691,143,462,0002,998,0002,482,176
65Oct 18, 2024 06:581,978,0291,176,415,6471,144,259,0002,998,0002,482,176
66Oct 18, 2024 07:331,978,1571,176,165,1121,145,333,0001,998,0002,371,584