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 listAug 27, 2026 07:17C#_userC#_userScore: 662,058Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 27, 2026 07:20546,753328,429,405308,727,0008,390,0002,150,400
2Aug 27, 2026 07:20573,581344,299,794324,456,0008,221,0002,150,400
3Aug 27, 2026 07:20608,563364,527,274344,726,0008,241,0002,236,416
4Aug 27, 2026 07:20612,062366,068,025345,666,0009,330,0002,236,416
5Aug 27, 2026 07:20662,058395,130,768374,948,0009,046,0002,150,400
6Aug 27, 2026 07:20698,344416,243,268395,602,0009,438,0002,236,416
7Aug 27, 2026 07:20780,712463,981,122444,356,0008,457,0002,154,496
8Aug 27, 2026 07:20818,598486,426,467465,325,0009,462,0002,236,416
9Aug 27, 2026 07:20840,375497,211,931478,217,0009,201,0002,170,880