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 listDec 19, 2025 02:13Wookyung JeongWookyung JeongScore: 33,000Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 19, 2025 02:1231,62445,328,39417,323,0001,019,0002,510,848
2Dec 18, 2025 17:2131,95543,364,41217,505,0001,029,0002,269,184
3Dec 18, 2025 17:1832,02242,311,21817,542,0001,031,0002,510,848
4Dec 18, 2025 17:1832,09041,558,61717,578,0001,034,0002,293,760
5Dec 18, 2025 17:1832,13335,116,11517,602,0001,035,0002,490,368
6Dec 18, 2025 17:1932,34348,447,42518,759,00002,289,664
7Dec 19, 2025 02:1232,55551,458,10217,889,000993,0002,498,560
8Dec 18, 2025 17:1832,59839,327,75717,912,000995,0002,306,048
9Dec 18, 2025 17:1832,59843,213,52317,912,000995,0002,502,656
10Dec 19, 2025 02:1332,67846,415,17417,956,000997,0002,269,184
11Dec 18, 2025 17:2032,70541,727,77817,971,000998,0002,506,752
12Dec 19, 2025 02:1232,71641,522,54116,978,0001,997,0002,494,464
13Dec 18, 2025 17:1832,82641,532,97517,035,0002,004,0002,461,696
14Dec 18, 2025 17:2132,83442,347,98817,040,0002,004,0002,342,912
15Dec 18, 2025 17:1832,84739,585,81916,043,0003,008,0002,457,600
16Dec 18, 2025 17:1932,86042,191,95918,056,0001,003,0002,498,560
17Dec 18, 2025 17:2032,87844,304,70617,062,0002,007,0002,502,656
18Dec 19, 2025 02:1232,88145,886,31617,064,0002,007,0002,465,792
19Dec 18, 2025 17:2032,91741,236,43418,088,0001,004,0002,510,848
20Dec 18, 2025 17:2032,93345,639,77817,091,0002,010,0002,338,816
21Dec 19, 2025 02:1232,94340,590,15218,102,0001,005,0002,490,368
22Dec 19, 2025 02:1232,97144,088,09217,110,0002,013,0002,502,656
23Dec 19, 2025 02:1232,99142,958,28317,121,0002,014,0002,478,080
24Dec 19, 2025 02:1232,99338,745,44517,122,0002,014,0002,285,568
25Dec 18, 2025 17:2033,00042,645,55318,133,0001,007,0002,465,792
26Dec 18, 2025 17:1933,00243,752,38517,127,0002,014,0002,281,472
27Dec 18, 2025 17:1933,05743,008,72618,164,0001,009,0002,510,848
28Dec 18, 2025 17:1833,06239,797,01717,158,0002,018,0002,256,896
29Dec 18, 2025 17:1833,08141,695,88018,178,0001,009,0002,260,992
30Dec 18, 2025 17:1833,10942,657,12818,193,0001,010,0002,510,848
31Dec 18, 2025 17:2033,13642,782,24417,196,0002,023,0002,387,968
32Dec 18, 2025 17:1933,13641,890,92217,196,0002,023,0002,273,280
33Dec 19, 2025 02:1233,13636,402,73817,196,0002,023,0002,506,752
34Dec 19, 2025 02:1233,14034,561,48716,186,0003,035,0002,461,696
35Dec 18, 2025 17:1833,15543,582,31218,218,0001,012,0002,506,752
36Dec 18, 2025 17:1933,20741,584,50118,247,0001,013,0002,408,448
37Dec 18, 2025 17:2133,25240,526,95018,271,0001,015,0002,461,696
38Dec 18, 2025 17:2133,26045,414,39319,291,00002,457,600
39Dec 18, 2025 17:1933,27242,411,90717,267,0002,031,0002,494,464
40Dec 18, 2025 17:1933,28441,494,38918,289,0001,016,0002,510,848
41Dec 18, 2025 17:2133,29546,757,94518,295,0001,016,0002,273,280
42Dec 18, 2025 17:1833,34343,037,06218,322,0001,017,0002,482,176
43Dec 19, 2025 02:1233,37642,491,07619,358,00002,486,272
44Dec 19, 2025 02:1333,41642,617,39218,361,0001,020,0002,461,696
45Dec 18, 2025 17:1933,49150,775,70619,425,00002,416,640
46Dec 19, 2025 02:1333,50555,246,48418,411,0001,022,0002,428,928
47Dec 19, 2025 02:1233,76439,634,23518,553,0001,030,0002,453,504
48Dec 18, 2025 17:2133,88642,913,07618,620,0001,034,0002,387,968