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 listFeb 11, 2026 13:17Code HintsCode HintsError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 15:01Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 15:55010,943,280987,000020,480No answer to stdout. Died or killed?
2Feb 11, 2026 13:1840,02147,261,15321,194,0002,018,0002,285,568
3Feb 11, 2026 13:1741,22451,898,59421,918,0001,992,0002,273,280
4Feb 11, 2026 13:1841,45954,792,19622,043,0002,003,0002,445,312
5Feb 11, 2026 13:1942,06445,645,76023,381,0001,016,0002,445,312
6Feb 11, 2026 13:1742,20946,375,85224,481,00002,441,216
7Feb 11, 2026 13:1842,87250,893,08523,872,000994,0002,449,408
8Feb 11, 2026 13:1842,93455,118,80122,910,0001,992,0002,449,408
9Feb 11, 2026 13:1842,99753,652,31522,943,0001,995,0002,539,520
10Feb 11, 2026 13:2043,37159,973,10124,149,0001,006,0002,277,376
11Feb 11, 2026 13:1843,73349,353,05124,351,0001,014,0002,445,312
12Feb 11, 2026 13:2043,75947,606,85125,380,00002,539,520
13Feb 11, 2026 13:1943,77646,789,14124,375,0001,015,0002,445,312
14Feb 11, 2026 13:1743,99747,450,96725,518,00002,441,216
15Feb 11, 2026 13:1844,08647,300,26025,570,00002,539,520
16Feb 11, 2026 13:1744,62452,288,09424,887,000995,0002,539,520
17Feb 11, 2026 13:1844,66053,924,93024,907,000996,0002,539,520
18Feb 11, 2026 13:2044,66456,716,35623,913,0001,992,0002,449,408
19Feb 11, 2026 13:1744,90750,168,20324,043,0002,003,0002,449,408
20Feb 11, 2026 13:1745,01451,320,93225,104,0001,004,0002,535,424
21Feb 11, 2026 13:1945,07648,639,96424,133,0002,011,0002,539,520
22Feb 11, 2026 13:1745,23649,973,93924,219,0002,018,0002,437,120
23Feb 11, 2026 13:1745,73651,576,24426,527,00002,445,312
24Feb 11, 2026 13:1846,08855,883,66925,703,0001,028,0002,445,312
25Feb 11, 2026 13:1746,37153,597,01025,899,000996,0002,437,120