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 listOct 4, 2026 14:25Karthik ReddyKarthik ReddyError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 4, 2026 14:25084,541,22068,221,0005,162,0002,117,632expected "3201062 ", got "first ++++++++++++++++++++++++++++++++++++++++ | dpos dec: 0 53 106 158 212 264 317 371 428 497 823 | price dec: 511 1121 1356 1495 1639 1804 1965 2124 2315 2638 4068 | buy mean 11258 | near10 0.000 near100 0.000 "
2Oct 4, 2026 14:25082,694,84664,886,0005,076,0002,117,632expected "946577 ", got "first +++++++++++++++++++++++++++++++++-++++++ | dpos dec: 0 53 105 158 210 263 315 369 426 495 805 | price dec: 299 548 608 660 708 757 809 867 933 1020 1761 | buy mean 11165 | near10 0.000 near100 0.002 "
3Oct 4, 2026 14:25079,862,29464,201,0005,169,0002,117,632expected "713176 ", got "first ++++++++++++++++++++++++++++++++++++++++ | dpos dec: 0 52 105 158 211 263 316 370 427 495 790 | price dec: 304 548 615 672 719 759 797 839 889 965 1534 | buy mean 11257 | near10 0.000 near100 0.004 "