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 14, 2025 20:40HattonuriHattonuriScore: 146,757Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 14, 2025 20:41137,303106,977,44176,612,0003,024,0002,285,568
2Dec 14, 2025 20:41138,400104,098,78676,259,0004,013,0002,334,720
3Dec 14, 2025 20:41138,962109,747,61674,554,0006,044,0002,347,008
4Dec 14, 2025 20:41141,167106,016,83277,883,0003,994,0002,351,104
5Dec 14, 2025 20:41143,788109,704,75980,383,0003,014,0002,486,272
6Dec 14, 2025 20:41146,298112,169,95680,860,0003,993,0002,265,088
7Dec 14, 2025 20:41146,757111,238,37081,114,0004,005,0002,334,720
8Dec 14, 2025 20:41148,241106,430,78880,982,0004,998,0002,330,624
9Dec 14, 2025 20:41150,759110,612,07180,405,0007,035,0002,416,640
10Dec 14, 2025 20:40355,833236,486,405203,378,0003,005,0002,285,568
11Dec 14, 2025 20:40363,841236,852,671207,028,0004,000,0002,326,528
12Dec 14, 2025 20:40371,686242,431,666209,562,0006,016,0002,338,816