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 21, 2025 02:04HattonuriHattonuriScore: 34,317Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 02:0331,93149,920,81517,492,0001,028,0002,408,448
2Dec 21, 2025 02:0431,94142,323,39917,497,0001,029,0002,523,136
3Dec 21, 2025 02:0331,96040,381,01317,508,0001,029,0002,531,328
4Dec 21, 2025 02:1631,98138,049,72718,549,00002,416,640
5Dec 21, 2025 02:0232,01439,533,46618,568,00002,408,448
6Dec 21, 2025 02:1432,10539,754,33317,587,0001,034,0002,420,736
7Dec 21, 2025 02:0332,16447,219,20918,655,00002,412,544
8Dec 21, 2025 02:0332,16739,016,31018,657,00002,412,544
9Dec 21, 2025 02:0332,27145,012,19917,678,0001,039,0002,408,448
10Dec 21, 2025 02:0432,61941,779,14616,928,0001,991,0002,523,136
11Dec 21, 2025 02:1532,62242,942,90815,934,0002,987,0002,404,352
12Dec 21, 2025 02:1632,64045,526,56617,880,0001,051,0002,412,544
13Dec 21, 2025 02:0332,78440,173,04317,014,0002,001,0002,412,544
14Dec 21, 2025 02:1532,85039,934,82717,048,0002,005,0002,408,448
15Dec 21, 2025 02:1532,85040,565,10917,048,0002,005,0002,408,448
16Dec 21, 2025 02:1432,86443,323,19117,055,0002,006,0002,424,832
17Dec 21, 2025 02:0232,86944,284,85617,058,0002,006,0002,375,680
18Dec 21, 2025 02:1633,09740,089,09718,186,0001,010,0002,420,736
19Dec 21, 2025 02:1633,12445,179,76317,190,0002,022,0002,408,448
20Dec 21, 2025 02:1433,23841,783,12018,264,0001,014,0002,531,328
21Dec 21, 2025 02:0233,31637,979,84819,323,00002,416,640
22Dec 21, 2025 02:1433,32443,730,61518,311,0001,017,0002,408,448
23Dec 21, 2025 02:1633,42942,248,93219,389,00002,404,352
24Dec 21, 2025 02:0233,65543,111,10218,493,0001,027,0002,404,352
25Dec 21, 2025 02:1533,71643,658,93318,526,0001,029,0002,269,184
26Dec 21, 2025 02:0333,72941,786,00418,534,0001,029,0002,531,328
27Dec 21, 2025 02:0433,75244,647,43819,576,00002,416,640
28Dec 21, 2025 02:1433,78839,252,97519,597,00002,523,136
29Dec 21, 2025 02:1433,81938,845,32518,583,0001,032,0002,531,328
30Dec 21, 2025 02:1533,93145,288,36119,680,00002,420,736
31Dec 21, 2025 02:1633,95240,875,67418,656,0001,036,0002,404,352
32Dec 21, 2025 02:1434,04843,041,30218,709,0001,039,0002,416,640
33Dec 21, 2025 02:1634,09148,764,98018,733,0001,040,0002,269,184
34Dec 21, 2025 02:0434,31746,737,38218,909,000995,0002,531,328
35Dec 21, 2025 02:1634,34543,009,66918,924,000996,0002,531,328
36Dec 21, 2025 02:0534,35249,436,39917,932,0001,992,0002,420,736
37Dec 21, 2025 02:1534,38641,020,25317,950,0001,994,0002,437,120
38Dec 21, 2025 02:0334,40752,547,31717,961,0001,995,0002,416,640
39Dec 21, 2025 02:0334,41041,448,81917,963,0001,995,0002,404,352
40Dec 21, 2025 02:0234,47442,032,97517,996,0001,999,0002,404,352
41Dec 21, 2025 02:1534,56452,156,20919,045,0001,002,0002,523,136
42Dec 21, 2025 02:0234,60537,344,95518,064,0002,007,0002,523,136
43Dec 21, 2025 02:0234,62939,144,82818,077,0002,008,0002,404,352
44Dec 21, 2025 02:1434,63445,868,60618,080,0002,008,0002,523,136
45Dec 21, 2025 02:0334,66049,250,37218,093,0002,010,0002,383,872
46Dec 21, 2025 02:0434,71249,354,94218,120,0002,013,0002,269,184
47Dec 21, 2025 02:0534,74846,729,33118,139,0002,015,0002,523,136
48Dec 21, 2025 02:0234,76243,246,36418,146,0002,016,0002,523,136
49Dec 21, 2025 02:0434,77840,696,32919,163,0001,008,0002,408,448
50Dec 21, 2025 02:1534,78436,434,68519,167,0001,008,0002,408,448
51Dec 21, 2025 02:0534,79847,427,53918,165,0002,018,0002,523,136
52Dec 21, 2025 02:1534,81237,162,23718,172,0002,019,0002,404,352
53Dec 21, 2025 02:0234,84539,374,01918,189,0002,021,0002,387,968
54Dec 21, 2025 02:0434,88144,461,60318,208,0002,023,0002,412,544
55Dec 21, 2025 02:0334,88836,713,55818,212,0002,023,0002,408,448
56Dec 21, 2025 02:0434,90041,552,92419,230,0001,012,0002,523,136
57Dec 21, 2025 02:0334,91940,062,73418,228,0002,025,0002,404,352
58Dec 21, 2025 02:1434,91941,257,17518,228,0002,025,0002,412,544
59Dec 21, 2025 02:1535,12641,016,80720,373,00002,416,640
60Dec 21, 2025 02:1535,17141,435,76920,399,00002,527,232
61Dec 21, 2025 02:0435,17946,619,71420,404,00002,400,256
62Dec 21, 2025 02:0335,28148,823,15819,440,0001,023,0002,408,448
63Dec 21, 2025 02:1635,29851,070,14020,473,00002,527,232
64Dec 21, 2025 02:0335,76745,160,16120,745,00002,371,584
65Dec 21, 2025 02:0336,02945,859,34818,907,0001,990,0002,523,136
66Dec 21, 2025 02:1536,05246,707,67919,915,000995,0002,416,640