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 24, 2026 14:46Jem BishopJem BishopScore: 3,990Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 24, 2026 14:463,83619,211,97602,225,0002,486,272
2Feb 24, 2026 14:463,86922,661,33802,244,0002,514,944
3Feb 24, 2026 14:463,87225,128,9752,246,00002,527,232
4Feb 24, 2026 14:463,87419,031,4592,247,00002,383,872
5Feb 24, 2026 14:463,90021,008,8442,262,00002,453,504
6Feb 24, 2026 14:463,90719,255,9621,133,0001,133,0002,478,080
7Feb 24, 2026 14:463,93820,494,1671,142,0001,142,0002,449,408
8Feb 24, 2026 14:463,94518,773,4882,288,00002,510,848
9Feb 24, 2026 14:463,96218,097,1002,298,00002,289,664
10Feb 24, 2026 14:463,96221,093,9001,149,0001,149,0002,494,464
11Feb 24, 2026 14:463,99020,192,4872,314,00002,498,560
12Feb 24, 2026 14:464,01419,960,6111,164,0001,164,0002,469,888
13Feb 24, 2026 14:464,01417,103,7661,164,0001,164,0002,314,240
14Feb 24, 2026 14:464,01919,139,7212,331,00002,514,944
15Feb 24, 2026 14:464,06616,991,6891,179,0001,179,0002,289,664
16Feb 24, 2026 14:464,13420,920,4372,398,00002,506,752
17Feb 24, 2026 14:464,20318,968,1911,219,0001,219,0002,420,736
18Feb 24, 2026 14:464,21425,061,6911,222,0001,222,0002,514,944
19Feb 24, 2026 14:464,21719,545,3901,223,0001,223,0002,469,888
20Feb 24, 2026 14:464,23422,852,3112,456,00002,531,328
21Feb 24, 2026 14:464,24119,816,3321,230,0001,230,0002,519,040