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 listSep 27, 2025 19:52Wookyung JeongWookyung JeongScore: 62,510Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 19:5359,21264,902,38328,283,0006,060,0002,473,984
2Sep 27, 2025 19:5159,32156,975,08229,347,0005,059,0002,277,376
3Sep 27, 2025 19:5059,50363,583,92429,437,0005,075,0002,494,464
4Sep 27, 2025 19:5159,65257,317,32529,510,0005,088,0002,371,584
5Sep 27, 2025 19:5360,14161,086,66331,893,0002,989,0002,392,064
6Sep 27, 2025 19:5160,26957,900,44529,963,0004,993,0002,469,888
7Sep 27, 2025 19:5160,31658,081,11230,985,0003,998,0002,367,488
8Sep 27, 2025 19:5360,32957,249,67132,992,0001,999,0002,445,312
9Sep 27, 2025 19:5360,51657,571,61729,082,0006,017,0002,367,488
10Sep 27, 2025 19:5360,73857,031,67030,196,0005,032,0002,445,312
11Sep 27, 2025 19:5060,97157,701,78731,322,0004,041,0002,490,368
12Sep 27, 2025 19:5361,03857,770,47129,333,0006,069,0002,416,640
13Sep 27, 2025 19:5361,11763,031,22528,359,0007,089,0002,428,928
14Sep 27, 2025 19:5361,12658,649,39732,415,0003,038,0002,310,144
15Sep 27, 2025 19:5361,44361,647,24132,583,0003,054,0002,437,120
16Sep 27, 2025 19:5161,51268,947,91627,523,0008,154,0002,437,120
17Sep 27, 2025 19:5061,56459,418,17930,606,0005,101,0002,453,504
18Sep 27, 2025 19:5361,61263,109,77529,609,0006,126,0002,424,832
19Sep 27, 2025 19:5061,81264,116,31627,884,0007,967,0002,519,040
20Sep 27, 2025 19:5361,88861,206,82429,913,0005,982,0002,498,560
21Sep 27, 2025 19:5161,90060,769,93330,916,0004,986,0002,441,216
22Sep 27, 2025 19:5061,90763,990,64227,927,0007,979,0002,420,736
23Sep 27, 2025 19:5261,92661,008,68730,929,0004,988,0002,367,488
24Sep 27, 2025 19:5362,01961,635,10328,977,0006,994,0002,420,736
25Sep 27, 2025 19:5362,06658,087,58929,999,0005,999,0002,285,568
26Sep 27, 2025 19:5362,08456,469,87931,008,0005,001,0002,433,024
27Sep 27, 2025 19:5362,15960,582,89432,047,0004,005,0002,482,176
28Sep 27, 2025 19:5362,20060,235,17031,066,0005,010,0002,306,048
29Sep 27, 2025 19:5262,22965,223,67233,086,0003,007,0002,359,296
30Sep 27, 2025 19:5162,26064,135,61431,096,0005,015,0002,519,040
31Sep 27, 2025 19:5362,51058,934,71831,221,0005,035,0002,400,256
32Sep 27, 2025 19:5162,51459,435,19032,230,0004,028,0002,457,600
33Sep 27, 2025 19:5262,92462,513,33432,441,0004,055,0002,478,080
34Sep 27, 2025 19:5163,55762,457,45131,882,0004,981,0002,355,200
35Sep 27, 2025 19:5263,59366,040,47932,897,0003,987,0002,494,464
36Sep 27, 2025 19:5363,61762,144,98733,907,0002,991,0002,330,624
37Sep 27, 2025 19:5163,69860,579,78030,954,0005,991,0002,428,928
38Sep 27, 2025 19:5363,73859,334,63127,976,0008,992,0002,437,120
39Sep 27, 2025 19:5363,82663,634,91229,015,0008,004,0002,465,792
40Sep 27, 2025 19:5363,90260,151,67529,050,0008,013,0002,371,584
41Sep 27, 2025 19:5163,90964,221,37332,058,0005,009,0002,347,008
42Sep 27, 2025 19:5363,91760,046,41731,061,0006,011,0002,387,968
43Sep 27, 2025 19:5164,25762,347,28631,226,0006,043,0002,461,696
44Sep 27, 2025 19:5264,36259,790,04631,277,0006,053,0002,338,816
45Sep 27, 2025 19:5264,36259,437,08328,250,0009,080,0002,363,392
46Sep 27, 2025 19:5264,70563,976,93629,415,0008,114,0002,367,488
47Sep 27, 2025 19:5364,72864,267,34633,484,0004,058,0002,445,312
48Sep 27, 2025 19:5164,76760,817,88332,489,0005,076,0002,314,240
49Sep 27, 2025 19:5264,87863,408,29331,527,0006,102,0002,490,368
50Sep 27, 2025 19:5364,89768,535,45833,571,0004,069,0002,465,792
51Sep 27, 2025 19:5364,96264,106,42530,550,0007,128,0002,326,528
52Sep 27, 2025 19:5364,96961,573,15629,535,0008,147,0002,433,024
53Sep 27, 2025 19:5165,19159,182,50232,836,0004,975,0002,433,024
54Sep 27, 2025 19:5365,48467,996,44228,986,0008,995,0002,322,432
55Sep 27, 2025 19:5365,77166,712,97432,124,0006,023,0002,519,040
56Sep 27, 2025 19:5265,85264,107,55833,169,0005,025,0002,342,912
57Sep 27, 2025 19:5065,88660,770,98533,186,0005,028,0002,453,504
58Sep 27, 2025 19:5166,20063,433,77332,334,0006,062,0002,424,832
59Sep 27, 2025 19:5366,34361,312,19531,391,0007,088,0002,428,928
60Sep 27, 2025 19:5367,27667,772,85633,017,0006,003,0002,494,464