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:55HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:35Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:35055,443,64247,609,000016,384No answer to stdout. Died or killed?
2Dec 19, 2025 23:2233,41751,390,26319,382,00002,269,184
3Dec 19, 2025 23:2233,49335,227,56018,404,0001,022,0002,531,328
4Dec 19, 2025 23:2133,64837,805,95719,516,00002,269,184
5Dec 20, 2025 21:5533,73846,851,26318,539,0001,029,0002,535,424
6Dec 19, 2025 23:2233,94049,893,12019,685,00002,269,184
7Dec 19, 2025 23:2234,30342,524,38217,907,0001,989,0002,277,376
8Dec 19, 2025 23:2134,31745,981,55418,909,000995,0002,269,184
9Dec 20, 2025 21:5535,17846,563,29620,403,00002,375,680
10Dec 19, 2025 23:2235,35541,478,81020,506,00002,277,376
11Dec 19, 2025 23:2235,60247,635,54220,649,00002,269,184
12Dec 20, 2025 21:5536,03638,254,59319,906,000995,0002,531,328
13Dec 19, 2025 23:2236,33141,824,12420,069,0001,003,0002,531,328
14Dec 20, 2025 21:5536,92445,697,56721,416,00002,371,584
15Dec 19, 2025 23:2237,02143,780,03121,472,00002,371,584
16Dec 20, 2025 21:5537,09043,911,71821,512,00002,523,136
17Dec 19, 2025 23:2237,09540,715,38421,515,00002,523,136
18Dec 19, 2025 23:2237,24347,180,43420,573,0001,028,0002,260,992
19Dec 19, 2025 23:2237,39050,545,70121,686,00002,531,328
20Dec 20, 2025 21:5537,78349,601,39819,922,0001,992,0002,281,472
21Dec 20, 2025 21:5537,95541,945,42721,014,0001,000,0002,531,328
22Dec 19, 2025 23:2238,14545,792,50520,113,0002,011,0002,260,992
23Dec 20, 2025 21:5538,36043,544,93321,238,0001,011,0002,523,136
24Dec 19, 2025 23:2138,67248,781,99122,430,00002,523,136
25Dec 20, 2025 21:5540,23445,851,77523,336,00002,523,136