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:26HattonuriHattonuriScore: 36,490Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 21:2634,61246,783,11619,072,0001,003,0002,531,328
2Dec 20, 2025 21:2534,65045,836,53819,093,0001,004,0002,531,328
3Dec 20, 2025 21:3334,97642,941,55418,258,0002,028,0002,281,472
4Dec 20, 2025 21:3335,06447,836,37520,337,00002,531,328
5Dec 20, 2025 21:2635,27439,492,00920,459,00002,535,424
6Dec 20, 2025 21:2735,36744,608,57420,513,00002,367,488
7Dec 20, 2025 21:2635,52239,073,87520,603,00002,535,424
8Dec 20, 2025 21:3335,52441,220,30320,604,00002,273,280
9Dec 20, 2025 21:3335,69149,358,89320,701,00002,535,424
10Dec 20, 2025 21:2735,70045,667,49919,671,0001,035,0002,523,136
11Dec 20, 2025 21:2735,71942,734,13220,717,00002,281,472
12Dec 20, 2025 21:2636,07646,020,14619,928,000996,0002,371,584
13Dec 20, 2025 21:2736,13439,846,67119,960,000998,0002,269,184
14Dec 20, 2025 21:2636,27146,079,74720,036,0001,001,0002,523,136
15Dec 20, 2025 21:3336,38342,423,21320,098,0001,004,0002,273,280
16Dec 20, 2025 21:2936,40743,235,27420,111,0001,005,0002,285,568
17Dec 20, 2025 21:3336,49040,180,35619,149,0002,015,0002,523,136
18Dec 20, 2025 21:2536,51748,110,10419,163,0002,017,0002,523,136
19Dec 20, 2025 21:3336,54341,752,40520,186,0001,009,0002,531,328
20Dec 20, 2025 21:2636,56049,182,13420,196,0001,009,0002,523,136
21Dec 20, 2025 21:2636,83338,921,58420,346,0001,017,0002,523,136
22Dec 20, 2025 21:2536,85343,999,25521,375,00002,523,136
23Dec 20, 2025 21:2637,06740,827,87320,476,0001,023,0002,265,088
24Dec 20, 2025 21:2737,09547,266,15921,515,00002,265,088
25Dec 20, 2025 21:2937,10349,665,17321,520,00002,273,280
26Dec 20, 2025 21:2937,35247,387,47221,664,00002,519,040
27Dec 20, 2025 21:3337,44546,577,18621,718,00002,523,136
28Dec 20, 2025 21:2637,75545,427,56520,903,000995,0002,531,328
29Dec 20, 2025 21:2637,76644,552,79220,909,000995,0002,281,472
30Dec 20, 2025 21:2637,99043,074,33720,031,0002,003,0002,527,232
31Dec 20, 2025 21:3338,02649,330,31420,050,0002,005,0002,531,328
32Dec 20, 2025 21:2738,27945,163,79521,193,0001,009,0002,363,392
33Dec 20, 2025 21:2638,84349,181,88821,505,0001,024,0002,519,040