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 25, 2025 13:13HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:46Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:4702,967,434,7422,958,834,00002,117,632expected "999999000 ", got "100000 "
2Jul 5, 2026 20:4702,967,663,8142,958,461,0001,000,0002,117,632expected "999999000 ", got "100000 "
3Jul 5, 2026 20:4702,967,040,6372,957,968,0001,028,0002,117,632expected "999999000 ", got "100000 "
4Dec 25, 2025 13:0529,18634,249,38615,933,000995,0002,506,752
5Dec 25, 2025 13:0829,24341,805,50815,964,000997,0002,326,528
6Dec 25, 2025 13:0229,25744,711,10715,971,000998,0002,326,528
7Dec 25, 2025 13:1329,37134,363,20416,033,0001,002,0002,342,912
8Dec 25, 2025 13:0829,40232,359,11016,050,0001,003,0002,486,272
9Dec 25, 2025 13:0229,58443,902,03016,150,0001,009,0002,330,624
10Dec 25, 2025 13:0729,70038,902,94016,213,0001,013,0002,322,432
11Dec 25, 2025 13:1329,72238,425,44316,225,0001,014,0002,494,464
12Dec 25, 2025 13:0229,81632,372,47017,293,00002,527,232
13Dec 25, 2025 13:0529,95538,556,24717,374,00002,310,144
14Dec 25, 2025 13:0829,95738,961,28117,375,00002,269,184
15Dec 25, 2025 13:0729,96037,729,61317,377,00002,408,448
16Dec 25, 2025 13:0729,97942,684,41617,388,00002,437,120
17Dec 25, 2025 13:0829,98341,621,32417,390,00002,326,528
18Dec 25, 2025 13:0830,00338,648,20216,379,0001,023,0002,334,720
19Dec 25, 2025 13:0230,13837,097,99617,480,00002,523,136
20Dec 25, 2025 13:0730,15339,681,03317,489,00002,457,600
21Dec 25, 2025 13:0630,19740,843,52416,484,0001,030,0002,453,504
22Dec 25, 2025 13:1330,20339,844,10017,518,00002,314,240
23Dec 25, 2025 13:0730,35543,514,27717,606,00002,461,696
24Dec 25, 2025 13:0730,37836,456,10516,583,0001,036,0002,494,464
25Dec 25, 2025 13:0730,45342,245,70217,663,00002,469,888
26Dec 25, 2025 13:0630,52243,958,85116,662,0001,041,0002,523,136
27Dec 25, 2025 13:0530,55741,127,75416,681,0001,042,0002,465,792
28Dec 25, 2025 13:0830,80344,303,49515,881,0001,985,0002,494,464
29Dec 25, 2025 13:1330,81442,570,89816,880,000992,0002,519,040
30Dec 25, 2025 13:0230,85343,281,35616,901,000994,0002,400,256
31Dec 25, 2025 13:1330,85539,469,27116,902,000994,0002,449,408
32Dec 25, 2025 13:0730,87241,572,17415,917,0001,989,0002,408,448
33Dec 25, 2025 13:0830,87243,482,77016,912,000994,0002,527,232
34Dec 25, 2025 13:0631,01936,613,61916,992,000999,0002,506,752
35Dec 25, 2025 13:1331,05336,830,39417,011,0001,000,0002,412,544
36Dec 25, 2025 13:0231,10051,341,07817,036,0001,002,0002,510,848
37Dec 25, 2025 13:0831,11639,284,81617,045,0001,002,0002,482,176
38Dec 25, 2025 13:0831,13143,040,55617,053,0001,003,0002,441,216
39Dec 25, 2025 13:0231,27641,902,20917,133,0001,007,0002,441,216
40Dec 25, 2025 13:0231,30742,219,78617,150,0001,008,0002,379,776
41Dec 25, 2025 13:0731,68839,692,98117,358,0001,021,0002,486,272
42Dec 25, 2025 13:0231,69047,137,47718,380,00002,486,272