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 28, 2025 18:18Josu San MartinJosu San MartinScore: 6,748Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 18:096,27919,491,6723,642,00002,273,280
2Dec 28, 2025 18:096,28121,222,2303,643,00002,523,136
3Dec 28, 2025 18:096,30323,230,2833,656,00002,285,568
4Dec 28, 2025 18:096,33127,378,7482,448,0001,224,0002,301,952
5Dec 28, 2025 18:096,45521,501,3542,496,0001,248,0002,510,848
6Dec 28, 2025 18:096,49119,330,7442,510,0001,255,0002,478,080
7Dec 28, 2025 18:096,74821,716,7932,936,000978,0002,404,352
8Dec 28, 2025 18:186,75225,261,7771,958,0001,958,0002,269,184
9Dec 28, 2025 18:186,75320,044,2692,938,000979,0002,273,280
10Dec 28, 2025 18:186,75522,981,677979,0002,939,0002,416,640
11Dec 28, 2025 18:096,76719,964,1412,944,000981,0002,510,848
12Dec 28, 2025 18:096,79520,550,9572,956,000985,0002,265,088