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 21, 2025 00:07Josu San MartinJosu San MartinScore: 37,193Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 01:3935,51743,164,72320,600,00002,420,736
2Dec 21, 2025 00:0736,04053,021,01819,908,000995,0002,490,368
3Dec 21, 2025 00:0736,05051,141,01519,914,000995,0002,433,024
4Dec 21, 2025 01:4036,05048,124,43119,914,000995,0002,351,104
5Dec 21, 2025 00:0736,05751,739,83019,918,000995,0002,359,296
6Dec 21, 2025 01:3936,36950,567,20320,090,0001,004,0002,412,544
7Dec 21, 2025 01:4036,39147,558,76319,097,0002,010,0002,396,160
8Dec 21, 2025 00:0736,39345,061,08520,103,0001,005,0002,285,568
9Dec 21, 2025 01:3936,58643,098,76120,210,0001,010,0002,506,752
10Dec 21, 2025 01:4036,71043,463,06619,265,0002,027,0002,404,352
11Dec 21, 2025 01:3936,80351,891,32921,346,00002,379,776
12Dec 21, 2025 01:3737,04045,011,83820,460,0001,023,0002,482,176
13Dec 21, 2025 01:3737,19344,074,32621,572,00002,359,296
14Dec 21, 2025 01:4037,19543,961,13920,546,0001,027,0002,387,968
15Dec 21, 2025 01:4037,22647,865,60620,563,0001,028,0002,428,928
16Dec 21, 2025 00:0737,29744,903,72920,602,0001,030,0002,469,888
17Dec 21, 2025 00:0737,29738,889,87521,632,00002,469,888
18Dec 21, 2025 01:3937,37647,438,51720,646,0001,032,0002,420,736
19Dec 21, 2025 00:0737,50549,474,74121,753,00002,420,736
20Dec 21, 2025 00:0737,73450,365,14919,897,0001,989,0002,433,024
21Dec 21, 2025 01:3937,76039,145,51719,910,0001,991,0002,322,432
22Dec 21, 2025 01:4037,88851,752,31919,978,0001,997,0002,428,928
23Dec 21, 2025 01:3737,96950,183,70321,021,0001,001,0002,408,448
24Dec 21, 2025 00:0738,08449,362,31920,081,0002,008,0002,449,408