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

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:11Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:1208,522,374871,000020,480No answer to stdout. Died or killed?
2Dec 21, 2025 01:3632,60545,429,05916,921,0001,990,0002,433,024
3Dec 21, 2025 01:3532,65739,389,31717,945,000996,0002,424,832
4Dec 21, 2025 01:3532,68832,368,55016,964,0001,995,0002,527,232
5Dec 21, 2025 01:3632,84745,678,11518,049,0001,002,0002,433,024
6Dec 21, 2025 01:3532,89139,769,10118,073,0001,004,0002,527,232
7Dec 21, 2025 01:3533,03639,297,45417,144,0002,017,0002,265,088
8Dec 21, 2025 01:3633,22241,526,54219,269,00002,527,232
9Dec 21, 2025 01:3834,05041,817,78518,710,0001,039,0002,424,832
10Dec 21, 2025 01:3534,33647,714,30617,924,0001,991,0002,523,136
11Dec 21, 2025 01:3634,44541,214,11117,981,0001,997,0002,424,832
12Dec 21, 2025 01:3534,49341,784,44918,006,0002,000,0002,519,040
13Dec 21, 2025 01:3634,55745,453,90118,039,0002,004,0002,424,832
14Dec 21, 2025 01:3534,56739,923,26619,047,0001,002,0002,457,600
15Dec 21, 2025 01:3634,57845,338,80518,050,0002,005,0002,527,232
16Dec 21, 2025 01:3534,90942,974,18918,223,0002,024,0002,523,136
17Dec 21, 2025 01:3535,25040,102,40720,445,00002,523,136
18Dec 21, 2025 01:3535,41637,077,27319,514,0001,027,0002,416,640
19Dec 21, 2025 01:3536,03344,280,95119,904,000995,0002,519,040
20Dec 21, 2025 01:3536,03643,815,18018,911,0001,990,0002,519,040
21Dec 21, 2025 01:3636,07147,106,10718,929,0001,992,0002,424,832
22Dec 21, 2025 01:3836,19840,301,91018,996,0001,999,0002,363,392
23Dec 21, 2025 01:3636,29042,252,15420,046,0001,002,0002,523,136
24Dec 21, 2025 01:3536,47946,247,03520,151,0001,007,0002,408,448
25Dec 21, 2025 01:3536,49346,076,25319,151,0002,015,0002,527,232
26Dec 21, 2025 01:3536,89748,032,72020,381,0001,019,0002,416,640
27Dec 21, 2025 01:3836,98145,296,91120,428,0001,021,0002,420,736
28Dec 21, 2025 01:3637,14844,168,16021,546,00002,519,040