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 listJan 29, 2025 00:09Code HintsCode HintsScore: 93,595Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 29, 2025 00:1091,68883,570,27952,176,0001,003,0002,535,424
2Jan 29, 2025 00:1091,77873,894,37153,231,00002,416,640
3Jan 29, 2025 00:1091,79176,913,64052,235,0001,004,0002,420,736
4Jan 29, 2025 00:1092,23874,151,98052,489,0001,009,0002,416,640
5Jan 29, 2025 00:0992,56785,211,87753,689,00002,535,424
6Jan 29, 2025 00:1092,90377,158,92452,887,000997,0002,531,328
7Jan 29, 2025 00:0992,90980,246,92651,892,0001,995,0002,535,424
8Jan 29, 2025 00:0993,59572,649,82653,280,0001,005,0002,273,280
9Jan 29, 2025 00:1093,62679,936,58153,298,0001,005,0002,535,424
10Jan 29, 2025 00:1093,87279,229,37554,446,00002,535,424
11Jan 29, 2025 00:0994,13176,181,85754,596,00002,510,848
12Jan 29, 2025 00:0994,19087,476,34353,637,000993,0002,416,640
13Jan 29, 2025 00:1094,24884,383,02253,652,0001,012,0002,514,944
14Jan 29, 2025 00:1094,74583,522,61352,954,0001,998,0002,424,832
15Jan 29, 2025 00:0994,94376,605,37953,065,0002,002,0002,531,328