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 15, 2026 04:37Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 22:52Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 23:00030,030,845,00530,019,443,0001,000,00016,384Killed by timeout
2Mar 15, 2026 04:3737,76647,067,45820,909,000995,0002,297,856
3Mar 15, 2026 04:3738,18145,722,98720,132,0002,013,0002,285,568
4Mar 15, 2026 04:3739,65248,864,51621,999,000999,0002,506,752
5Mar 15, 2026 04:3739,78446,216,25521,069,0002,006,0002,457,600
6Mar 15, 2026 04:3739,89751,564,35522,134,0001,006,0002,502,656
7Mar 15, 2026 04:3739,93346,408,12521,147,0002,014,0002,494,464
8Mar 15, 2026 04:3739,96251,680,55221,163,0002,015,0002,256,896
9Mar 15, 2026 04:3739,98648,098,56321,176,0002,016,0002,457,600
10Mar 15, 2026 04:3740,49744,340,82323,488,00002,375,680