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 MartinScore: 36,838Success
Source Code

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

Challenge History

No challenges yet.

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