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 listFeb 24, 2026 14:49Jem BishopJem BishopScore: 3,902Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 24, 2026 14:493,62122,269,8891,050,0001,050,0002,519,040
2Feb 24, 2026 14:493,66417,626,89602,125,0002,502,656
3Feb 24, 2026 14:493,67224,570,83202,130,0002,297,856
4Feb 24, 2026 14:493,68620,194,04302,138,0002,527,232
5Feb 24, 2026 14:493,69017,774,3591,070,0001,070,0002,306,048
6Feb 24, 2026 14:493,72117,808,6991,079,0001,079,0002,281,472
7Feb 24, 2026 14:493,87920,673,95902,250,0002,502,656
8Feb 24, 2026 14:493,90220,067,13702,263,0002,273,280
9Feb 24, 2026 14:493,90316,756,6241,132,0001,132,0002,494,464
10Feb 24, 2026 14:493,92424,167,1371,138,0001,138,0002,277,376
11Feb 24, 2026 14:493,96219,808,2201,149,0001,149,0002,527,232
12Feb 24, 2026 14:493,96417,656,32902,299,0002,289,664
13Feb 24, 2026 14:493,96618,783,82302,300,0002,301,952
14Feb 24, 2026 14:493,97821,671,44202,307,0002,277,376
15Feb 24, 2026 14:493,97918,853,18402,308,0002,514,944