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 26, 2025 14:15Code HintsCode HintsScore: 93,314Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 26, 2025 14:1591,62676,997,08452,141,0001,002,0002,535,424
2Jan 26, 2025 14:2091,68677,298,46351,172,0002,006,0002,420,736
3Jan 26, 2025 14:1591,74176,742,33752,207,0001,003,0002,535,424
4Jan 26, 2025 14:1591,74573,457,75151,204,0002,008,0002,535,424
5Jan 26, 2025 14:1891,82676,785,54553,259,00002,535,424
6Jan 26, 2025 14:1992,02173,842,53752,365,0001,007,0002,416,640
7Jan 26, 2025 14:2092,22173,081,90153,488,00002,412,544
8Jan 26, 2025 14:1992,24177,796,99352,491,0001,009,0002,514,944
9Jan 26, 2025 14:1592,66081,577,40452,729,0001,014,0002,416,640
10Jan 26, 2025 14:2092,84179,873,21452,851,000997,0002,412,544
11Jan 26, 2025 14:1592,89779,691,51351,885,0001,995,0002,535,424
12Jan 26, 2025 14:1593,10782,312,84052,002,0002,000,0002,535,424
13Jan 26, 2025 14:1893,17875,113,95653,043,0001,000,0002,535,424
14Jan 26, 2025 14:3493,31474,440,02152,118,0002,004,0002,535,424
15Jan 26, 2025 14:1893,35376,125,61852,140,0002,005,0002,416,640
16Jan 26, 2025 14:1893,74381,387,56554,371,00002,535,424
17Jan 26, 2025 14:1893,89886,360,74454,461,00002,273,280
18Jan 26, 2025 14:3494,12480,109,92654,592,00002,269,184
19Jan 26, 2025 14:1894,16976,620,39753,607,0001,011,0002,535,424
20Jan 26, 2025 14:1594,20382,994,71653,645,000993,0002,269,184
21Jan 26, 2025 14:1594,30079,459,18253,682,0001,012,0002,535,424
22Jan 26, 2025 14:1894,62680,565,04853,886,000997,0002,531,328
23Jan 26, 2025 14:1894,64784,273,83553,879,0001,016,0002,535,424
24Jan 26, 2025 14:1594,65982,342,90153,904,000998,0002,531,328
25Jan 26, 2025 14:1994,77481,165,98852,971,0001,998,0002,412,544
26Jan 26, 2025 14:3494,91276,698,42654,049,0001,000,0002,273,280
27Jan 26, 2025 14:1894,92680,707,29653,055,0002,002,0002,416,640