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 29, 2025 18:40Wookyung JeongWookyung JeongScore: 60,512Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 29, 2025 18:4554,94556,725,23926,889,0004,979,0002,498,560
2Sep 29, 2025 18:4555,94061,768,15027,376,0005,069,0002,260,992
3Sep 29, 2025 18:4157,54561,294,87028,319,0005,057,0002,404,352
4Sep 29, 2025 18:4159,40757,492,48429,389,0005,067,0002,482,176
5Sep 29, 2025 18:4060,51259,377,58828,078,0007,019,0002,334,720
6Sep 29, 2025 18:4061,07458,353,64030,363,0005,060,0002,408,448
7Sep 29, 2025 18:4061,30359,924,26032,509,0003,047,0002,428,928
8Sep 29, 2025 18:4563,11260,097,80931,521,0005,084,0002,420,736
9Sep 29, 2025 18:4165,46263,892,74733,972,0003,996,0002,482,176