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 18, 2026 22:38Yuriy LyfenkoYuriy LyfenkoScore: 3,683Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 18, 2026 22:393,40020,126,929986,000986,0002,539,520
2Mar 18, 2026 22:403,41016,973,100989,000989,0002,523,136
3Mar 18, 2026 22:403,52822,258,6141,023,0001,023,0002,342,912
4Mar 18, 2026 22:393,54822,579,00002,058,0002,338,816
5Mar 18, 2026 22:403,63121,624,3871,053,0001,053,0002,539,520
6Mar 18, 2026 22:393,64123,684,5961,056,0001,056,0002,322,432
7Mar 18, 2026 22:403,64516,655,1221,057,0001,057,0002,400,256
8Mar 18, 2026 22:403,68319,224,6131,068,0001,068,0002,412,544
9Mar 18, 2026 22:393,72823,611,7431,081,0001,081,0002,285,568
10Mar 18, 2026 22:393,74818,156,4831,087,0001,087,0002,396,160
11Mar 18, 2026 22:403,77817,703,97502,191,0002,371,584
12Mar 18, 2026 22:394,12822,149,0871,197,0001,197,0002,342,912
13Mar 18, 2026 22:385,35018,239,8182,069,0001,034,0002,371,584
14Mar 18, 2026 22:385,42820,499,8422,099,0001,049,0002,355,200
15Mar 18, 2026 22:385,47918,347,0212,119,0001,059,0002,371,584