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 18:14Josu San MartinJosu San MartinScore: 36,974Success
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 18:1434,81441,817,57419,183,0001,009,0002,510,848
2Dec 21, 2025 18:1435,31639,214,72419,459,0001,024,0002,502,656
3Dec 21, 2025 18:1535,59040,191,93519,610,0001,032,0002,473,984
4Dec 21, 2025 18:5135,63346,665,11120,667,00002,510,848
5Dec 21, 2025 18:1435,96647,831,74419,867,000993,0002,490,368
6Dec 21, 2025 16:1236,02145,101,47519,898,000994,0002,322,432
7Dec 21, 2025 16:1236,02953,773,15919,902,000995,0002,457,600
8Dec 21, 2025 18:1636,06945,305,06118,928,0001,992,0002,494,464
9Dec 21, 2025 18:1436,19550,191,62018,994,0001,999,0002,371,584
10Dec 21, 2025 18:1436,23649,748,14819,016,0002,001,0002,502,656
11Dec 21, 2025 18:1436,32943,755,15319,065,0002,006,0002,408,448
12Dec 21, 2025 18:1436,37443,063,12320,093,0001,004,0002,506,752
13Dec 21, 2025 18:1536,64145,614,47920,240,0001,012,0002,416,640
14Dec 21, 2025 18:1436,65041,512,47720,245,0001,012,0002,502,656
15Dec 21, 2025 18:1536,74548,595,06920,298,0001,014,0002,506,752
16Dec 21, 2025 18:1436,84846,435,41521,372,00002,310,144
17Dec 21, 2025 18:1436,90549,374,55319,367,0002,038,0002,265,088
18Dec 21, 2025 18:5136,91039,358,21120,389,0001,019,0002,453,504
19Dec 21, 2025 18:1536,97442,312,69520,424,0001,021,0002,461,696
20Dec 21, 2025 18:1636,98844,035,11521,453,00002,465,792
21Dec 21, 2025 18:1437,02241,596,07220,451,0001,022,0002,416,640
22Dec 21, 2025 18:1537,05348,478,96020,468,0001,023,0002,490,368
23Dec 21, 2025 18:1437,08446,058,92720,485,0001,024,0002,445,312
24Dec 21, 2025 18:1437,17849,449,98221,563,00002,281,472
25Dec 21, 2025 18:1437,17943,396,68321,564,00002,506,752
26Dec 21, 2025 18:1437,18342,904,23621,566,00002,420,736
27Dec 21, 2025 18:1437,21246,893,11020,556,0001,027,0002,510,848
28Dec 21, 2025 18:1437,24044,898,20720,571,0001,028,0002,318,336
29Dec 21, 2025 18:1437,24545,064,02921,602,00002,433,024
30Dec 21, 2025 18:1437,36748,733,39520,641,0001,032,0002,387,968
31Dec 21, 2025 18:5137,44149,382,98419,742,0001,974,0002,322,432
32Dec 21, 2025 18:1637,45748,239,54521,725,00002,297,856
33Dec 21, 2025 18:1437,73648,415,82021,887,00002,404,352
34Dec 21, 2025 16:1237,74849,744,28319,904,0001,990,0002,318,336
35Dec 21, 2025 18:1537,80252,521,00920,929,000996,0002,457,600
36Dec 21, 2025 18:1438,01641,443,44420,045,0002,004,0002,420,736