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 20, 2025 21:55HattonuriHattonuriScore: 36,924Success
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 23:2233,41751,390,26319,382,00002,269,184
2Dec 19, 2025 23:2233,49335,227,56018,404,0001,022,0002,531,328
3Dec 19, 2025 23:2133,64837,805,95719,516,00002,269,184
4Dec 20, 2025 21:5533,73846,851,26318,539,0001,029,0002,535,424
5Dec 19, 2025 23:2233,94049,893,12019,685,00002,269,184
6Dec 19, 2025 23:2234,30342,524,38217,907,0001,989,0002,277,376
7Dec 19, 2025 23:2134,31745,981,55418,909,000995,0002,269,184
8Dec 20, 2025 21:5535,17846,563,29620,403,00002,375,680
9Dec 19, 2025 23:2235,35541,478,81020,506,00002,277,376
10Dec 19, 2025 23:2235,60247,635,54220,649,00002,269,184
11Dec 20, 2025 21:5536,03638,254,59319,906,000995,0002,531,328
12Dec 19, 2025 23:2236,33141,824,12420,069,0001,003,0002,531,328
13Dec 20, 2025 21:5536,92445,697,56721,416,00002,371,584
14Dec 19, 2025 23:2237,02143,780,03121,472,00002,371,584
15Dec 20, 2025 21:5537,09043,911,71821,512,00002,523,136
16Dec 19, 2025 23:2237,09540,715,38421,515,00002,523,136
17Dec 19, 2025 23:2237,24347,180,43420,573,0001,028,0002,260,992
18Dec 19, 2025 23:2237,39050,545,70121,686,00002,531,328
19Dec 20, 2025 21:5537,78349,601,39819,922,0001,992,0002,281,472
20Dec 20, 2025 21:5537,95541,945,42721,014,0001,000,0002,531,328
21Dec 19, 2025 23:2238,14545,792,50520,113,0002,011,0002,260,992
22Dec 20, 2025 21:5538,36043,544,93321,238,0001,011,0002,523,136
23Dec 19, 2025 23:2138,67248,781,99122,430,00002,523,136
24Dec 20, 2025 21:5540,23445,851,77523,336,00002,523,136