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 26, 2025 11:28HattonuriHattonuriScore: 30,210Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 11:3529,16445,778,15115,920,000995,0002,478,080
2Dec 26, 2025 11:2329,17143,899,02415,924,000995,0002,322,432
3Dec 26, 2025 11:2529,33638,169,18216,015,0001,000,0002,523,136
4Dec 26, 2025 11:2429,62234,298,19316,171,0001,010,0002,457,600
5Dec 26, 2025 11:3529,89838,509,12916,321,0001,020,0002,330,624
6Dec 26, 2025 11:2530,03638,256,39916,397,0001,024,0002,519,040
7Dec 26, 2025 11:2830,19544,418,98817,513,00002,301,952
8Dec 26, 2025 11:2430,21039,378,20017,522,00002,510,848
9Dec 26, 2025 11:2330,29134,070,10316,536,0001,033,0002,297,856
10Dec 26, 2025 11:2330,41244,955,71417,639,00002,469,888
11Dec 26, 2025 11:3530,43447,488,12417,652,00002,371,584
12Dec 26, 2025 11:2830,46645,809,62316,631,0001,039,0002,289,664
13Dec 26, 2025 11:2430,63441,884,42017,768,00002,490,368
14Dec 26, 2025 11:2830,81743,879,58615,888,0001,986,0002,281,472
15Dec 26, 2025 11:2530,89037,835,33816,921,000995,0002,535,424