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 listOct 10, 2023 04:12matsuoka-601matsuoka-601Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 10, 2023 04:1200000Error: No answer to stdout. Died or killed?stderr
2Oct 10, 2023 04:1253,39554,871,23025,974,0004,995,0002,273,280
3Oct 10, 2023 04:1253,48853,302,30628,021,0003,002,0002,527,232
4Oct 10, 2023 04:1253,49358,717,53526,022,0005,004,0002,527,232
5Oct 10, 2023 04:1253,64856,537,45327,101,0004,015,0002,519,040
6Oct 10, 2023 04:1253,77453,528,69428,171,0003,018,0002,510,848
7Oct 10, 2023 04:1253,86954,793,67025,197,0006,047,0002,281,472
8Oct 10, 2023 04:1254,25354,720,68927,407,0004,060,0002,527,232
9Oct 10, 2023 04:1254,65559,594,89927,610,0004,090,0002,277,376
10Oct 10, 2023 04:1254,75355,333,96027,660,0004,097,0002,281,472
11Oct 10, 2023 04:1258,86661,132,01431,130,0003,012,0002,281,472
12Oct 10, 2023 04:1258,89354,223,31129,135,0005,023,0002,273,280
13Oct 10, 2023 04:1259,02153,301,95333,226,0001,006,0002,514,944
14Oct 10, 2023 04:1259,13357,587,13423,201,00011,096,0002,514,944
15Oct 10, 2023 04:1259,37464,640,65031,399,0003,038,0002,510,848
16Oct 10, 2023 04:1259,43460,141,85131,431,0003,041,0002,510,848
17Oct 10, 2023 04:1259,65255,139,10732,563,0002,035,0002,510,848
18Oct 10, 2023 04:1260,11463,373,69132,874,0001,992,0002,281,472
19Oct 10, 2023 04:1260,16466,994,98133,898,000997,0002,514,944
20Oct 10, 2023 04:1267,576110,140,51639,194,01102,232,320
21Oct 10, 2023 04:1268,546107,785,15839,756,51702,240,512
22Oct 10, 2023 04:1268,824114,231,49639,918,11002,248,704