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 listApr 27, 2026 20:40TudyMTudyMScore: 202,163Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 27, 2026 20:41199,350136,815,423105,569,00010,054,0002,392,064
2Apr 27, 2026 20:41200,505134,833,136104,263,00012,030,0002,457,600
3Apr 27, 2026 20:41200,837139,042,182105,440,00011,046,0002,424,832
4Apr 27, 2026 20:41201,205135,966,000107,723,0008,976,0002,428,928
5Apr 27, 2026 20:41202,163137,115,778104,227,00013,028,0002,412,544
6Apr 27, 2026 20:41202,784134,166,902107,563,00010,052,0002,322,432
7Apr 27, 2026 20:41203,132135,095,152103,839,00013,978,0002,445,312
8Apr 27, 2026 20:41203,415134,922,943102,984,00014,997,0002,347,008
9Apr 27, 2026 20:41207,434137,729,222108,281,00012,031,0002,359,296