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 listNov 23, 2024 20:22E SequeiraE SequeiraScore: 74,340Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 23, 2024 20:5273,47163,702,06141,599,0001,014,0002,523,136
2Nov 23, 2024 20:5273,52877,130,28242,646,00002,514,944
3Nov 23, 2024 21:5973,60575,846,53241,675,0001,016,0002,514,944
4Nov 23, 2024 20:5273,62668,018,14342,703,00002,514,944
5Nov 23, 2024 20:2273,66669,435,06041,709,0001,017,0002,510,848
6Nov 23, 2024 20:2273,67272,961,55841,713,0001,017,0002,289,664
7Nov 23, 2024 21:0373,72174,248,59541,740,0001,018,0002,359,296
8Nov 23, 2024 21:0373,87172,268,46140,853,0001,992,0002,289,664
9Nov 23, 2024 21:0373,92972,463,63140,885,0001,994,0002,289,664
10Nov 23, 2024 21:0373,94178,467,29240,892,0001,994,0002,514,944
11Nov 23, 2024 20:2273,94573,008,72540,894,0001,994,0002,514,944
12Nov 23, 2024 20:5273,95368,617,68540,898,0001,995,0002,510,848
13Nov 23, 2024 20:5273,99072,706,06640,918,0001,996,0002,289,664
14Nov 23, 2024 20:2274,11967,312,57840,990,0001,999,0002,506,752
15Nov 23, 2024 20:5274,20368,277,14641,037,0002,001,0002,289,664
16Nov 23, 2024 20:2274,34066,308,83341,112,0002,005,0002,289,664
17Nov 23, 2024 20:2274,49169,356,33641,196,0002,009,0002,510,848
18Nov 23, 2024 21:5974,81668,284,24142,384,0001,009,0002,289,664
19Nov 23, 2024 21:0374,81771,391,94742,385,0001,009,0002,510,848
20Nov 23, 2024 20:2274,94170,791,73242,456,0001,010,0002,510,848
21Nov 23, 2024 21:0375,00564,678,85742,492,0001,011,0002,285,568
22Nov 23, 2024 20:2275,03169,083,19642,506,0001,012,0002,289,664
23Nov 23, 2024 20:2275,13472,423,68942,565,0001,013,0002,514,944
24Nov 23, 2024 21:5975,30272,751,28842,660,0001,015,0002,514,944
25Nov 23, 2024 21:0375,37174,552,62342,699,0001,016,0002,514,944
26Nov 23, 2024 20:5275,39375,843,54043,728,00002,265,088
27Nov 23, 2024 20:5275,64869,432,91942,879,000997,0002,289,664
28Nov 23, 2024 21:0375,66275,494,62841,890,0001,994,0002,514,944
29Nov 23, 2024 21:0375,66472,232,89841,891,0001,994,0002,523,136
30Nov 23, 2024 20:5275,76070,937,01541,944,0001,997,0002,289,664