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 22, 2025 00:10HattonuriHattonuriScore: 31,266Success
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 23:5730,05740,038,62316,408,0001,025,0002,510,848
2Dec 21, 2025 23:5630,14842,827,51016,458,0001,028,0002,453,504
3Dec 22, 2025 00:2330,15342,777,15417,489,00002,531,328
4Dec 22, 2025 00:1230,35032,963,67717,603,00002,306,048
5Dec 22, 2025 00:1230,50943,910,89917,695,00002,461,696
6Dec 22, 2025 00:2330,60348,990,67216,706,0001,044,0002,498,560
7Dec 22, 2025 00:1030,60343,986,11416,706,0001,044,0002,510,848
8Dec 22, 2025 00:1030,80732,477,02116,876,000992,0002,482,176
9Dec 22, 2025 00:1030,85941,211,37715,910,0001,988,0002,510,848
10Dec 22, 2025 00:2230,86438,575,52416,907,000994,0002,510,848
11Dec 22, 2025 00:1030,87247,692,60916,912,000994,0002,318,336
12Dec 22, 2025 00:2530,88144,011,27616,916,000995,0002,301,952
13Dec 22, 2025 00:1030,88643,714,94717,914,00002,510,848
14Dec 22, 2025 00:0130,96235,259,66116,961,000997,0002,502,656
15Dec 21, 2025 23:5731,01042,259,25916,987,000999,0002,289,664
16Dec 22, 2025 00:2331,02236,873,99916,994,000999,0002,502,656
17Dec 21, 2025 23:5631,09742,326,57917,034,0001,002,0002,449,408
18Dec 22, 2025 00:2331,16938,533,69117,074,0001,004,0002,519,040
19Dec 22, 2025 00:2231,26642,464,43117,127,0001,007,0002,314,240
20Dec 22, 2025 00:2231,33445,287,19916,155,0002,019,0002,486,272
21Dec 22, 2025 00:1031,58839,396,50617,304,0001,017,0002,293,760
22Dec 22, 2025 00:0131,74835,810,64017,391,0001,023,0002,301,952
23Dec 22, 2025 00:1031,76246,712,90118,422,00002,293,760
24Dec 22, 2025 00:2231,77635,688,35718,430,00002,465,792
25Dec 22, 2025 00:2231,82437,807,64517,433,0001,025,0002,351,104
26Dec 22, 2025 00:2531,89039,172,06617,469,0001,027,0002,322,432
27Dec 22, 2025 00:0132,20947,567,81418,681,00002,392,064
28Dec 22, 2025 00:1032,23343,104,34318,695,00002,502,656
29Dec 22, 2025 00:2532,24834,711,91518,704,00002,396,160
30Dec 22, 2025 00:2232,35344,810,91817,723,0001,042,0002,437,120
31Dec 22, 2025 00:1232,36244,419,14218,770,00002,424,832
32Dec 21, 2025 23:5632,59143,547,04416,914,0001,989,0002,412,544
33Dec 22, 2025 00:1032,60549,599,53916,921,0001,990,0002,375,680
34Dec 22, 2025 00:2332,66239,037,91616,950,0001,994,0002,433,024
35Dec 22, 2025 00:2332,82439,094,33718,036,0001,002,0002,408,448
36Dec 21, 2025 23:5733,84743,280,60018,598,0001,033,0002,531,328