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 16, 2025 23:17HattonuriHattonuriScore: 40,743Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 16, 2025 23:1637,52146,832,34020,726,0001,036,0002,498,560
2Dec 16, 2025 23:1738,14740,639,36620,114,0002,011,0002,334,720
3Dec 16, 2025 23:1638,80342,535,56222,506,00002,334,720
4Dec 16, 2025 23:1738,88445,952,58722,553,00002,531,328
5Dec 16, 2025 23:1739,07847,449,74422,665,00002,531,328
6Dec 16, 2025 23:1639,48648,476,54021,907,000995,0002,531,328
7Dec 16, 2025 23:1739,96449,381,55722,172,0001,007,0002,531,328
8Dec 16, 2025 23:1740,41647,388,11123,441,00002,506,752
9Dec 16, 2025 23:1740,55945,984,07823,524,00002,498,560
10Dec 16, 2025 23:1640,70044,530,59023,606,00002,527,232
11Dec 16, 2025 23:1740,74351,636,08223,631,00002,506,752
12Dec 16, 2025 23:1640,84049,546,88622,658,0001,029,0002,498,560
13Dec 16, 2025 23:1641,21451,889,13922,908,000996,0002,502,656
14Dec 16, 2025 23:1741,35247,737,00822,985,000999,0002,506,752
15Dec 16, 2025 23:1641,45543,048,80622,041,0002,003,0002,506,752
16Dec 16, 2025 23:1742,15048,867,82924,447,00002,527,232
17Dec 16, 2025 23:1642,29052,078,09623,506,0001,022,0002,326,528
18Dec 16, 2025 23:1742,39845,688,18624,591,00002,502,656
19Dec 16, 2025 23:1643,37846,761,79724,153,0001,006,0002,498,560
20Dec 16, 2025 23:1743,55748,771,98424,253,0001,010,0002,527,232
21Dec 16, 2025 23:1743,76448,780,95524,368,0001,015,0002,527,232