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 17:12Wookyung JeongWookyung JeongScore: 80,462Success
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 17:1274,97475,621,15538,429,0005,056,0002,318,336
2Sep 27, 2025 17:1177,05570,620,67237,740,0006,952,0002,363,392
3Sep 27, 2025 17:1278,11074,583,19239,264,0006,040,0002,318,336
4Sep 27, 2025 17:1278,68472,439,17340,567,0005,070,0002,387,968
5Sep 27, 2025 17:1178,72870,529,37639,574,0006,088,0002,490,368
6Sep 27, 2025 17:1279,11271,980,35739,900,0005,985,0002,314,240
7Sep 27, 2025 17:1279,95273,958,93239,316,0007,056,0002,441,216
8Sep 27, 2025 17:1280,46276,194,64840,581,0006,087,0002,506,752
9Sep 27, 2025 17:1280,79370,328,44441,875,0004,985,0002,502,656
10Sep 27, 2025 17:1280,84873,958,16439,908,0006,984,0002,416,640
11Sep 27, 2025 17:1181,96473,736,58140,459,0007,080,0002,387,968
12Sep 27, 2025 17:1282,70271,860,02243,970,0003,997,0002,273,280
13Sep 27, 2025 17:1282,88373,711,99740,060,0008,012,0002,297,856
14Sep 27, 2025 17:1282,94170,607,38441,091,0007,015,0002,510,848
15Sep 27, 2025 17:1283,54569,537,37346,437,0002,019,0002,367,488