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 04:09Josu San MartinJosu San MartinScore: 37,107Success
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 04:0835,28144,829,45020,463,00002,273,280
2Dec 21, 2025 04:1035,36249,168,75719,485,0001,025,0002,490,368
3Dec 21, 2025 03:3635,38845,290,16820,525,00002,523,136
4Dec 21, 2025 03:3835,44342,541,27220,557,00002,482,176
5Dec 21, 2025 04:0936,05246,805,73318,919,0001,991,0002,387,968
6Dec 21, 2025 04:0936,13449,569,95918,962,0001,996,0002,400,256
7Dec 21, 2025 04:0836,26044,654,32520,030,0001,001,0002,469,888
8Dec 21, 2025 04:0936,29744,107,10819,047,0002,005,0002,392,064
9Dec 21, 2025 03:3636,30343,689,97519,051,0002,005,0002,486,272
10Dec 21, 2025 04:0936,34740,735,97919,074,0002,007,0002,457,600
11Dec 21, 2025 03:3836,49750,523,60119,152,0002,016,0002,441,216
12Dec 21, 2025 03:3636,71942,871,87420,283,0001,014,0002,473,984
13Dec 21, 2025 04:0836,75544,640,47620,303,0001,015,0002,449,408
14Dec 21, 2025 04:0836,86450,801,98520,363,0001,018,0002,441,216
15Dec 21, 2025 03:3636,87653,462,51821,388,00002,478,080
16Dec 21, 2025 03:3836,88450,771,04321,393,00002,465,792
17Dec 21, 2025 04:0536,95548,844,44820,414,0001,020,0002,404,352
18Dec 21, 2025 04:1037,07143,631,19020,478,0001,023,0002,519,040
19Dec 21, 2025 04:0837,10746,725,86320,498,0001,024,0002,367,488
20Dec 21, 2025 04:0537,16443,887,23620,529,0001,026,0002,416,640
21Dec 21, 2025 03:3637,16943,269,83120,532,0001,026,0002,498,560
22Dec 21, 2025 04:0937,22643,405,64521,591,00002,486,272
23Dec 21, 2025 04:1037,24145,713,56220,572,0001,028,0002,326,528
24Dec 21, 2025 04:0937,27944,645,03221,622,00002,424,832
25Dec 21, 2025 04:0837,39046,639,78720,654,0001,032,0002,306,048
26Dec 21, 2025 04:0837,43351,344,50421,711,00002,519,040
27Dec 21, 2025 04:0837,44052,754,58720,681,0001,034,0002,461,696
28Dec 21, 2025 04:0837,46255,504,66220,694,0001,034,0002,273,280
29Dec 21, 2025 04:0937,48446,060,14721,741,00002,404,352
30Dec 21, 2025 04:0837,48648,643,82120,707,0001,035,0002,265,088
31Dec 21, 2025 04:0937,74747,989,43920,898,000995,0002,375,680
32Dec 21, 2025 04:0837,75251,813,30620,901,000995,0002,404,352
33Dec 21, 2025 04:0537,76047,756,89020,906,000995,0002,375,680
34Dec 21, 2025 03:3637,77147,677,92920,912,000995,0002,338,816
35Dec 21, 2025 04:0937,77446,150,00919,918,0001,991,0002,428,928
36Dec 21, 2025 04:0837,96044,940,91520,016,0002,001,0002,318,336