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 23, 2025 14:51Josu San MartinJosu San MartinScore: 36,033Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 23, 2025 14:5133,61650,212,50518,471,0001,026,0002,486,272
2Dec 23, 2025 14:5033,95337,866,30719,693,00002,265,088
3Dec 23, 2025 14:5334,31750,731,33017,914,0001,990,0002,494,464
4Dec 23, 2025 14:5134,32145,157,69317,916,0001,990,0002,449,408
5Dec 23, 2025 14:5334,51044,255,49518,015,0002,001,0002,465,792
6Dec 23, 2025 14:5334,65742,684,14218,091,0002,010,0002,494,464
7Dec 23, 2025 14:5134,78441,119,31719,167,0001,008,0002,506,752
8Dec 23, 2025 14:5134,83150,986,35818,182,0002,020,0002,461,696
9Dec 23, 2025 14:5135,04843,768,27819,312,0001,016,0002,441,216
10Dec 23, 2025 14:5035,22939,377,08419,412,0001,021,0002,285,568
11Dec 23, 2025 14:5335,31942,834,54219,461,0001,024,0002,441,216
12Dec 23, 2025 14:5335,37449,314,85519,492,0001,025,0002,453,504
13Dec 23, 2025 14:5335,38353,185,25619,496,0001,026,0002,428,928
14Dec 23, 2025 14:5135,50246,931,96919,562,0001,029,0002,473,984
15Dec 23, 2025 14:5335,52638,000,47019,575,0001,030,0002,416,640
16Dec 23, 2025 14:5135,69047,071,67620,700,00002,265,088
17Dec 23, 2025 14:5136,03343,849,65518,909,0001,990,0002,412,544
18Dec 23, 2025 14:5136,08650,723,55718,937,0001,993,0002,441,216
19Dec 23, 2025 14:5336,10051,988,63819,941,000997,0002,334,720
20Dec 23, 2025 14:5136,10941,275,54820,943,00002,437,120
21Dec 23, 2025 14:5336,27947,014,30920,040,0001,002,0002,416,640
22Dec 23, 2025 14:5336,44843,993,99020,134,0001,006,0002,416,640
23Dec 23, 2025 14:5336,51249,622,93419,161,0002,016,0002,371,584
24Dec 23, 2025 14:5336,56942,678,66319,190,0002,020,0002,453,504
25Dec 23, 2025 14:5336,70942,415,82320,278,0001,013,0002,416,640
26Dec 23, 2025 14:5036,74150,157,50720,296,0001,014,0002,420,736
27Dec 23, 2025 14:5336,94042,784,18520,405,0001,020,0002,428,928
28Dec 23, 2025 14:5137,01042,096,55720,444,0001,022,0002,334,720
29Dec 23, 2025 14:5137,01638,115,36821,469,00002,433,024
30Dec 23, 2025 14:5137,14544,967,88620,519,0001,025,0002,363,392
31Dec 23, 2025 14:5337,37248,858,55220,644,0001,032,0002,273,280
32Dec 23, 2025 14:5137,45553,051,31721,724,00002,465,792
33Dec 23, 2025 14:5137,50247,778,87020,716,0001,035,0002,338,816