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 03:02HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:00Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:0002,758,634,2432,750,771,000016,384No answer to stdout. Died or killed?
2Dec 21, 2025 03:0029,93137,227,23716,339,0001,021,0002,523,136
3Dec 21, 2025 03:0230,11639,694,02617,467,00002,342,912
4Dec 21, 2025 03:0130,61742,444,58617,758,00002,494,464
5Dec 21, 2025 03:0130,87245,567,09617,906,00002,510,848
6Dec 21, 2025 03:0030,87936,179,32816,915,000995,0002,301,952
7Dec 21, 2025 03:0130,99336,306,07316,978,000998,0002,326,528
8Dec 21, 2025 03:0131,07241,791,28417,021,0001,001,0002,306,048
9Dec 21, 2025 03:0131,27644,266,51117,133,0001,007,0002,494,464
10Dec 21, 2025 03:0031,30946,114,65517,151,0001,008,0002,301,952
11Dec 21, 2025 03:0131,40031,964,60017,201,0001,011,0002,510,848
12Dec 21, 2025 03:0031,41936,573,16616,199,0002,024,0002,375,680
13Dec 21, 2025 02:5931,45939,255,30817,233,0001,013,0002,482,176
14Dec 21, 2025 03:0031,65942,837,31118,362,00002,281,472
15Dec 21, 2025 03:0232,02836,181,26217,544,0001,032,0002,306,048
16Dec 21, 2025 03:0032,19843,352,16818,675,00002,519,040
17Dec 21, 2025 03:0132,27449,115,95318,719,00002,457,600
18Dec 21, 2025 03:0132,61645,414,27716,926,0001,991,0002,510,848
19Dec 21, 2025 03:0032,62146,486,93718,920,00002,510,848
20Dec 21, 2025 03:0232,62246,632,51517,926,000995,0002,318,336
21Dec 21, 2025 03:0032,70345,817,53917,970,000998,0002,506,752
22Dec 21, 2025 02:5932,76041,387,24918,001,0001,000,0002,301,952
23Dec 21, 2025 03:0232,80045,708,05218,023,0001,001,0002,506,752
24Dec 21, 2025 03:0132,81744,416,88617,031,0002,003,0002,490,368
25Dec 21, 2025 03:0232,86945,342,90518,061,0001,003,0002,519,040
26Dec 21, 2025 03:0132,89544,473,01316,067,0003,012,0002,404,352
27Dec 21, 2025 02:5932,92445,840,97417,086,0002,010,0002,490,368
28Dec 21, 2025 03:0133,07842,264,01817,166,0002,019,0002,424,832
29Dec 21, 2025 03:0233,53336,840,13218,426,0001,023,0002,482,176
30Dec 21, 2025 03:0134,29144,266,00018,895,000994,0002,420,736
31Dec 21, 2025 03:0034,32842,026,82518,915,000995,0002,371,584