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 listMar 2, 2026 21:45JuneLJuneLError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:17Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:29010,955,9041,067,000024,576No answer to stdout. Died or killed?
2Mar 2, 2026 21:4551,91457,876,61730,110,00002,433,024
3Mar 2, 2026 21:4652,59861,234,20530,507,00002,437,120
4Mar 2, 2026 21:4653,84353,161,29030,222,0001,007,0002,428,928
5Mar 2, 2026 21:4654,21454,625,94030,430,0001,014,0002,428,928
6Mar 2, 2026 21:4655,11457,417,00129,969,0001,997,0002,494,464
7Mar 2, 2026 21:4655,23654,834,32830,035,0002,002,0002,514,944
8Mar 2, 2026 21:4555,39554,172,76530,121,0002,008,0002,498,560
9Mar 2, 2026 21:4655,40760,384,41931,132,0001,004,0002,486,272
10Mar 2, 2026 21:4656,23660,750,98631,598,0001,019,0002,494,464
11Mar 2, 2026 21:4657,13864,220,84732,136,0001,004,0002,486,272
12Mar 2, 2026 21:4557,46662,121,86031,310,0002,020,0002,420,736
13Mar 2, 2026 21:4658,43666,407,48232,897,000996,0002,473,984
14Mar 2, 2026 21:4658,61458,818,59032,997,000999,0002,469,888
15Mar 2, 2026 21:4558,99159,904,13732,203,0002,012,0002,400,256
16Mar 2, 2026 21:4659,44357,032,50733,463,0001,014,0002,469,888
17Mar 2, 2026 21:4560,62861,250,45634,160,0001,004,0002,445,312
18Mar 2, 2026 21:4562,87658,495,06636,468,00002,498,560
19Mar 2, 2026 21:4563,67862,368,80435,935,000998,0002,408,448
20Mar 2, 2026 21:4666,07662,411,45037,316,0001,008,0002,420,736
21Mar 2, 2026 21:4572,01069,527,15440,748,0001,018,0002,392,064
22Mar 2, 2026 21:4574,64363,910,22641,280,0002,013,0002,433,024