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 2, 2026 07:40RussellStrongRussellStrongScore: 101,459Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 2, 2026 07:2799,95282,540,02055,973,0001,999,0002,277,376
2Jan 2, 2026 07:26100,04383,006,59156,025,0002,000,0002,289,664
3Jan 2, 2026 07:27100,06687,922,63957,038,0001,000,0002,281,472
4Jan 2, 2026 07:26100,34576,838,86157,197,0001,003,0002,334,720
5Jan 2, 2026 07:27100,40982,877,11557,233,0001,004,0002,347,008
6Jan 2, 2026 07:27100,69182,669,98056,388,0002,013,0002,338,816
7Jan 2, 2026 07:26100,77480,414,18455,426,0003,023,0002,277,376
8Jan 2, 2026 07:40100,96785,573,59956,542,0002,019,0002,285,568
9Jan 2, 2026 07:27101,05580,132,51656,591,0002,021,0002,338,816
10Jan 2, 2026 07:40101,10786,847,98156,620,0002,022,0002,338,816
11Jan 2, 2026 07:40101,13386,093,43657,646,0001,011,0002,330,624
12Jan 2, 2026 07:27101,17684,975,55256,659,0002,023,0002,338,816
13Jan 2, 2026 07:26101,19184,309,08956,668,0002,023,0002,281,472
14Jan 2, 2026 07:27101,20286,392,17756,673,0002,024,0002,338,816
15Jan 2, 2026 07:26101,27486,001,38557,727,0001,012,0002,347,008
16Jan 2, 2026 07:27101,29585,724,18057,739,0001,012,0002,330,624
17Jan 2, 2026 07:26101,45985,181,91854,857,0003,989,0002,342,912
18Jan 2, 2026 07:40101,50585,723,39355,880,0002,993,0002,347,008
19Jan 2, 2026 07:40101,50786,594,82756,879,0001,995,0002,342,912
20Jan 2, 2026 07:26101,50990,922,95256,880,0001,995,0002,338,816
21Jan 2, 2026 07:26101,51686,923,39555,886,0002,993,0002,281,472
22Jan 2, 2026 07:40101,52483,889,79656,888,0001,996,0002,338,816
23Jan 2, 2026 07:27101,52684,670,74355,891,0002,994,0002,342,912
24Jan 2, 2026 07:27101,52885,944,21055,892,0002,994,0002,342,912
25Jan 2, 2026 07:27101,54186,607,18755,900,0002,994,0002,342,912
26Jan 2, 2026 07:26101,54185,829,79657,896,000998,0002,342,912
27Jan 2, 2026 07:40101,54386,059,38156,899,0001,996,0002,326,528
28Jan 2, 2026 07:27101,55085,063,52957,901,000998,0002,330,624
29Jan 2, 2026 07:40101,69885,034,93255,986,0002,999,0002,347,008
30Jan 2, 2026 07:40101,70289,073,42757,988,000999,0002,338,816
31Jan 2, 2026 07:40101,77884,238,49858,031,0001,000,0002,334,720
32Jan 2, 2026 07:40101,77882,212,70056,030,0003,001,0002,338,816
33Jan 2, 2026 07:40103,69182,541,35258,137,0002,004,0002,277,376