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 listJul 13, 2025 16:51Alexey IlyukhovAlexey IlyukhovScore: 141,810Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 13, 2025 16:44135,624105,950,95275,637,0003,025,0002,478,080
2Jul 13, 2025 16:44136,271102,970,87277,037,0002,000,0002,420,736
3Jul 13, 2025 16:44139,419112,377,91976,870,0003,993,0002,412,544
4Jul 13, 2025 16:51139,424114,264,30677,871,0002,995,0002,392,064
5Jul 13, 2025 16:44139,588101,601,83776,963,0003,998,0002,355,200
6Jul 13, 2025 16:44140,729106,759,59180,616,0001,007,0002,383,872
7Jul 13, 2025 16:51141,810106,375,22780,244,0002,006,0002,449,408
8Jul 13, 2025 16:44143,090116,960,02577,993,0004,999,0002,461,696
9Jul 13, 2025 16:44144,562109,776,69680,852,0002,994,0002,449,408
10Jul 13, 2025 16:44145,366107,072,02381,301,0003,011,0002,371,584
11Jul 13, 2025 16:44146,559112,147,20480,004,0005,000,0002,375,680
12Jul 13, 2025 16:51147,248109,706,86584,400,0001,004,0002,441,216