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 28, 2025 23:49alxmrcktralxmrcktrScore: 93,074Success
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:1591,82874,470,07153,260,00002,420,736
2Jan 28, 2025 23:4991,84078,592,09151,257,0002,010,0002,535,424
3Jan 29, 2025 00:1592,33173,032,51552,542,0001,010,0002,416,640
4Jan 29, 2025 00:1592,59379,004,16753,704,00002,535,424
5Jan 29, 2025 00:1592,60081,497,39752,695,0001,013,0002,273,280
6Jan 29, 2025 00:1592,61276,428,36652,702,0001,013,0002,424,832
7Jan 28, 2025 23:4992,62482,678,22352,709,0001,013,0002,535,424
8Jan 29, 2025 00:1492,89078,496,05651,881,0001,995,0002,420,736
9Jan 29, 2025 00:1592,90579,105,28652,888,000997,0002,412,544
10Jan 29, 2025 00:1592,91777,159,13950,898,0002,994,0002,535,424
11Jan 29, 2025 00:1592,93179,983,33052,902,000998,0002,523,136
12Jan 29, 2025 00:1592,96478,454,19452,921,000998,0002,535,424
13Jan 29, 2025 00:1493,07482,017,22851,984,0001,999,0002,523,136
14Jan 28, 2025 23:5194,12176,134,43354,590,00002,535,424
15Jan 29, 2025 00:1594,12175,326,07954,590,00002,420,736
16Jan 29, 2025 00:1594,37480,652,30753,724,0001,013,0002,420,736
17Jan 29, 2025 00:1594,41679,701,78853,747,0001,014,0002,514,944
18Jan 29, 2025 00:1494,66784,720,89153,891,0001,016,0002,420,736
19Jan 28, 2025 23:4994,77982,380,70653,973,000999,0002,535,424
20Jan 28, 2025 23:5194,81680,198,26853,994,000999,0002,535,424
21Jan 28, 2025 23:5195,05383,234,05054,129,0001,002,0002,273,280
22Jan 29, 2025 00:1595,25078,009,38953,237,0002,008,0002,416,640
23Jan 29, 2025 00:1595,81977,904,92655,575,00002,273,280
24Jan 29, 2025 00:1595,87283,525,51854,595,0001,011,0002,535,424