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 27, 2025 17:30Wookyung JeongWookyung JeongScore: 69,843Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 17:2866,94868,966,73831,861,0006,969,0002,363,392
2Sep 27, 2025 17:2767,16258,899,79330,964,0007,990,0002,310,144
3Sep 27, 2025 17:3067,44764,536,52233,101,0006,018,0002,334,720
4Sep 27, 2025 17:2767,59064,629,67835,182,0004,020,0002,404,352
5Sep 27, 2025 17:2867,63162,211,20536,209,0003,017,0002,506,752
6Sep 27, 2025 17:2967,81759,395,31631,266,0008,068,0002,277,376
7Sep 27, 2025 17:2768,77265,563,04831,911,0007,977,0002,338,816
8Sep 27, 2025 17:2868,77864,536,88834,905,0004,986,0002,379,776
9Sep 27, 2025 17:2768,90261,451,37031,971,0007,992,0002,420,736
10Sep 27, 2025 17:2769,19068,040,32536,117,0004,013,0002,293,760
11Sep 27, 2025 17:2969,45961,619,62038,272,0002,014,0002,318,336
12Sep 27, 2025 17:2869,53861,272,64735,291,0005,041,0002,314,240
13Sep 27, 2025 17:2969,84364,040,98134,433,0006,076,0002,265,088
14Sep 27, 2025 17:2770,07669,805,27835,564,0005,080,0002,490,368
15Sep 27, 2025 17:2870,95266,314,45036,134,0005,018,0002,498,560
16Sep 27, 2025 17:2771,06267,753,64935,185,0006,031,0002,306,048
17Sep 27, 2025 17:2871,15065,937,64936,235,0005,032,0002,408,448
18Sep 27, 2025 17:3071,41463,424,66837,379,0004,041,0002,293,760
19Sep 27, 2025 17:2871,54366,387,22736,435,0005,060,0002,449,408
20Sep 27, 2025 17:2871,67963,306,84036,504,0005,070,0002,506,752
21Sep 27, 2025 17:2871,69863,625,56735,500,0006,085,0002,330,624
22Sep 27, 2025 17:2771,85568,721,44935,577,0006,099,0002,506,752
23Sep 27, 2025 17:3072,20972,934,88033,904,0007,977,0002,461,696
24Sep 27, 2025 17:2772,75767,478,36633,157,0009,042,0002,387,968