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 05:05Josu San MartinJosu San MartinScore: 36,810Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 05:0534,59746,973,07818,060,0002,006,0002,502,656
2Dec 21, 2025 05:0835,75249,367,25519,700,0001,036,0002,473,984
3Dec 21, 2025 05:0536,04848,135,44318,917,0001,991,0002,404,352
4Dec 21, 2025 05:0836,07246,860,36719,926,000996,0002,412,544
5Dec 21, 2025 05:0536,17942,757,94618,986,0001,998,0002,461,696
6Dec 21, 2025 05:0536,19746,786,52218,995,0001,999,0002,473,984
7Dec 21, 2025 05:0636,26444,508,05219,030,0002,003,0002,490,368
8Dec 21, 2025 05:0836,27943,305,01619,038,0002,004,0002,510,848
9Dec 21, 2025 05:0736,29548,091,58719,047,0002,004,0002,392,064
10Dec 21, 2025 05:0736,31744,186,11820,061,0001,003,0002,461,696
11Dec 21, 2025 05:0536,60941,785,93618,200,0003,033,0002,392,064
12Dec 21, 2025 05:0636,73146,264,94021,304,00002,334,720
13Dec 21, 2025 05:0636,81047,074,60120,334,0001,016,0002,297,856
14Dec 21, 2025 05:0636,84744,863,36120,354,0001,017,0002,412,544
15Dec 21, 2025 05:0736,85347,472,54720,358,0001,017,0002,490,368
16Dec 21, 2025 05:0536,93644,776,24521,423,00002,498,560
17Dec 21, 2025 05:0536,96445,905,79420,419,0001,020,0002,297,856
18Dec 21, 2025 05:0637,00743,744,88420,442,0001,022,0002,449,408
19Dec 21, 2025 05:0537,09342,456,72121,514,00002,486,272
20Dec 21, 2025 05:0537,10746,955,32921,522,00002,404,352
21Dec 21, 2025 05:0537,28847,561,21120,598,0001,029,0002,404,352
22Dec 21, 2025 05:0637,32449,120,53921,648,00002,408,448
23Dec 21, 2025 05:0537,33147,583,67221,652,00002,433,024
24Dec 21, 2025 05:0537,39345,222,58721,688,00002,424,832