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 JeongError
Source Code

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

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