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 listAug 11, 2025 20:54CadovvlCadovvlScore: 70,548Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 13, 2025 08:4670,02269,098,32840,613,00002,260,992
2Aug 13, 2025 09:1270,10767,062,68239,646,0001,016,0002,383,872
3Aug 11, 2025 20:5870,26469,789,00839,735,0001,018,0002,387,968
4Aug 11, 2025 21:0070,43667,870,60139,857,000996,0002,510,848
5Aug 11, 2025 20:5770,47966,985,76540,878,00002,519,040
6Aug 11, 2025 20:5770,49765,765,66238,894,0001,994,0002,510,848
7Aug 11, 2025 21:0070,50268,992,44839,894,000997,0002,256,896
8Aug 11, 2025 20:5870,50374,587,04339,895,000997,0002,519,040
9Aug 12, 2025 09:5970,50371,629,09540,892,00002,519,040
10Aug 13, 2025 08:4670,51271,744,97038,902,0001,995,0002,510,848
11Aug 13, 2025 08:4670,51472,867,76840,898,00002,519,040
12Aug 13, 2025 09:1270,51771,591,63439,903,000997,0002,256,896
13Aug 13, 2025 08:4670,52469,483,11338,909,0001,995,0002,510,848
14Aug 13, 2025 08:4670,54870,043,76038,922,0001,996,0002,256,896
15Aug 13, 2025 08:4670,56665,287,79039,930,000998,0002,383,872
16Aug 11, 2025 20:5470,57267,221,54739,934,000998,0002,519,040
17Aug 12, 2025 09:5970,60571,816,02838,954,0001,997,0002,510,848
18Aug 11, 2025 20:5470,62865,546,37639,965,000999,0002,519,040
19Aug 11, 2025 20:5770,63366,578,59739,968,000999,0002,510,848
20Aug 13, 2025 08:4670,73368,709,17141,025,00002,519,040
21Aug 13, 2025 09:1270,74771,744,88339,032,0002,001,0002,510,848
22Aug 11, 2025 20:5470,84569,469,05339,086,0002,004,0002,375,680
23Aug 13, 2025 08:4670,98165,660,70739,161,0002,008,0002,383,872
24Aug 11, 2025 21:0071,02466,481,74239,185,0002,009,0002,519,040
25Aug 11, 2025 20:5871,05266,724,95240,205,0001,005,0002,519,040
26Aug 13, 2025 08:4671,08166,355,34640,222,0001,005,0002,502,656
27Aug 12, 2025 09:5971,17862,438,05040,277,0001,006,0002,375,680