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 16, 2025 23:30HattonuriHattonuriScore: 41,884Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 16, 2025 23:3037,02142,963,24920,450,0001,022,0002,330,624
2Dec 16, 2025 23:2938,73647,557,45522,467,00002,502,656
3Dec 16, 2025 23:3039,06749,129,02822,659,00002,269,184
4Dec 16, 2025 23:2939,07149,335,05021,631,0001,030,0002,531,328
5Dec 16, 2025 23:3039,50051,508,05221,914,000996,0002,269,184
6Dec 16, 2025 23:2939,77947,519,87022,069,0001,003,0002,269,184
7Dec 16, 2025 23:3039,79047,210,17721,072,0002,006,0002,506,752
8Dec 16, 2025 23:2939,83346,748,86721,094,0002,009,0002,494,464
9Dec 16, 2025 23:3039,85349,614,63521,105,0002,010,0002,269,184
10Dec 16, 2025 23:3040,03346,072,20021,200,0002,019,0002,506,752
11Dec 16, 2025 23:3040,39551,097,42122,411,0001,018,0002,506,752
12Dec 16, 2025 23:3040,46051,160,65821,427,0002,040,0002,531,328
13Dec 16, 2025 23:3041,13350,963,59322,863,000994,0002,531,328
14Dec 16, 2025 23:2941,42153,342,32322,022,0002,002,0002,527,232
15Dec 16, 2025 23:3041,43844,645,64123,033,0001,001,0002,527,232
16Dec 16, 2025 23:3041,59549,510,99823,120,0001,005,0002,506,752
17Dec 16, 2025 23:3041,62945,849,68522,133,0002,012,0002,330,624
18Dec 16, 2025 23:2941,73146,262,70222,187,0002,017,0002,527,232
19Dec 16, 2025 23:2941,88445,556,99322,269,0002,024,0002,527,232
20Dec 16, 2025 23:2941,96748,546,23724,341,00002,506,752
21Dec 16, 2025 23:2942,39747,550,92123,566,0001,024,0002,502,656
22Dec 16, 2025 23:3042,42449,621,95623,581,0001,025,0002,527,232
23Dec 16, 2025 23:3042,45248,017,17623,597,0001,025,0002,330,624
24Dec 16, 2025 23:2942,77953,790,67624,812,00002,498,560
25Dec 16, 2025 23:3042,86749,443,53924,863,00002,527,232
26Dec 16, 2025 23:3042,91049,815,96523,893,000995,0002,527,232
27Dec 16, 2025 23:2942,93846,490,24124,904,00002,527,232
28Dec 16, 2025 23:3042,99847,771,23923,942,000997,0002,498,560
29Dec 16, 2025 23:2943,05547,845,69323,974,000998,0002,269,184
30Dec 16, 2025 23:3043,20946,497,90924,059,0001,002,0002,502,656
31Dec 16, 2025 23:2943,24350,128,76124,078,0001,003,0002,494,464
32Dec 16, 2025 23:3043,25250,397,11424,083,0001,003,0002,527,232
33Dec 16, 2025 23:2943,54545,214,70923,236,0002,020,0002,527,232
34Dec 16, 2025 23:2943,56652,445,14624,258,0001,010,0002,269,184
35Dec 16, 2025 23:3043,68647,549,77323,311,0002,027,0002,527,232
36Dec 16, 2025 23:3043,93151,118,26425,480,00002,502,656