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 28, 2025 18:27imotw2imotw2Score: 67,572Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 18:1464,17659,803,15033,198,0004,024,0002,322,432
2Dec 28, 2025 18:1864,28165,128,32733,253,0004,030,0002,424,832
3Dec 28, 2025 18:2265,32664,113,84832,904,0004,985,0002,453,504
4Dec 28, 2025 18:1465,41662,478,70032,949,0004,992,0002,469,888
5Dec 28, 2025 18:2265,78464,215,40733,135,0005,020,0002,519,040
6Dec 28, 2025 18:1866,18365,696,11832,325,0006,061,0002,420,736
7Dec 28, 2025 18:2266,39162,164,43433,441,0005,066,0002,293,760
8Dec 28, 2025 18:1466,76768,759,32035,668,0003,057,0002,379,776
9Dec 28, 2025 18:1866,80266,289,67332,628,0006,117,0002,392,064
10Dec 28, 2025 18:1866,98664,147,01733,871,0004,981,0002,293,760
11Dec 28, 2025 18:1867,05266,615,03534,902,0003,988,0002,506,752
12Dec 28, 2025 18:1467,07867,225,06932,920,0005,985,0002,301,952
13Dec 28, 2025 18:1867,20969,533,37034,983,0003,998,0002,519,040
14Dec 28, 2025 18:1467,28364,524,87535,022,0004,002,0002,334,720
15Dec 28, 2025 18:2767,42165,437,08534,091,0005,013,0002,506,752
16Dec 28, 2025 18:2267,57260,457,98735,173,0004,019,0002,437,120
17Dec 28, 2025 18:1467,70960,542,23334,237,0005,034,0002,433,024
18Dec 28, 2025 18:1467,71465,206,15434,239,0005,035,0002,490,368
19Dec 28, 2025 18:2267,90264,037,25134,334,0005,049,0002,416,640
20Dec 28, 2025 18:1468,11965,906,09135,457,0004,052,0002,445,312
21Dec 28, 2025 18:2268,14061,318,99033,441,0006,080,0002,379,776
22Dec 28, 2025 18:1868,15961,364,94334,464,0005,068,0002,387,968
23Dec 28, 2025 18:2268,44866,505,15634,611,0005,089,0002,347,008
24Dec 28, 2025 18:2768,45964,729,22034,616,0005,090,0002,494,464
25Dec 28, 2025 18:2768,49165,765,07734,632,0005,093,0002,306,048
26Dec 28, 2025 18:1868,66264,402,36634,846,0004,978,0002,330,624
27Dec 28, 2025 18:2268,88667,338,21533,961,0005,993,0002,351,104
28Dec 28, 2025 18:1469,48863,877,80434,258,0006,045,0002,433,024
29Dec 28, 2025 18:1869,56262,839,69935,303,0005,043,0002,334,720
30Dec 28, 2025 18:2269,92268,870,23535,486,0005,069,0002,424,832