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, 2024 18:35Yuriy LyfenkoYuriy LyfenkoError
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, 2024 18:4100000Error: expected "1026784 ", got "1030206 "
2Dec 20, 2024 18:4100000Error: expected "874899 ", got "911602 "
3Dec 20, 2024 18:4000000Error: expected "1029395 ", got "1051016 "
4Dec 20, 2024 18:3700000Error: expected "1942778 ", got "1942921 "
5Dec 20, 2024 18:3600000Error: expected "1324211 ", got "1327512 "
6Dec 20, 2024 18:3600000Error: expected "2152029 ", got "2153331 "
7Dec 20, 2024 18:3500000Error: expected "1079087 ", got "1109488 "
8Dec 20, 2024 18:3500000Error: expected "1381696 ", got "1389467 "
9Dec 20, 2024 18:4932,57449,070,82316,905,0001,988,0002,285,568
10Dec 20, 2024 18:3532,58851,224,15017,907,000994,0002,260,992
11Dec 20, 2024 18:3532,60043,574,94517,913,000995,0002,285,568
12Dec 20, 2024 18:4132,63447,156,91716,936,0001,992,0002,285,568
13Dec 20, 2024 18:3532,74837,813,19216,995,0001,999,0002,277,376
14Dec 20, 2024 18:4132,81444,318,46817,029,0002,003,0002,285,568
15Dec 20, 2024 18:4132,84744,323,54517,046,0002,005,0002,281,472
16Dec 20, 2024 18:4132,87139,299,81118,062,0001,003,0002,277,376
17Dec 20, 2024 18:4032,90946,112,76417,078,0002,009,0002,277,376
18Dec 20, 2024 18:4932,92244,385,52117,085,0002,010,0002,285,568
19Dec 20, 2024 18:3532,99143,082,18617,121,0002,014,0002,396,160
20Dec 20, 2024 18:4133,00741,013,87318,137,0001,007,0002,277,376
21Dec 20, 2024 18:3533,02441,016,87017,138,0002,016,0002,285,568
22Dec 20, 2024 18:4133,03648,284,96618,153,0001,008,0002,285,568
23Dec 20, 2024 18:3533,04138,224,34617,147,0002,017,0002,277,376
24Dec 20, 2024 18:4033,08135,910,67517,168,0002,019,0002,285,568
25Dec 20, 2024 18:4833,22640,325,88017,243,0002,028,0002,285,568
26Dec 20, 2024 18:4933,25040,479,01117,255,0002,030,0002,285,568
27Dec 20, 2024 18:3533,31040,704,75517,287,0002,033,0002,277,376
28Dec 20, 2024 18:3533,42236,870,30817,345,0002,040,0002,285,568
29Dec 20, 2024 18:4133,46943,116,46518,391,0001,021,0002,273,280
30Dec 20, 2024 18:4833,55743,351,17718,439,0001,024,0002,269,184
31Dec 20, 2024 18:4133,55746,283,48218,439,0001,024,0002,285,568
32Dec 20, 2024 18:4033,57843,378,33618,450,0001,025,0002,281,472
33Dec 20, 2024 18:4833,58344,047,88919,478,00002,277,376
34Dec 20, 2024 18:3533,67141,589,64018,502,0001,027,0002,285,568
35Dec 20, 2024 18:4136,21038,921,26319,002,0002,000,0002,285,568