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 30, 2025 14:28Wookyung JeongWookyung JeongScore: 55,772Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 30, 2025 14:3051,88454,666,72427,084,0003,009,0002,445,312
2Sep 30, 2025 14:2852,11456,139,42528,211,0002,015,0002,510,848
3Sep 30, 2025 14:2752,51453,656,00629,443,0001,015,0002,482,176
4Sep 30, 2025 14:3152,89760,116,69629,658,0001,022,0002,404,352
5Sep 30, 2025 14:2853,01260,636,60729,723,0001,024,0002,437,120
6Sep 30, 2025 14:2853,52465,491,87928,040,0003,004,0002,285,568
7Sep 30, 2025 14:3053,89355,226,34927,225,0004,033,0002,433,024
8Sep 30, 2025 14:2753,97452,769,09528,276,0003,029,0002,441,216
9Sep 30, 2025 14:2754,04860,034,34028,315,0003,033,0002,265,088
10Sep 30, 2025 14:2754,43153,433,11630,552,0001,018,0002,469,888
11Sep 30, 2025 14:3154,97660,430,40128,897,0002,989,0002,273,280
12Sep 30, 2025 14:2855,33459,300,24528,083,0004,011,0002,486,272
13Sep 30, 2025 14:2855,77254,573,94730,327,0002,021,0002,301,952
14Sep 30, 2025 14:3155,89358,708,41328,366,0004,052,0002,396,160
15Sep 30, 2025 14:2756,12157,903,72231,533,0001,017,0002,400,256
16Sep 30, 2025 14:2856,23860,543,10230,580,0002,038,0002,441,216
17Sep 30, 2025 14:2856,77857,488,11730,936,0001,995,0002,301,952
18Sep 30, 2025 14:2857,22859,760,76730,175,0003,017,0002,285,568
19Sep 30, 2025 14:2857,36754,576,38030,249,0003,024,0002,392,064
20Sep 30, 2025 14:2757,52956,266,46229,323,0004,044,0002,334,720
21Sep 30, 2025 14:2858,06760,242,11431,638,0002,041,0002,465,792
22Sep 30, 2025 14:3058,86056,353,88527,111,0007,028,0002,281,472
23Sep 30, 2025 14:2859,79861,638,63332,643,0002,040,0002,363,392
24Sep 30, 2025 14:2859,90559,684,85033,724,0001,021,0002,306,048