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:06E SequeiraE SequeiraScore: 75,979Success
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:0675,28669,394,05642,651,0001,015,0002,514,944
2Nov 23, 2024 20:1775,29177,983,32742,654,0001,015,0002,510,848
3Nov 23, 2024 20:0975,43172,345,83542,733,0001,017,0002,510,848
4Nov 23, 2024 20:0675,65570,502,30241,886,0001,994,0002,289,664
5Nov 23, 2024 20:1375,66669,368,97141,892,0001,994,0002,514,944
6Nov 23, 2024 20:0975,66973,419,86541,894,0001,994,0002,289,664
7Nov 23, 2024 20:0675,66971,495,68842,891,000997,0002,523,136
8Nov 23, 2024 20:1575,67274,352,56941,895,0001,995,0002,289,664
9Nov 23, 2024 20:1575,68374,679,72542,899,000997,0002,273,280
10Nov 23, 2024 20:1375,97970,466,11142,065,0002,003,0002,277,376
11Nov 23, 2024 20:1176,02268,663,73142,089,0002,004,0002,514,944
12Nov 23, 2024 20:1176,07167,809,46442,116,0002,005,0002,355,200
13Nov 23, 2024 20:1776,22970,276,19442,204,0002,009,0002,514,944
14Nov 23, 2024 20:1376,53671,387,57043,383,0001,008,0002,289,664
15Nov 23, 2024 20:1577,02971,773,64943,662,0001,015,0002,289,664
16Nov 23, 2024 20:1777,08669,860,63344,710,00002,510,848
17Nov 23, 2024 20:0977,37270,834,89342,882,0001,994,0002,281,472
18Nov 23, 2024 20:1177,46070,613,29042,931,0001,996,0002,289,664