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:04Josu San MartinJosu San MartinScore: 21,771Success
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:0420,53438,770,47911,910,00002,383,872
2Dec 28, 2025 18:0420,84833,640,83610,077,0002,015,0002,510,848
3Dec 28, 2025 18:0420,92928,709,45211,128,0001,011,0002,400,256
4Dec 28, 2025 18:0420,99134,974,92510,146,0002,029,0002,387,968
5Dec 28, 2025 18:0421,16427,163,15712,275,00002,334,720
6Dec 28, 2025 18:0421,23330,214,49412,315,00002,412,544
7Dec 28, 2025 18:0421,46633,235,81712,450,00002,449,408
8Dec 28, 2025 18:0421,66029,084,96812,563,00002,482,176
9Dec 28, 2025 18:0421,74530,216,96111,561,0001,051,0002,461,696
10Dec 28, 2025 18:0421,77129,083,19211,575,0001,052,0002,379,776
11Dec 28, 2025 18:0421,77829,881,91011,579,0001,052,0002,392,064
12Dec 28, 2025 18:0421,82432,080,41411,604,0001,054,0002,355,200
13Dec 28, 2025 18:0421,85731,657,86511,621,0001,056,0002,498,560
14Dec 28, 2025 18:0421,92129,988,39012,714,00002,334,720
15Dec 28, 2025 18:0421,97646,449,87212,746,00002,441,216
16Dec 28, 2025 18:0422,02141,326,16412,772,00002,510,848
17Dec 28, 2025 18:0422,04033,621,26011,718,0001,065,0002,375,680
18Dec 28, 2025 18:0422,28132,716,92711,929,000994,0002,347,008