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 18, 2025 11:08Wookyung JeongWookyung JeongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 16:33Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 16:49030,028,500,37230,015,836,0001,008,00016,384Killed by timeout
2Dec 18, 2025 11:1238,99743,642,48322,618,00002,416,640
3Dec 18, 2025 11:0839,09152,768,83921,643,0001,030,0002,269,184
4Dec 18, 2025 11:1239,16253,170,50522,714,00002,437,120
5Dec 18, 2025 11:0839,20349,718,28621,705,0001,033,0002,265,088
6Dec 18, 2025 11:0839,48150,647,22720,908,0001,991,0002,478,080
7Dec 18, 2025 11:1339,49049,645,05920,913,0001,991,0002,523,136
8Dec 18, 2025 11:0839,50746,820,57320,922,0001,992,0002,367,488
9Dec 18, 2025 11:0839,54549,136,07420,942,0001,994,0002,498,560
10Dec 18, 2025 11:0839,55548,780,07921,945,000997,0002,502,656
11Dec 18, 2025 11:0839,64556,293,72821,995,000999,0002,506,752
12Dec 18, 2025 11:0839,70045,040,42422,025,0001,001,0002,469,888
13Dec 18, 2025 11:0839,82243,301,69421,089,0002,008,0002,400,256
14Dec 18, 2025 11:1240,06051,708,00321,215,0002,020,0002,383,872
15Dec 18, 2025 11:1240,06946,826,80422,230,0001,010,0002,494,464
16Dec 18, 2025 11:0840,13849,138,80421,256,0002,024,0002,338,816
17Dec 18, 2025 11:1240,26446,805,44922,338,0001,015,0002,355,200
18Dec 18, 2025 11:0840,42940,831,03123,449,00002,498,560
19Dec 18, 2025 11:0840,47145,535,39223,473,00002,473,984
20Dec 18, 2025 11:1340,50750,837,93323,494,00002,473,984
21Dec 18, 2025 11:1240,51647,344,48422,478,0001,021,0002,502,656
22Dec 18, 2025 11:0840,51645,845,50921,456,0002,043,0002,465,792
23Dec 18, 2025 11:0840,54344,734,55022,493,0001,022,0002,404,352
24Dec 18, 2025 11:1240,57446,922,84622,510,0001,023,0002,437,120
25Dec 18, 2025 11:1240,62447,754,32022,538,0001,024,0002,510,848
26Dec 18, 2025 11:1240,66646,083,16223,586,00002,510,848
27Dec 18, 2025 11:0840,67650,349,30822,567,0001,025,0002,396,160
28Dec 18, 2025 11:1340,94155,141,36023,746,00002,519,040