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 listSep 27, 2025 14:58Wookyung JeongWookyung JeongScore: 84,291Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 14:5777,86067,632,28640,142,0005,017,0002,396,160
2Sep 27, 2025 14:5780,40074,236,86839,536,0007,096,0002,498,560
3Sep 27, 2025 14:5780,85372,597,36439,911,0006,984,0002,424,832
4Sep 27, 2025 14:5782,55377,707,49540,899,0006,982,0002,285,568
5Sep 27, 2025 14:5882,90276,522,44441,071,0007,012,0002,269,184
6Sep 27, 2025 14:5783,14877,622,05842,198,0006,028,0002,306,048
7Sep 27, 2025 14:5783,46470,634,09742,358,0006,051,0002,285,568
8Sep 27, 2025 14:5784,29173,452,22343,901,0004,988,0002,416,640
9Sep 27, 2025 14:5785,52179,769,22541,504,0008,098,0002,277,376
10Sep 27, 2025 14:5787,95274,065,56345,011,0006,001,0002,363,392
11Sep 27, 2025 14:5788,02679,892,48145,049,0006,006,0002,420,736
12Sep 27, 2025 14:5788,48177,752,35742,263,0009,056,0002,314,240
13Sep 27, 2025 14:5788,87679,968,50248,516,0003,032,0002,347,008
14Sep 27, 2025 14:5889,02477,723,20642,522,0009,112,0002,457,600
15Sep 27, 2025 14:5889,04078,454,96945,568,0006,075,0002,473,984