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 listDec 31, 2025 10:32Aniruddha DebAniruddha DebScore: 9,462Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 31, 2025 10:258,06621,584,5764,678,00002,273,280
2Dec 31, 2025 10:328,10720,927,6802,351,0002,351,0002,535,424
3Dec 31, 2025 10:328,16221,357,1484,734,00002,535,424
4Dec 31, 2025 10:328,19020,110,6194,750,00002,342,912
5Dec 31, 2025 10:338,47420,897,5313,932,000983,0002,342,912
6Dec 31, 2025 10:328,55721,040,7971,985,0002,978,0002,424,832
7Dec 31, 2025 10:328,64323,603,0183,008,0002,005,0002,523,136
8Dec 31, 2025 10:328,75020,608,6733,045,0002,030,0002,535,424
9Dec 31, 2025 10:339,37421,372,2253,262,0002,175,0002,281,472
10Dec 31, 2025 10:339,46024,834,9534,390,0001,097,0002,338,816
11Dec 31, 2025 10:329,46222,782,5253,293,0002,195,0002,342,912
12Dec 31, 2025 10:329,46725,038,6423,295,0002,196,0002,338,816
13Dec 31, 2025 10:259,48427,300,9443,301,0002,200,0002,535,424
14Dec 31, 2025 10:329,54023,624,0583,320,0002,213,0002,531,328
15Dec 31, 2025 10:329,54725,702,0213,322,0002,215,0002,273,280
16Dec 31, 2025 10:329,56225,944,0923,328,0002,218,0002,535,424
17Dec 31, 2025 10:329,64821,645,9674,477,0001,119,0002,342,912
18Dec 31, 2025 10:329,71922,646,1154,510,0001,127,0002,334,720
19Dec 31, 2025 10:329,72817,718,6344,514,0001,128,0002,338,816
20Dec 31, 2025 10:329,95925,655,8174,621,0001,155,0002,465,792
21Dec 31, 2025 10:2510,39019,583,9983,013,0003,013,0002,535,424