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 11, 2025 16:10caandocaandoScore: 254,972Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2025 16:10249,741173,099,478119,876,00024,974,0002,420,736
2Sep 11, 2025 16:10250,740173,902,365121,358,00024,071,0002,400,256
3Sep 11, 2025 16:10250,972178,250,154127,494,00018,070,0002,297,856
4Sep 11, 2025 16:10251,797170,986,873123,036,00023,006,0002,396,160
5Sep 11, 2025 16:10254,972174,221,945124,902,00022,982,0002,355,200
6Sep 11, 2025 16:10256,540177,240,009128,821,00019,972,0002,310,144
7Sep 11, 2025 16:10257,400175,780,363130,255,00019,037,0002,392,064
8Sep 11, 2025 16:10257,462174,515,839129,284,00020,044,0002,330,624
9Sep 11, 2025 16:10259,421174,603,853135,418,00015,046,0002,277,376