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 01:55HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:15Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:1508,467,542849,000020,480No answer to stdout. Died or killed?
2Dec 21, 2025 01:4832,80243,502,57017,023,0002,002,0002,433,024
3Dec 21, 2025 01:4833,00942,982,87817,130,0002,015,0002,433,024
4Dec 21, 2025 01:4133,02846,172,27718,148,0001,008,0002,424,832
5Dec 21, 2025 01:5533,22842,986,97818,258,0001,014,0002,531,328
6Dec 21, 2025 01:4033,41644,683,86718,361,0001,020,0002,531,328
7Dec 21, 2025 01:5533,86749,801,90319,643,00002,420,736
8Dec 21, 2025 01:4033,90548,153,72418,630,0001,035,0002,531,328
9Dec 21, 2025 01:5534,02144,142,09219,732,00002,523,136
10Dec 21, 2025 01:4034,31745,096,76517,914,0001,990,0002,424,832
11Dec 21, 2025 01:4134,32645,586,08718,914,000995,0002,269,184
12Dec 21, 2025 01:5534,67646,830,81518,101,0002,011,0002,523,136
13Dec 21, 2025 01:4034,75942,937,72819,152,0001,008,0002,424,832
14Dec 21, 2025 01:4134,89737,205,77318,216,0002,024,0002,527,232
15Dec 21, 2025 01:4034,96943,584,22420,282,00002,531,328
16Dec 21, 2025 01:4834,97643,353,74018,258,0002,028,0002,531,328
17Dec 21, 2025 01:4835,15540,847,01018,351,0002,039,0002,420,736
18Dec 21, 2025 01:5535,49847,605,24519,560,0001,029,0002,424,832
19Dec 21, 2025 01:4835,58648,246,77219,608,0001,032,0002,531,328
20Dec 21, 2025 01:4135,97848,564,48318,880,0001,987,0002,428,928
21Dec 21, 2025 01:5536,04049,031,30518,913,0001,990,0002,523,136
22Dec 21, 2025 01:4136,04546,897,00819,911,000995,0002,523,136
23Dec 21, 2025 01:4836,05239,816,17919,915,000995,0002,531,328
24Dec 21, 2025 01:5536,15743,320,14818,974,0001,997,0002,416,640
25Dec 21, 2025 01:4036,31643,822,92919,057,0002,006,0002,523,136
26Dec 21, 2025 01:5536,31742,781,70419,058,0002,006,0002,523,136
27Dec 21, 2025 01:4836,32248,200,35020,064,0001,003,0002,416,640
28Dec 21, 2025 01:5536,34746,727,77019,074,0002,007,0002,523,136
29Dec 21, 2025 01:4836,36641,483,35519,084,0002,008,0002,420,736
30Dec 21, 2025 01:4836,45344,587,39219,130,0002,013,0002,523,136
31Dec 21, 2025 01:5536,61438,448,52320,225,0001,011,0002,523,136
32Dec 21, 2025 01:4136,82242,232,81420,340,0001,017,0002,420,736
33Dec 21, 2025 01:5536,86444,288,67421,381,00002,416,640
34Dec 21, 2025 01:5537,04841,157,34420,465,0001,023,0002,420,736