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 22, 2025 02:30Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:29Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:41010,646,052727,000020,480No answer to stdout. Died or killed?
2Dec 22, 2025 03:3534,39045,371,79718,949,000997,0002,506,752
3Dec 22, 2025 02:5634,50743,629,86619,014,0001,000,0002,510,848
4Dec 22, 2025 02:1234,59847,014,20518,061,0002,006,0002,482,176
5Dec 22, 2025 02:3534,94845,583,83620,270,00002,473,984
6Dec 22, 2025 03:3635,02241,768,93420,313,00002,494,464
7Dec 22, 2025 02:0335,13143,623,57419,358,0001,018,0002,482,176
8Dec 22, 2025 03:0035,23343,898,22619,414,0001,021,0002,482,176
9Dec 22, 2025 02:5735,24743,829,44019,421,0001,022,0002,490,368
10Dec 22, 2025 02:5635,32842,514,62020,490,00002,473,984
11Dec 22, 2025 02:2935,33145,646,52119,468,0001,024,0002,506,752
12Dec 22, 2025 02:3635,35045,310,42119,478,0001,025,0002,473,984
13Dec 22, 2025 02:0535,36244,069,59619,485,0001,025,0002,469,888
14Dec 22, 2025 02:5635,36243,778,62519,485,0001,025,0002,490,368
15Dec 22, 2025 02:1235,39148,057,00720,527,00002,465,792
16Dec 22, 2025 02:2935,41637,880,84319,514,0001,027,0002,461,696
17Dec 22, 2025 03:5335,41745,734,28219,515,0001,027,0002,408,448
18Dec 22, 2025 02:5635,47447,883,43219,547,0001,028,0002,461,696
19Dec 22, 2025 02:0535,58147,328,11419,606,0001,031,0002,510,848
20Dec 22, 2025 02:5735,62445,114,18719,629,0001,033,0002,490,368
21Dec 22, 2025 03:0035,65945,570,99719,648,0001,034,0002,408,448
22Dec 22, 2025 02:3635,66650,407,60819,652,0001,034,0002,441,216
23Dec 22, 2025 02:5735,74747,055,90419,697,0001,036,0002,465,792
24Dec 22, 2025 02:3635,79053,701,25619,721,0001,037,0002,347,008
25Dec 22, 2025 03:5436,03149,864,68219,903,000995,0002,383,872
26Dec 22, 2025 04:1336,03445,910,72118,910,0001,990,0002,465,792
27Dec 22, 2025 03:5336,04047,020,25818,913,0001,990,0002,420,736
28Dec 22, 2025 02:5636,04549,502,70418,915,0001,991,0002,412,544
29Dec 22, 2025 03:3536,05349,243,60820,911,00002,453,504
30Dec 22, 2025 02:3636,05347,767,36118,920,0001,991,0002,392,064
31Dec 22, 2025 02:1236,07143,928,99418,929,0001,992,0002,441,216
32Dec 22, 2025 02:3536,14549,736,44318,968,0001,996,0002,408,448
33Dec 22, 2025 02:5636,15241,732,04719,970,000998,0002,273,280
34Dec 22, 2025 02:5636,19549,113,55618,994,0001,999,0002,412,544
35Dec 22, 2025 03:5436,21646,189,01019,005,0002,000,0002,445,312
36Dec 22, 2025 02:5636,34545,855,31819,073,0002,007,0002,437,120
37Dec 22, 2025 03:3636,36742,303,66420,089,0001,004,0002,449,408
38Dec 22, 2025 02:5636,40540,160,07420,110,0001,005,0002,416,640
39Dec 22, 2025 03:5336,41045,118,59720,113,0001,005,0002,469,888
40Dec 22, 2025 02:5636,49747,764,61018,144,0003,024,0002,334,720
41Dec 22, 2025 02:3036,72942,831,73621,303,00002,486,272
42Dec 22, 2025 02:3036,73144,013,64020,290,0001,014,0002,400,256
43Dec 22, 2025 02:3636,80242,409,27820,329,0001,016,0002,322,432
44Dec 22, 2025 03:3536,83847,625,82221,366,00002,449,408
45Dec 22, 2025 02:0336,87453,854,91621,387,00002,445,312
46Dec 22, 2025 02:5636,89545,167,96720,380,0001,019,0002,441,216
47Dec 22, 2025 04:1336,90945,497,86921,407,00002,392,064
48Dec 22, 2025 02:3636,91243,681,26820,390,0001,019,0002,420,736
49Dec 22, 2025 02:5636,94043,368,92720,405,0001,020,0002,412,544
50Dec 22, 2025 02:5636,94548,098,74320,408,0001,020,0002,404,352
51Dec 22, 2025 02:5736,98344,273,62920,429,0001,021,0002,334,720
52Dec 22, 2025 03:5436,99544,669,09721,457,00002,416,640
53Dec 22, 2025 02:5637,00345,880,21021,462,00002,379,776
54Dec 22, 2025 03:0037,01048,457,87020,444,0001,022,0002,351,104
55Dec 22, 2025 02:3637,02647,310,93020,453,0001,022,0002,506,752
56Dec 22, 2025 02:3337,06056,069,93920,472,0001,023,0002,441,216
57Dec 22, 2025 02:5637,07243,909,00520,479,0001,023,0002,265,088
58Dec 22, 2025 02:1237,10045,075,48620,494,0001,024,0002,318,336
59Dec 22, 2025 02:5737,10242,854,48820,495,0001,024,0002,502,656
60Dec 22, 2025 02:0537,12447,012,10720,507,0001,025,0002,301,952
61Dec 22, 2025 02:3337,12451,567,92321,532,00002,342,912
62Dec 22, 2025 04:1337,20747,903,33120,553,0001,027,0002,404,352
63Dec 22, 2025 02:3337,21642,513,01720,558,0001,027,0002,437,120
64Dec 22, 2025 02:5737,22144,692,03420,560,0001,028,0002,355,200
65Dec 22, 2025 02:3637,30260,015,53120,605,0001,030,0002,310,144
66Dec 22, 2025 02:0337,35348,092,32120,634,0001,031,0002,265,088
67Dec 22, 2025 02:2937,38150,268,45820,649,0001,032,0002,338,816
68Dec 22, 2025 02:3637,39053,609,16520,654,0001,032,0002,359,296
69Dec 22, 2025 02:3037,42847,344,51020,675,0001,033,0002,416,640
70Dec 22, 2025 02:5637,46748,324,61321,731,00002,416,640
71Dec 22, 2025 02:5637,49148,231,85121,745,00002,461,696
72Dec 22, 2025 02:3037,55055,148,38220,742,0001,037,0002,420,736
73Dec 22, 2025 02:5637,76048,124,47220,906,000995,0002,502,656
74Dec 22, 2025 02:3037,76051,367,33918,915,0002,986,0002,383,872
75Dec 22, 2025 02:5637,76248,166,43520,907,000995,0002,404,352
76Dec 22, 2025 02:3537,76645,908,85019,913,0001,991,0002,273,280
77Dec 22, 2025 02:3637,78347,774,11520,918,000996,0002,445,312
78Dec 22, 2025 02:3637,87250,839,25220,968,000998,0002,318,336
79Dec 22, 2025 02:3637,88854,961,48319,978,0001,997,0002,322,432
80Dec 22, 2025 02:3037,89751,039,47019,982,0001,998,0002,392,064
81Dec 22, 2025 03:3637,92246,670,31019,996,0001,999,0002,445,312
82Dec 22, 2025 02:1237,97843,840,71821,026,0001,001,0002,285,568
83Dec 22, 2025 02:1238,01247,093,65020,043,0002,004,0002,334,720
84Dec 22, 2025 02:5638,02947,800,42421,055,0001,002,0002,433,024
85Dec 22, 2025 02:5638,28846,147,38221,198,0001,009,0002,498,560